'Create jinja template from dictionary in ansible
how can I access stdout and item values in a dictionary?
My inventory file:
all:
hosts:
server1:
dict:
custom_mountpoints:
- /srv/sftp
- /opt
server2:
dict:
custom_mountpoints:
- /srv/sftp
- /opt
My ansible task:
- name: Genarate OIDs for custom inventroy variables
become: yes
shell: 'snmptranslate -On NET-SNMP-EXTEND-MIB::nsExtendOutput1Line.\"{{ item[1] }}-{{ item[0] }}\"'
loop: "{{ dict.custom_mountpoints | product(snmp_mountpoints_extends) | list }}"
register: custom_mountpoints_output
when:
- dict is defined
- name: print output from custom_mountpoints_output
debug: msg={{ custom_mountpoints_output }}
Output from custom_mountpoints_output variable:
ok: [server01] =>
msg:
changed: true
msg: All items completed
results:
- ansible_loop_var: item
changed: true
cmd: snmptranslate -On NET-SNMP-EXTEND-MIB::nsExtendOutput1Line.\"folder-size-/srv/sftp\"
delta: '0:00:00.015394'
end: '2022-04-28 11:40:29.130234'
failed: false
invocation:
module_args:
_raw_params: snmptranslate -On NET-SNMP-EXTEND-MIB::nsExtendOutput1Line.\"folder-size-/srv/sftp\"
_uses_shell: true
argv: null
chdir: null
creates: null
executable: null
removes: null
stdin: null
stdin_add_newline: true
strip_empty_ends: true
warn: false
item:
- /srv/sftp
- folder-size
rc: 0
start: '2022-04-28 11:40:29.114840'
stderr: ''
stderr_lines: []
stdout: .1.3.6.1.4.1.8072.1.3.2.3.1.1.21.102.111.108.100.101.114.45.115.105.122.101.45.47.115.114.118.47.115.102.116.112
stdout_lines:
- .1.3.6.1.4.1.8072.1.3.2.3.1.1.21.102.111.108.100.101.114.45.115.105.122.101.45.47.115.114.118.47.115.102.116.112
etc. for each snmp_mountpoints_extends
then I try to access only stdout value:
- name: print output from custom_mountpoints_output
debug:
msg: "{{ item.stdout }}"
loop: "{{ custom_mountpoints_output.results }}"
register: output
become the following output:
ok: [server1] => (item={'cmd': 'snmptranslate -On NET-SNMP-EXTEND-MIB::nsExtendOutput1Line.\\"folder-size-/srv/sftp\\"', 'stdout': '.1.3.6.1.4.1.8072.1
.3.2.3.1.1.21.102.111.108.100.101.114.45.115.105.122.101.45.47.115.114.118.47.115.102.116.112', 'stderr': '', 'rc': 0, 'start': '2022-04-28 11:40:29.114840', 'end': '2022-04-28 11:40:29.130234', 'delta': '0:00:00.015394', 'changed': True, 'invocation': {'module_args': {'_raw_params': 'snmptranslate -On NET-SNMP-EXTEND-MIB::nsExtendOutput1Line.\\"folder-size-/srv/sftp\\"', '_uses_shell': True, 'warn': False, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['.1.3.6.1.4.1.8072.1.3.2.3.1.1.21.102.111.108.100.101.114.45.115.105.122.101.45.47.115.114.118.47.115.102.116.112'], 'stderr_lines': [], 'failed': False, 'item': ['/srv/sftp', 'folder-size'], 'ansible_loop_var': 'item'}) =>
msg: .1.3.6.1.4.1.8072.1.3.2.3.1.1.21.102.111.108.100.101.114.45.115.105.122.101.45.47.115.114.118.47.115.102.116.112
ok: [server1] => (item={'cmd': 'snmptranslate -On NET-SNMP-EXTEND-MIB::nsExtendOutput1Line.\\"folder-avail-/srv/sftp\\"', 'stdout': '.1.3.6.1.4.1.8072.1.3.2.3.1.1.22.102.111.108.100.101.114.45.97.118.97.105.108.45.47.115.114.118.47.115.102.116.112', 'stderr': '', 'rc': 0, 'start': '2022-04-28 11:40:29.605138', 'end': '2022-04-28 11:40:29.620156', 'delta': '0:00:00.015018', 'changed': True, 'invocation': {'module_args': {'_raw_params': 'snmptranslate -On NET-SNMP-EXTEND-MIB::nsExtendOutput1Line.\\"folder-avail-/srv/sftp\\"', '_uses_shell': True, 'warn': False, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['.1.3.6.1.4.1.8072.1.3.2.3.1.1.22.102.111.108.100.101.114.45.97.118.97.105.108.45.47.115.114.118.47.115.102.116.112'], 'stderr_lines': [], 'failed': False, 'item': ['/srv/sftp', 'folder-avail'], 'ansible_loop_var': 'item'}) =>
msg: .1.3.6.1.4.1.8072.1.3.2.3.1.1.22.102.111.108.100.101.114.45.97.118.97.105.108.45.47.115.114.118.47.115.102.116.112
etc.
But i would like to create a jinja template that write for me the following output in the file:
Hostname from a inventory file
/srv/sftp\
folder-size
extend: value of stdout (.1.3.6.1.4.1.8072.1.3.2.3.1.1.21.102.111.108.100.101.114.45.115.105.122.101.45.47.115.114.118.47.115.102.116.112)
I try with following code:
{% for i in groups['all'] %}
Servername: {{ hostvars[i].inventory_hostname }}
{% for dict_item in output %}
{% for item in dict_item.values() %}
{{ item.value.msg }}
{% endfor %}
{% endfor %}
{% for item in output.values() %}
Extend: {{ output[item].stdout }}
{% endfor %}
{% endfor %}
But it didn't work. Any idea how can i implement this with ansible and jinja?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|