创建多个用户的Ansible jinja字典以为每个用户创建多个sudo文件

huangapple go评论83阅读模式
英文:

Ansible jinja dictionary create multiple sudo files for each user

问题

我正在尝试为每个用户创建一个sudo文件。

Playbook(剧本):

- name:
  hosts: all
  gather_facts: false
  tasks:
    - name:
      template:
        src: sudo.j2
        dest: "/etc/sudoers.d/{{item.name}}"
      loop: "{{userinfo}}"
      when: "'admins' in item.groupname"

Var文件:

userinfo:
  - groupname: admins
    name: bill
  - groupname: admins
    name: bob
  - groupname: devs
    name: bea

Jinja文件:

{% for item in userinfo %}
{% if item.groupname=="admins" %}
{{item.name}} ALL=ALL NOPASSWD:ALL
{% endif %}
{% endfor %}

我得到的结果是两个文件,但包含了两个用户的信息。

bill ALL=ALL NOPASSWD:ALL
bob ALL=ALL NOPASSWD:ALL

如何使其正常工作,使每个文件仅包含该用户的信息?

英文:

I'm trying to create a sudo file for each user.

Playbook:

- name:
  hosts: all
  gather_facts: false
  tasks:
    - name:
      template:
        src: sudo.j2
        dest: "/etc/sudoers.d/{{item.name}}"
      loop: "{{userinfo}}"
      when: "'admins' in item.groupname"

Var file:

userinfo:
  - groupname: admins
    name: bill
  - groupname: admins
    name: bob
  - groupname: devs
    name: bea

Jinja file:

{% for item in userinfo %}
{% if item.groupname=="admins" %}
{{item.name}} ALL=ALL NOPASSWD:ALL
{% endif %}
{% endfor %}

What I am getting is two files but with information of both users.

bill ALL=ALL NOPASSWD:ALL
bob ALL=ALL NOPASSWD:ALL

How do I make it work such that each file contains information of that user only

答案1

得分: 1

问题在于您有2个循环:一个在剧本中,另一个在模板Jinja文件中;尝试将模板文件中仅包含模板信息

{{ item.name }} ALL=ALL NOPASSWD:ALL
英文:

The issue is that you have 2 loops: one in the playbook, the other in the template jinja file; try leaving the template file with the templated information only

{{ item.name }} ALL=ALL NOPASSWD:ALL

huangapple
  • 本文由 发表于 2023年1月9日 04:59:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定