Greetings all,
Recently I created a Playbook to extract selected information from our CMDB so nothing special there. Having finessed it a little I found that the lineinfile module was taking a very long time to write out 585 records to the file -- 260 seconds to be precise. The offending task is detailed below:
- name: 'Generate CMDB extract file'
ansible.builtin.lineinfile:
line: "{{ item['NETWORK_NAME'] }}\t{{ item['ISTATUS'] }}"
path: /var/tmp/CMDB.txt
create: true
insertafter: EOF
loop: "{{ fact_CIDetails['CIDetails']['ConfigurationItem'] }}"
loop_control:
label: "{{ item['NETWORK_NAME']}}"
This got me thinking and after a little head scratching I created a Jinja2 template (CMDB.j2), below, to replace the loop:
{# Used by CMDB_Extract_CI_Details.yml #}
{% for CIDetails in fact_CIDetails['CIDetails']['ConfigurationItem'] %}
{{ CIDetails['NETWORK_NAME'] | lower }} {{ CIDetails['ISTATUS'] }}
{% endfor %}
I then re-defined the task per the below:
- name: 'Generate CMDB extract file'
ansible.builtin.template:
src: templates/CMDB.j2
dest: /var/tmp/CMDB.txt
owner: aixadmin
group: aixadmin
mode: '0777'
The moment of truth had arrived, would there be much of a performance improvement?
The redefined task, using the Jinja2 template, created the extract in 1.34 seconds!! To be honest I had to re-run things a few more times before I believed what I was seeing -- a reduction from 260 to 1.34 seconds, absolutely amazing.
Hope this might be of some help.
Steve
------------------------------
Steve Munday
AIX, IBM i, HMC, PowerVM
------------------------------