In my playbook I have this:
- name: compile
hosts: localhost
gather_facts: false
tasks:
- name: compile binary
local_action: command make build FOO=foo1
I want to execute make build FOO=bar1
on localhost once if host is either bar-1
or bar-2
(they're both in group bars
, so distinguishing by group is fine too). I tried using when
:
- name: compile binary
local_action: command make build FOO=foo1
when: (inventory_hostname != "bar-1") and (inventory_hostname != "bar-2")
- name: compile binary
local_action: command make build FOO=bar1
when: (inventory_hostname == "bar-1") or (inventory_hostname == "bar-2")
But inventory_hostname
is always localhost
.
In my hosts I have
[foos]
foo-1 ...
foo-2 ...
[bars]
bar-1 ...
bar-2 ...
And I run it as
ansible-playbook -i provision/hosts -l localhost,bars provision/deploy.yml
This task will run a command on localhost once if the current host being operated on is part of the bars
group.
shell: echo {{ inventory_hostname }}
run_once: true
delegate_to: localhost
when: "'bars' in group_names"
Note: If you plan on using serial
mode it affects run_once
behaviour.
http://docs.ansible.com/ansible/playbooks_delegation.html#run-once
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments