Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (2.3k points)

So this is how my plabook looks :

---

- hosts: all

  tasks:

    - command: echo {{ item }}

      with_items: [ itemforhost1, itemforhost2]

this is what i get as the output :

TASK [command] *****************************************************************

changed: [host1] => (item=itemforhost1)

changed: [host2] => (item=itemforhost1)

changed: [host1] => (item=itemforhost2)

changed: [host2] => (item=itemforhost2)

ALl of the hosts are reading the same line, I only want my host1 to read itemforhost1 and same for host2. 

How do you think i can go about doing this? I want my output to be something like this:

TASK [command] *****************************************************************

changed: [host1] => (item=itemforhost1)

changed: [host2] => (item=itemforhost2)

2 Answers

0 votes
by (41.4k points)

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 }}

If you are interested to learn DevOps, you can sign up for this DevOps Training Course by Intellipaat provides instructor-led training, hands-on projects, certification, and job assistance.

0 votes
by (6.9k points)

Try this out. Make 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 }}

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...