Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

How do I loop over a list of hosts in a group, inside of a template?

Answer Posted / Baldev Singh

To loop over a list of hosts in a group and use that list within a template in Ansible, you can use the with_items loop module:n1. Define your group: In your inventory, define a group containing the hosts you want to loop through.nExample:ngroup:n - host1n - host2n - host3nn2. Use the with_items loop in a task: Inside a play or role, use the with_items loop module to loop over the hosts in your group and pass the current host to a template.nExample:n- name: Render a template for each hostn hosts: alln tasks:n - name: Loop through hosts and render templaten template: src=mytemplate.j2 dest=/etc/myconfig.confn with_items: '{{ groups.mygroup }}'

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the ansible modules?

3


What is the way to access shell environment variables in ansible?

3


How does ansible work?

3


What is the best way to make content reusable/redistributable?

3


How to interpolate variables or dynamic variable names?

3


Does ansible support aws?

3


Why is ‘{{ }}’ notation used? And how can one interpolate variables or dynamic variable names?

2


How do I access shell environment variables?

3


How to create encrypted files using ansible?

3


What are ansible server requirements?

4


Is ansible is an open source tool?

3


How do I see all the inventory vars defined for my host?

3


How do I access a variable of the first host in a group?

3


How can one interpolate variables or dynamic variable names?

3


Can you build your own modules with ansible?

3