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,510 points
44 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,810 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 in Ansible by anonymous
891 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 in Ansible by Priya
79 views
+13 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 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
264 views
0 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,810 points
840 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,810 points
387 views