stopping different hosts from reading the same value when usign ansible in loop

0 votes

I've got this playbook:

---
- hosts: all
  tasks:
    - command: echo {{ item }}
      with_items: [ itemforhost1, itemforhost2]

And this is the output:

TASK [command] *****************************************************************
changed: [host1] => (item=itemforhost1)
changed: [host2] => (item=itemforhost1)
changed: [host1] => (item=itemforhost2)
changed: [host2] => (item=itemforhost2)

Each host is reading the same line, but I want host1 to only read itemforhost1 and same for host2. How should I go about doing this? I want something like this:

TASK [command] *****************************************************************
changed: [host1] => (item=itemforhost1)
changed: [host2] => (item=itemforhost2)
Jul 27, 2018 in Ansible by Damon Salvatore
• 5,980 points
518 views

1 answer to this question.

0 votes

Try using this. Create a new directory host_vars in your ansible directory. create two file host1.yml and host2.yml

host1.yml:

---
echo_value: itemforhost1

host2.yml:

---
echo_value: itemforhost2

and your playbook should look something like this:

---
- hosts: all
  tasks:
    - command: echo {{ echo_value }}
answered Jul 27, 2018 by DareDev
• 6,890 points

Related Questions In Ansible

0 votes
2 answers

How do you stop Ansible from creating .retry files in the home directory?

[defaults] ... retry_files_enabled = True # Create them ...READ MORE

answered Apr 5, 2019 in Ansible by anonymous
2,938 views
0 votes
1 answer

How do I wget a file from web server using shell in the ansible playbook

Hey Ayaan, you could probably use this ...READ MORE

answered Jan 24, 2019 in Ansible by Anushri
3,867 views
0 votes
1 answer
0 votes
1 answer

What are the different types of plugins available in Ansible?

Ansible provides a huge variety of plugins ...READ MORE

answered Feb 19, 2019 in Ansible by Priya
687 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,520 views
+2 votes
1 answer
0 votes
1 answer

Using multiple config files using same template in a role in ansible

Go through this include_role module: tasks: - ...READ MORE

answered Jun 5, 2018 in Ansible by DareDev
• 6,890 points
4,009 views
+1 vote
1 answer

Ansible: Deleting matching IPs from hosts file using regex

Ansible notation isn't the same as YAML ...READ MORE

answered Jul 25, 2018 in Ansible by DareDev
• 6,890 points
2,031 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP