Jinja_dude
Jinja_dude

Reputation: 529

Ansible customizing the output of a result before writing it in the log file

I am writing logs into a local file via a task that captures the result of further tasks in my playbook.

I am trying to log only the parts in which the task perfom the wanted action and not the skipped parts

here's the logs task :

 - name: log the changed hosts
   shell: echo "{{ item }}" >> /tmp/changed.txt
   with_items: 
      - "{{ansible_facts.hostname}}{{debresult}}"
      - "{{ansible_facts.hostname}}{{redresult}}"
   delegate_to: "localhost"

and here is an one of the two tasks that i want to log :


 - name: change default gateway address
      replace:
        path: /etc/network/interfaces
        regexp: '(up route add default gw [\d]*\.[\d]*.[\d]*)\.[\d]*$'
        replace: '\1.252'
        backup: yes
      register: debresult

So far the log looks like this :

test-vipgw-redhat{'skip_reason': u'Conditional result was False', 'skipped': True, 'changed': False}
test-vipgw-debian{u'msg': u'1 replacements made', 'failed': False, u'changed': True, u'backup_file': u'/etc/network/interfaces.23603.2021-02-07@17:52:48~'}
test-vipgw-debian{'skip_reason': u'Conditional result was False', 'skipped': True, 'changed': False}
test-vipgw-redhat{u'msg': u'1 replacements made', 'failed': False, u'changed': True, u'backup_file': u'/etc/sysconfig/network-scripts/ifcfg-eth0.23282.2021-02-07@17:52:37~'}

But what i want to have should look like this :

test-vipgw-debian{u'msg': u'1 replacements made', 'failed': False, u'changed': True, u'backup_file': u'/etc/network/interfaces.23603.2021-02-07@17:52:48~'}

test-vipgw-redhat{u'msg': u'1 replacements made', 'failed': False, u'changed': True, u'backup_file': u'/etc/sysconfig/network-scripts/ifcfg-eth0.23282.2021-02-07@17:52:37~'}

I tried to add this sed to my echo to get rid of the skipped tasks but it is not working :

shell: echo "{{ item }}" |  sed -nr '/^test/{h;n;/^skipping:/{n;b};H;x};p' >> /tmp/changed.txt

I also tried to use callback and skippy but still the result output keeps the skipped parts.

I tried to use the stdout also but still the result is the same I even have a dict error :

TASK [debug] *********************************************************************************************************** 
fatal: [test-vipgw-debian]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to be in '/home/rezzaamari/gateway_change.yml': line 14, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: debresult\n - debug:\n ^ here\n"}

So my question is is there a way in ansible or a module that would help me have the needed result? all advices hints or sed alternatives are welcome.

Upvotes: 0

Views: 245

Answers (1)

Jinja_dude
Jinja_dude

Reputation: 529

Well I managed to get what I want just by adding a condition to my :

- name: log the changed hosts
      local_action:
        module: lineinfile
        line: "{{item}}"
        dest: /tmp/changed_logs_test.log
      with_items:
        - "{{ansible_facts.hostname}}{{redresult}}"
      when: redresult.changed

this way the log will write only when the the changes are done.

Upvotes: 0

Related Questions