Request for jinja2 template.

34 views
Skip to first unread message

Melvin Satheesan

unread,
May 30, 2021, 5:51:55 AM5/30/21
to Ansible Project
Hello All,
Good day.

I am practicing the Ansible. A small play book has been created in order to collect few command out put and format it from a bunch of servers. The commands and the template is working as expected. When we run it to multiple hosts, the command output is superimposing the file.ie. the command output is collecting in the ansible tower. When we run it in multiple server, the last server is superimposing the output so that only one server is present in the list. Would you be so kind enough to help me.

Playbook.

---
- name: adhoc
  hosts: all
  serial: 1
  gather_facts: true
  tasks:
    - name: manufacture
      shell: dmidecode -t1 | grep -i manufacture | awk '{print $2}'
      register: manufacture
    - debug: var=manufacture.stdout_lines[0]

    - name: vgs
      shell: vgs | grep -i vg00 | awk '{print $1"  "$6"  "$7}'
      register: vgs
    - debug: var=vgs.stdout_lines[0]

    - name: df
      shell: df -hP /| awk '{print $1" "$2"  "$4}'
      register: df
    - debug: var=df.stdout_lines[1]

    - name: hostname
      shell: uname -a | awk '{print $2}'
      register: hostname
    - debug: var=hostname.stdout_lines[0]

    - name: Create server-info.txt using Jinja2
      template:
       src: jinja.j2
       dest: /tmp/server-info.txt
      delegate_to: localhost

Jinja2

==================================================================================================================

{{ "%-5s" | format("NO") }} {{ "%-25s" | format("HOSTNAME") }} {{ "%-20s" | format("MANUFACTURE") }} {{ "%-42s" | format("ROOT_VG:name: total: free") }} {{ "%-30s" | format("ROOT_FS:total:free") }}

==================================================================================================================
{% for hosts in groups.all %}

{{ "%-5s" | format(loop.index) }} {{ "%-25s" | format(ansible_facts['hostname']) }} {{ "%-25s" | format(manufacture.stdout_lines[0]) }}  {{ "%-36s" | format(vgs.stdout_lines[0]) }} {{ "%-33s" | format(df.stdout_lines[1]) }}

{% endfor %}

Sample O/P.

==================================================================================================================

NO    HOSTNAME                  MANUFACTURE          ROOT_VG:name: total: free                  ROOT_FS:total:free

==================================================================================================================

1     ansible_tower_client_1    VMware,                    vg00  <60.00g  <40.00g               /dev/sda3 96G  94G


Reply all
Reply to author
Forward
0 new messages