如何使用 ansible 找到用户 ID 并在 jinja2 模板中使用它?

Flo*_*rin 0 variables jinja2 ansible

我必须使用 ansible 创建多个用户。我在 vars 部分的 ansible play 中将用户作为列表传递:

  vars:
    users: ['user1', 'user2']
Run Code Online (Sandbox Code Playgroud)

然后我必须创建一个使用该用户 ID 作为参数的脚本。脚本中的命令是这样的:

blobfuse $1 --tmp-path=/mnt/resource/{{ item }}/ -o attr_timeout=240 -o entry_timeout=240 -o negative_timeout=120 -o uid=$USER_ID -o allow_other --container-name={{ item }} --file-cache-timeout-in-seconds=120 --config-file=/root/connection-{{ item }}.cfg

一切正常,除了uid= 我已经尝试使用查找的管道插件,但这并没有让我获得正确的 UID:

{{ lookup('pipe', 'grep -w {{ item }} /etc/passwd | cut -d : -f3') }}

我的最终目标是获取每个创建的用户的 UID,并将其传递给上面的 blobfuse 命令。

Vla*_*tka 6

问:“获取每个创建的用户的 UID。”

A:模块getent正是为了这个目的。例如

- hosts: localhost
  vars:
    my_users: ['root', 'admin']
  tasks:
    - getent:
        database: passwd
    - debug:
        msg:
          - "{{ item }} uid: {{ getent_passwd[item].1 }}"
          - "{{ item }} gid: {{ getent_passwd[item].2 }}"
          - "{{ item }} home: {{ getent_passwd[item].4 }}"
          - "{{ item }} shell: {{ getent_passwd[item].5 }}"
      loop: "{{ my_users }}"
Run Code Online (Sandbox Code Playgroud)

给

ok: [localhost] => (item=root) => {
    "msg": [
        "root uid: 0", 
        "root gid: 0", 
        "root home: /root", 
        "root shell: /bin/bash"
    ]
}
ok: [localhost] => (item=admin) => {
    "msg": [
        "admin uid: 1002", 
        "admin gid: 1002", 
        "admin home: /home/admin", 
        "admin shell: /bin/bash"
    ]
}
Run Code Online (Sandbox Code Playgroud)

模块getent自动创建getent_passwd可以在模板中使用的字典。例如模板

shell> cat template.j2
{% for user in my_users %}
{{ user }} {{ getent_passwd[user].1 }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
    - template:
        src: template.j2
        dest: my_users.txt
Run Code Online (Sandbox Code Playgroud)

给

shell> cat my_users.txt 
root 0
admin 1002
Run Code Online (Sandbox Code Playgroud)


小智 5

我也有同样的问题。

不幸的是,查找插件仅在控制节点上有用。
换句话说,受控用户(具有相同的uid)需要同时位于控制节点和所有被管理节点上。否则任务将失败并返回代码 1。

对我来说,使用getent模块是一个不错的解决方案。
或者,所有内容实际上都存在于用户模块的返回值中。我只需要注册其返回值即可访问它们。

下面的剧本演示了如何在任意后续任务(调试)和 jinja 模板中根据用户模块的返回值计算uid 。

- name: Demonstrate How to Compute UID after User Creations
  hosts: all
  become: true
  gather_facts: no
  vars:
    my_users:
      - u1
      - u2
      - u3
  tasks:
    - name: Create users in loop
      user:
        name: "{{ item }}"
      loop: "{{ my_users }}"
      register: created_users

    - name: Display created users
      debug:
        msg: "User: uid:{{ item.uid }} name:{{ item.name }} group:{{ item.group }} home:{{ item.home }} shell:{{ item.shell }}"
      loop: "{{ created_users.results }}"
      # Use loop_control-label to suppress item verbosity
      loop_control:
        label: "{{ item.name }}"

    - name: Work with template
      template:
        src: myusers.j2
        dest: /tmp/myusers.txt
Run Code Online (Sandbox Code Playgroud)

这是 myusers.j2 模板。

{{ ansible_managed | comment }}
{% for item in created_users.results %}
User: uid:{{ item.uid }} name:{{ item.name }} group:{{ item.group }} home:{{ item.home }} shell:{{ item.shell }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

输出如下:

$ ansible-playbook compute-uid.yml

PLAY [Demonstrate How to Compute UID after User Creations] **********************************************************************************************

TASK [Create users in loop] *****************************************************************************************************************************
changed: [ansible1] => (item=u1)
changed: [ansible1] => (item=u2)
changed: [ansible1] => (item=u3)

TASK [Display created users] ****************************************************************************************************************************
ok: [ansible1] => (item=u1) => {
    "msg": "User: uid:1001 name:u1 group:1001 home:/home/u1 shell:/bin/bash"
}
ok: [ansible1] => (item=u2) => {
    "msg": "User: uid:1002 name:u2 group:1002 home:/home/u2 shell:/bin/bash"
}
ok: [ansible1] => (item=u3) => {
    "msg": "User: uid:1003 name:u3 group:1003 home:/home/u3 shell:/bin/bash"
}

TASK [Work with template] *******************************************************************************************************************************
changed: [ansible1]

PLAY RECAP **********************************************************************************************************************************************
ansible1                   : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

$ ansible all -a "cat /tmp/myusers.txt"
ansible1 | CHANGED | rc=0 >>
#
# Ansible managed
#
User: uid:1001 name:u1 group:1001 home:/home/u1 shell:/bin/bash
User: uid:1002 name:u2 group:1002 home:/home/u2 shell:/bin/bash
User: uid:1003 name:u3 group:1003 home:/home/u3 shell:/bin/bash
Run Code Online (Sandbox Code Playgroud)