0

In my playbook I have two major tasks one task that change the gateway last digit for Debian machines and the second one applies to Redhat machines everything works fine but to be able to trace the machines on which the tasks were executed succesfully on a local file (kind of a log).

I tried to register the results of both tasks with a register

for 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

for 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

and then I added a task to log the changed hosts :


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

What I am trying to have is a log like this :

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

But the problem is that I only get the last output so how can have both entries in my file? is there a module that I can use ? I have tried the local_action with copy, set_fact but it did not work.

1 Answer 1

0

here's the answer using a max of ansible modules :

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .