使用 ansible 剧本创建具有多行的 .csv 文件时出现问题 - dict 对象没有属性 stdout_lines

问题描述 投票:0回答:1

我正在尝试创建一个 .csv 文件,该文件在不同的行中列出该服务器上的 Linux 主机名和用户。所以我希望它看起来像这样:

Hostname  User
server1   bill
server1   bonnie
server2   jim
server2   jack

我被困在为每个用户创建新行,它只是创建一个长行。我应该使用循环还是 with_items?我得到了相同的 {"msg": "'dict object' has no attribute 'stdout_lines'"} 每个错误。

这是我的剧本,我相信最后一行是我的问题所在:

---
- hosts: all
  gather_facts: true
  tasks:
  - name: Create empty /tmp/rhel_user_list.csv file
    shell:  echo 'Hostname, User List' > {{report_path}}
    args:
      chdir: /tmp
    run_once: true

  - name: Get user list
    getent:
      database: passwd
    register: user_list

  - debug:
      var: user_list

  - name: Show gathered local user names only
    debug:
      msg: "{{ user_list.results | json_query('[].ansible_facts.getent_passwd.keys(@)') }}"

  - name: Write username only to file
    lineinfile:
      line: "{{ansible_hostname}}, {{ getent_passwd.keys() | list }}"
      path: "{{report_path}}"
    with_items: "{{ user_list.stdout_lines }}"

我试过使用 loop 和 with_items 无济于事。我得到相同的 dict 对象没有属性,但我不确定要使用哪个属性。

linux ansible yaml redhat
1个回答
0
投票

例如,下面的剧本将所有主机和用户的聚合写入控制器

- hosts: all

  tasks:

    - getent:
        database: passwd

    - copy:
        dest: /tmp/user_list.csv
        content: |
          Hostname User
          {% for h in ansible_play_hosts %}
          {% for u in hostvars[h].getent_passwd %}
          {{ h }} {{ u }}
          {% endfor %}
          {% endfor %}
      delegate_to: localhost
      run_once: true

备注:

© www.soinside.com 2019 - 2024. All rights reserved.