add_host and --limit

585 views
Skip to first unread message

Piotr Owcarz

unread,
Jun 4, 2019, 8:58:40 AM6/4/19
to ansible...@googlegroups.com
Hello All

I have the following inventory:
...
[group1]
host1

[group2]
host2

[all]
host1
host2
...

and a playbook:

- name: play1
  hosts: group1
  tasks:
    - add_host:
        name: "{{groups[group2] | first}}"
        groups: temp_group
    - debug: var=groups.temp_group

- name: play2
  hosts: temp_group
  tasks:
    - debug: msg="yay"

My intention is to execute a play on host1, where I read a host on which next play is going to be executed, and execute the second play on that host, but when I execute the playbook with --limit group1 even though the host2 is added to temp_group, ansible won't match any hosts to run the second play on. The output (writing from top of my head) :

$ ansible-playbook playbook.yml --limit group1
PLAY [play1] ***************
TASK [add_host] ************
ok: [host1]
TASK [debug] ***************
ok: [host1] => {
  "groups.temp_group" : [
     "host2"
  ]
}
PLAY [play2] ***************
skipping: no hosts matched

Is there any way to make the add_host work with --limit?

Thanks
Piotr


Kai Stian Olstad

unread,
Jun 5, 2019, 3:12:47 AM6/5/19
to ansible...@googlegroups.com
The whole point with limit is to say, only run on this/these host(s), so
no unless you add that host to the limit statement.

Since you only have one host in group1 limit is not needed.

--
Kai Stian Olstad

Knut Karevoll

unread,
Jun 5, 2019, 3:58:17 AM6/5/19
to Ansible Project
Firstly you should have a look at the delegate_to which might be a better way at achieving what you want. But I am assuming there is a good reason for your current approach. The reason it does not work is that the limit is still in place after you have done an add_host. So when it gets to the second play it see that there are no hosts both in temp_group and group1 and will therefore not do anything. One solution is to add temp_group to your limit. So you would run "ansible-playbook playbook.yml --limit group1:temp_group".

Piotr Owcarz

unread,
Jun 5, 2019, 3:59:10 AM6/5/19
to ansible...@googlegroups.com
Thanks Kai,
I solved this with delegate_to, fortunately it was only a few simple tasks to execute on host2.
Out of curiousity, where does Ansible runtime keep the set of hosts which are the subject of a playbook, after being --limit-ed? Is it accesible from inside a playbook? Would a custom module have the access there?

Piotr

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3f2542fd49b0223306379344ae6356f8%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Jun 5, 2019, 4:43:08 AM6/5/19
to ansible...@googlegroups.com
On 05.06.2019 09:58, Piotr Owcarz wrote:
> Out of curiousity, where does Ansible runtime keep the set of hosts
> which
> are the subject of a playbook, after being --limit-ed?

Ansible has a list of special variables and the one that gives you that
information is ansible_play_hosts_all.

For all of them check this page
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages