How do I change hosts when including tasks from another playbook? I’ve tried both methods I could find below, but neither seems to work:
`
name: example
hosts: localhost
tasks:
name: execute this task
include: tasks/task1.yml
hosts: “{{ name }}.example.com”
`
`
name: example
hosts: localhost
tasks:
name: execute this task
include: tasks/task1.yml hosts={{ name }}.example.com
`
-Tristan
Brian_Coca
(Brian Coca)
September 24, 2016, 12:48am
2
you don’t, you create a new play that targets those hosts:
I need to be able to do this from a single playbook. What if I create a task that is a shell command that calls into ansible-playbook on a different playbook?
-Tristan
Brian_Coca
(Brian Coca)
September 24, 2016, 3:28am
4
A playbook can have multiple plays:
hosts: host_for_play_1
tasks:
debug: msg=‘play 1 task’
…
- hosts: host_for_play_2
tasks:
debug: msg='play 2 task’
…
I understand that, but it seems that each play can’t have a different hosts set. That is, I would like the first play to have “localhost” as the hosts and the second play to have hosts dynamically set to whatever the first play says it is.
-Tristan
This it possible to do, see example below.
Using your example (and replacing ‘a1’ with the host I care about) doesn’t appear to work. This is what I see:
PLAY [test] *******************************************************************
skipping: no hosts matched
When I directly copy/paste the hostname into hosts in the second play, it works as expected.
-Tristan
It works for me with Ansible 2.1.1.
Maybe the issue is that I’m using 1.9.1-1
I was able to finally resolve my issue by doing the below:
I do this by setting the hosts line in a playbook to a variable. I’ve been working with virtual machines a lot lately, so the variable I use is vms.
What will be the command to pass username and password for both the host considering none of the host is localhost.