in Technology by
How can looping be done over a list of hosts in a group, inside of a template?

1 Answer

0 votes
by

This can be done by accessing the “$groups” dictionary in the template, like so:

{% for host in groups['db_servers'] %}
{{ host }}
{% endfor %}

If we need to access facts also we need to make sure that the facts have been populated. For instance, a play that talks to db_servers:

- hosts: db_servers
tasks:
- debug: msg="Something to debug"

Now, this can be used within a template, like so:

{% for host in groups['db_servers'] %}
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
{% endfor %}.

Related questions

0 votes
    I asked a question yesterday and none of the answers are working so I decided to start a fresh one ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    It seems using getters inside templates causes Angular change detection to go into a loop (getter gets ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I can't solve this for almost four hours, and i can't find any helpful documentation for this ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    I need help on looping all my list and print my list name. Currently, I have a few raspberry pi types and windows PC ... ) print ("\n") Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    The value of the control variables can be decreased in the For …..Next loop with the help of the ___ statement in looping statement Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    ___________ hosts many add-on packages that can be used to extend the functionality of R. (a) CRAN (b) ... Started of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a List : Old_list =['orders_ce_deduped.01.csv','orders_ce_deduped.02.csv'] I need to get the 01 and 02 part ... = ['01','02'] Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    In which one of the following switching, there will not be any dedicated connection between two hosts? A. Circuit switching ... switching C. Both the options D. None of the above...
asked Jan 8, 2023 in Technology by JackTerrance
0 votes
    Which of the following class in Bootstrap is used to create a basic list group? .grouped-list .select-list .list-group .list-grouped...
asked Mar 2, 2021 in Technology by JackTerrance
0 votes
    Which of the following class in Bootstrap is used to create a basic list group? .grouped-list .select-list .list-group .list-grouped...
asked Feb 28, 2021 in Technology by JackTerrance
0 votes
    Why text inside / list is not affected (color of "item1", "item2" is not changed) by CSS applied ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    Why text inside / list is not affected (color of "item1", "item2" is not changed) by CSS applied ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    Why text inside / list is not affected (color of "item1", "item2" is not changed) by CSS applied ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have an ETL requirement like: I need to fetch around 20000 records from a table and process each ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
...