Ansible 自定义包含剧本中多个任务输出的本地日志文件

Ansible 自定义包含剧本中多个任务输出的本地日志文件

在我的剧本中,我有两个主要任务,一个任务是更改 Debian 机器的网关最后一位数字,第二个任务适用于 Redhat 机器,一切运行正常,但能够在本地文件(一种日志)上跟踪成功执行任务的机器。

我尝试用寄存器记录这两项任务的结果

对于 Debian:

 name: change default gateway address
      replace:
        path: /etc/network/interfaces
        regexp: '(up route add default gw [\d]*\.[\d]*.[\d]*)\.[\d]*$'
        replace: '\1.254'
        backup: yes
      when: (ansible_facts['distribution'] == "Debian")
      register: debresult

对于 Redhat:


- name: change default gateway address on Redhat
      replace:
        path: /etc/sysconfig/network-scripts/ifcfg-eth0
        regexp: '(GATEWAY=[\d]*\.[\d]*.[\d]*)\.[\d]*$'
        replace: '\1.254'
        backup: yes
      when: (ansible_facts['distribution'] == "RedHat")
      register: redresult

然后我添加了一个任务来记录更改的主机:


    - name: log the changed hosts
      local_action:
        module : copy
        content: "{{ansible_facts.hostname}}{{debresult}}"
        content: "{{ansible_facts.hostname}}{{redresult}}"
        dest: /tmp/changed.txt

我想要的是这样的日志:

debianhostname {"msg": "", "success": true, "changed": true}
redhathostname {"msg": "", "success": true, "changed": true}

但问题是我只得到了最后一个输出,那么如何才能在我的文件中同时包含这两个条目呢?有没有我可以使用的模块?我尝试过使用 copy、set_fact 的 local_action,但没有效果。

答案1

以下是使用最大数量的 ansible 模块得出的答案:

 - name: log the changed hosts
      local_action:
        module : lineinfile
        line: "{{ ansible_facts['hostname'] }} {{item}}"
        dest: /tmp/changed.txt
      with_items:
        - "{{debresult}}"
        - "{{redresult}}"

相关内容