Jinja Loops

  • Initial example
{% set count = 1 %}
{% for i in p %}
	{{ count }}
	{% set count = count + 1 %}
{% endfor %}
  • Ansible example to access each item within the output of a previous command:
- name: Gather Nodes Status
  ansible.builtin.command: oc get node {{ item }} -o=jsonpath='{@.metadata.name}{range @.status.conditions[*]}{@.status}{end}'
	changed_when: false
	loop: "{{ ocp_nodes.stdout_lines }}"
	register: ocp_nodes_status
{% set count = 0 %}
{% for i in ocp_nodes_status.results | map(attribute='stdout_lines') %}
	{% for j in ocp_nodes_status.results[count].stdout_lines %}
		{{ j }}
	{% endfor %}
	{% set count = count + 1 %}
{% endfor %}
Previous
Next