Ansible:parsing/concatenating getent 模块的输出

Ansible: parsing/concatenating output of getent module

我尝试为 sftp 用户设置 chroot,以便他们可以根据 this articlels -l 上看到 user/group 个名称。为此,我需要获取 getent 命令的输出并将其放入 /chroots/{{ user.username }}/etc/passwd 文件。

我尝试使用 Ansible 来替换这个命令 getent passwd sftpuser > /chroots/sftpuser/etc/passwd 如下:

- name: get {{ user.username }} user info
  getent:
    database: passwd
    key: "{{ user.username }}"

- debug:
    var: getent_passwd

- name: create /chroots/{{ user.username }}/etc/passwd file
  lineinfile:
    path: /chroots/{{ user.username }}/etc/passwd
    line: "{{ getent_passwd | from_json }}"
    state: present
    create: yes
    owner: root
    group: root
    mode: '0644'

'getent_passwd' 看起来如下:

ok: [cf1] => {
    "getent_passwd": {
        "testuser1": [
            "x", 
            "1001", 
            "1002", 
            "", 
            "/home/testuser1", 
            "/usr/sbin/nologin"
        ]
    }
}

但是我得到这个错误:FAILED! => {"failed": true, "msg": "Unexpected templating type error occurred on ({{ getent_passwd | from_json }}): expected string or buffer"}

  1. getent_passwd 提供的这些值放入一个由“:”连接的扁平字符串中的正确方法是什么?
  2. 以这种方式将 genent 模块与 key: "root" 一起使用而不是 echo "root:x:0:0:not really root:::" >> /chroots/sftpuser/etc/passwd 是否安全?
  3. 一个可以 运行 getent passwd user1 user2 - 是否可以通过某种方式向 ansible 的 getent 模块提供两个密钥?

What is the proper way to get those values supplied by getent_passwd into one flat string joined by ":"?

例如使用带有 join filter 的 Jinja2 模板:

- debug:
    msg: "{{ user.username }}:{{getent_passwd[user.username]|join(':')}}"

One can run getent passwd user1 user2 - is it possible to supply two keys to the ansible's getent module somehow?

没有。要么一个要么全部。

在第一种情况下使用外循环请求值,或在第二种情况下过滤结果列表。