首页 > 解决方案 > 将列表保存到ansible中的文件列

问题描述

我创建了可以使用某些扩展名中的文件列表更新纯文本 CSV 文件(报告)的代码:

- name: Find files with extension {{ extensions }} in paths {{ paths }}
  find:
    paths: "{{ path }}"
    recurse: yes
    patterns: "{{ extensions }}"
    file_type: file
  register: files_matched
  changed_when: false

 - name: Show files paths
   debug:
     msg: "{{ files_matched.files | map(attribute='path') | list }}"
   register: files

输出正在文件中注册:

- name: Save outputs from hosts
  become: false
  lineinfile:
    dest:  "{{ hostvars['localhost'].base_dir }}/files_to_remove.csv"
    line: "{{ inventory_hostname }};{{ server_env }};{{ path }};{{ files_matched.files | map(attribute='path') | list }}"
    insertafter: EOF
  delegate_to: localhost
  changed_when: false

保存在文件中的输出如下:

server001;PRD;/root;[u'/root/Agent-Core-RedHat_EL7-11.0.0-326.x86_64.rpm', u'/root/Agent-Core-RedHat_x86_64.rpm', u'/root/Agent-Core.rpm', u'/root/some_file.tar']

也许解决方案很简单,但我无法更新行/行以生成 CSV 报告,该报告可以轻松导入 Excel 以获得如下内容:

server1 | PRD | /root | /root/Agent-Core-RedHat_EL7-11.0.0-326.x86_64.rpm |
        |     |       | /root/Agent-Core-RedHat_x86_64.rpm                |
        |     |       | /root/some_file.tar                               |
--------+-----+-------+---------------------------------------------------+
server2 | PRD | /root | /root/1.rpm                                       |
        |     |       | /root/some_file.rpm                               |
        |     |       | /root/some_file.tar                               |

...我花了一天时间寻找解决方案,但我可能不明白一些事情。可以按照上述格式进行吗?我不知道如何才能拥有这样的 CSV。

标签: ansible

解决方案


推荐阅读