0

Sorry to put again a bockinfile issue, but regarding the cases before, i don't think, this is similar to them. How can i use key/value pair, to return a unique key/value in the output file for each host. Using the playbook mentioned bellow, it loops the key/value and returns just the same key/value in all outputs

- hosts: all
  gather_facts: yes
  become: yes
  tasks:
     - blockinfile:
           create: yes
           path: /root/hardware_report
           block: |
             hostname: "{{inventory_hostname}}"
             total_mem: "{{ansible_memtotal_mb}}"
             bios_version: "{{ansible_bios_version}}"
             device_size: "{{ansible_devices.nvme0n1.size | default ('NONE')}}"
             device_size: "{{ansible_devices.nvme1n1.size | default ('NONE')}}"
             "{{item.key}}: {{item.value}}" # (the line main of my issue)
       with_dict: {a: 1, b: 2, c: 3, d: 4}

Expected_outputs: host1:

# BEGIN ANSIBLE MANAGED BLOCK
hostname: "node6"
total_mem: "966"
bios_version: "1.0"
vda_size: "20.00 GB"
vdb_size: "2.00 GB"
"b: 2"
# END ANSIBLE MANAGED BLOCK

host2:

# BEGIN ANSIBLE MANAGED BLOCK
hostname: "node6"
total_mem: "966"
bios_version: "1.0"
vda_size: "20.00 GB"
vdb_size: "2.00 GB"
"d: 4"
# END ANSIBLE MANAGED BLOCK
yelmir
  • 13
  • 4

2 Answers2

0

For example

- hosts: test_11,test_12,test_13,test_14
  vars:
    dict: {a: 1, b: 2, c: 3, d: 4}
    list: "{{ dict|dict2items }}"
  tasks:
    - blockinfile:
        create: yes
        path: /root/hardware_report
        block: |
          hostname: {{ inventory_hostname }}
          total_mem: {{ ansible_memtotal_mb }}
          bios_version: {{ ansible_bios_version }}
          {% set index = ansible_play_hosts_all.index(inventory_hostname) %}
          "{{ list[index].key }}: {{ list[index].value }}"
      become: yes

gives

shell> root@test_11:/home/admin # cat /root/hardware_report 
# BEGIN ANSIBLE MANAGED BLOCK
hostname: test_11
total_mem: 3915
bios_version: NA
"a: 1"
# END ANSIBLE MANAGED BLOCK

shell> root@test_12:/home/admin # cat /root/hardware_report 
# BEGIN ANSIBLE MANAGED BLOCK
hostname: test_12
total_mem: 3915
bios_version: NA
"b: 2"
# END ANSIBLE MANAGED BLOCK

...
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • yeah, it works fine, exists it another way ?? i m trying to use jinja2 with variable for example {{item|random|unique}} – yelmir Dec 26 '20 at 14:22
  • (There is no iteration. What `item` are you talking about?). 1) Make the question [mre], please! It's not reproducible atm. 2) If you have another issue open a new question. – Vladimir Botka Dec 26 '20 at 14:27
0

is it valid syntax if i use the syntax with jinja2 as mentioned in that example bellow ?

  vars:
     keys:
        a:1
        b:2  
        c:3
        d:4
  tasks:
     - blockinfile:
           create: yes
           path: /root/hwreport.txt
           block: |
             hostname: "{{inventory_hostname}}"
             total_mem: "{{ansible_memtotal_mb}}"
             bios_version: "{{ansible_bios_version}}"
             "{{keys|random|unique}}"
yelmir
  • 13
  • 4