Ansible:解析/连接模块的输出



我尝试为 sftp 用户设置 chroot,以便他们可以按照本文在ls -l上看到用户/组名称。为此,我需要获取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 模块而不是echo "root:x:0:0:not really root:::" >> /chroots/sftpuser/etc/passwd key: "root"是否安全?
  3. 可以运行getent passwd user1 user2 - 是否可以以某种方式为 ansible 的 getent 模块提供两个密钥?

getent_passwd提供的这些值放入一个由":"连接的平面字符串的正确方法是什么?

例如,使用带有join过滤器的 Jinja2 模板:

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

可以运行 getent passwd user1 user2 - 是否可以以某种方式为 ansible 的 getent 模块提供两个键?

不。要么是单个,要么是全部。

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

最新更新