Search code examples
ansiblejinja2ansible-template

Ansible Looping over dynamic inventory using jinja template


Here is my Ansible playbook. It reads /etc/waagent.conf file and checks if variable AutoUpdate.Enabled=y or not.And it uses Jinja template to generate output.csv file.

ansibleuser@server:~/plays$ cat report_waagent_local.yaml
---
 - name: waagent auto update report
   hosts: localhost
   connection: ssh
   remote_user: ewxxxx
   become: true
   become_user: root
   gather_facts: true

   tasks:
    - name: "Ensure status of AutoUpdate.Enabled in /etc/waagent.conf"
      lineinfile:
         name: /etc/waagent.conf
         line: AutoUpdate.Enabled=y
         state: present
      check_mode: yes #means make no change , just check
      register: conf
      failed_when: (conf is changed) or (conf is failed)
      ignore_errors: yes
      # logic
      # if "conf.changed": false --> that mean AutoUpdate.Enabled=y
      # if "conf.changed": true  --> that means value is not set in file.
    - name: generate report
      template:
        src: report_waagent_local.j2
        dest: ./output.csv

ansibleuser@server:~/plays$

And here is Jinja Template.

ansibleuser@server:~/plays$ cat templates/report_waagent_local.j2
{% if conf.changed == false %}
  {{ ansible_host }} , AutoUpdate.Enabled=y
{% else %}
  {{ ansible_host }} , AutoUpdate.Enabled=n
{% endif %}
ansibleuser@server:~/plays$

It produces output.csv as expected.

127.0.0.1, AutoUpdate.Enabled=y

Now, I need to fetch similar reports for all the servers present in the Azure subscription.

I modified my playbook. Note: I am using dynamic inventory in azure, I have a group named "all_pls" on which I need to run a playbook.

ansibleuser@server:~/plays$ cat report_waagent.yaml
---
- name: "generate waagent report"
  hosts: all
  connection: ssh
  remote_user : ewxxxxx
  become: True
  become_user: root
  gather_facts: True
  tasks:
    - name: "Ensure status of AutoUpdate.Enabled in /etc/waagent.conf"
      lineinfile:
         name: /etc/waagent.conf
         line: AutoUpdate.Enabled=y
         state: present
      check_mode: yes #means make no change , just check
      register: conf
      failed_when: (conf is changed) or (conf is failed)
      ignore_errors: yes
      # if "conf.changed": false --> that mean AutoUpdate.Enabled=y
      # if "conf.changed": true  --> that means the value is not set in the file.
    - name: generate report
      template:
        src: report_waagent_local.j2
        dest: ./output.csv

ansibleuser@server:~/plays$

I am running my playbook and getting no issues.

But I am getting no output in output.csv.

ansible-playbook --limit all_pls report_waagent.yaml

I guess I need to loop over hosts in a group name and also check conf.changed in the Jinja template. Can someone help please?


Solution

  • I fixed the issue.

    - name: log conf.changed in output.csv using a template
        lineinfile:
          line: "{{ lookup('template', 'report_waagent_local.j2') }}"
          insertafter: EOF
          dest: output.csv
        delegate_to: localhost