Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of Jinja for loops in Ansible to print pretty output with the debug module.
PLAY [localhost] ****************************************************************************************************************************************************************************************************************************
TASK [Get top 5 /r/ansible posts] ***********************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Print the titles nicely] **************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
"1. Examples of complexity / power of Ansible?",
"2. Help Getting started with Ansible Tower",
"3. Joining dictionaries / lists is a HEADACHE! why?",
"4. Joining many lists together",
"5. append file using ansible"
]
}
TASK [Print the titles ugly] ****************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=c31hal) => {
"msg": "0. Examples of complexity / power of Ansible?"
}
ok: [localhost] => (item=c2uyc5) => {
"msg": "1. Help Getting started with Ansible Tower"
}
ok: [localhost] => (item=c2uh12) => {
"msg": "2. Joining dictionaries / lists is a HEADACHE! why?"
}
ok: [localhost] => (item=c2tnz6) => {
"msg": "3. Joining many lists together"
}
ok: [localhost] => (item=c2s68v) => {
"msg": "4. append file using ansible"
}
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
---
- hosts: localhost
gather_facts: no
tasks:
- name: Get top 5 /r/ansible posts
uri:
url: https://api.pushshift.io/reddit/search/submission/?subreddit=ansible&limit=5
register: reddit_posts
- name: Print the titles nicely
debug:
msg: "[{% for post in reddit_posts.json.data %}'{{ loop.index }}. {{ post.title }}',{% endfor %}]"
- name: Print the titles ugly
debug:
msg: "{{ index }}. {{ item.title }}"
with_items: "{{ reddit_posts.json.data }}"
loop_control:
index_var: index
label: "{{ item.id }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.