Change hosts variable when including another playbook

3,477 views
Skip to first unread message

Tristan Muntsinger

unread,
Sep 23, 2016, 6:46:12 PM9/23/16
to Ansible Project
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

unread,
Sep 23, 2016, 8:48:57 PM9/23/16
to ansible...@googlegroups.com
you don't, you create a new play that targets those hosts:


--
----------
Brian Coca

Tristan Muntsinger

unread,
Sep 23, 2016, 8:58:23 PM9/23/16
to Ansible Project
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

unread,
Sep 23, 2016, 11:28:31 PM9/23/16
to ansible...@googlegroups.com
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'​
    ....


----------
Brian Coca

Tristan Muntsinger

unread,
Sep 26, 2016, 1:53:33 PM9/26/16
to Ansible Project
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

Kai Stian Olstad

unread,
Sep 26, 2016, 2:51:18 PM9/26/16
to ansible...@googlegroups.com
On 26. sep. 2016 19:53, Tristan Muntsinger wrote:
> 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.

This it possible to do, see example below.

---
- hosts: localhost
tasks:
- command: echo "a1"
register: result

- hosts: "{{ hostvars['localhost']['result']['stdout'] }}"
tasks:
- debug: var=play_host

The second play will run against host a1.

--
Kai Stian Olstad

Tristan Muntsinger

unread,
Sep 29, 2016, 8:30:44 PM9/29/16
to Ansible Project, ansible-pr...@olstad.com
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

Kai Stian Olstad

unread,
Sep 30, 2016, 1:07:21 PM9/30/16
to ansible...@googlegroups.com
On 30. sep. 2016 02:30, Tristan Muntsinger wrote:
> 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.

It works for me with Ansible 2.1.1.

---
- hosts: localhost
tasks:
- command: echo "a1"
register: result

- hosts: "{{ hostvars['localhost']['result']['stdout'] }}"
tasks:
- debug: var=play_hosts


$ ansible-playbook test.yml

PLAY [localhost] **********************************************

TASK [setup] **************************************************
ok: [localhost]

TASK [command] ************************************************
changed: [localhost]

PLAY [a1] *****************************************************

TASK [setup] **************************************************
ok: [a1]

TASK [debug] **************************************************
ok: [a1] => {
"play_hosts": [
"a1"
]
}

PLAY RECAP ***************************************************************
a1 : ok=2 changed=0 unreachable=0 failed=0
localhost : ok=2 changed=1 unreachable=0 failed=0

--
Kai Stian Olstad

Tristan Muntsinger

unread,
Sep 30, 2016, 8:38:23 PM9/30/16
to Ansible Project, ansible-pr...@olstad.com
Maybe the issue is that I'm using 1.9.1-1

I was able to finally resolve my issue by doing the below:

---
- hosts: localhost
  tasks:
    - command: echo "a1"
      register: result

    - add_host: name="{{ result.stdout }}" groups="new-play-hosts"

- hosts: "new-play-hosts"
  tasks:
    - local_action: debug var=play_hosts

-Tristan

Mike Schlottman

unread,
Oct 3, 2016, 9:14:39 PM10/3/16
to Ansible Project
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.

---
- hosts: "{{ vms }}"

Then when I call the playbook, I just pass the group or hosts that I want to run the playbook against.

ansible-playbook -i myhosts task1.yml -e "vms=dev-servers"

Aditya Garg

unread,
Apr 13, 2020, 3:10:51 AM4/13/20
to Ansible Project
What will be the command to pass username and password for both the host considering none of the host is localhost.
Reply all
Reply to author
Forward
0 new messages