I am trying to run certain tasks using delegate_to localhost
or connection: local
and other tasks on the remote host. however the task is executed on the localhost multiple times when i use "delegate_to: localhost"
my inventory
localhost ansible_host=127.0.0.1 ansible_connection=local ansible_python_interpreter="{{ansible_playbook_python}}"
[master1]
ip-10-90-148-195.ap-southeast-1.compute.internal
[master2]
ip-10-90-149-130.ap-southeast-1.compute.internal
[master3]
ip-10-90-150-239.ap-southeast-1.compute.internal
[master:children]
master1
master2
master3
[worker]
ip-10-90-148-206.ap-southeast-1.compute.internal
ip-10-90-149-178.ap-southeast-1.compute.internal
ip-10-90-150-86.ap-southeast-1.compute.internal
[all:vars]
ansible_user="core"
ansible_ssh_private_key_file="~/.ssh/id_rsa"
my task:
- name: host name
shell: echo `hostname` >> /tmp/123
#delegate_to: localhost
#connection: local
if i comment delegate_to: localhost
and connection: local
, i will get a file /tmp/123 on each remote host with their own hostname inside it. expected result.
however if i uncomment either one of them,the task will be executed 6 times on the localhost. meaning /tmp/ls on localhost will have localhost's hostname printed 6 times in it.
my goal is simple, i just want to run certain task on all host as per define in playbook hosts: groupa:groupb
, and certain task on localhost, but 1 time only. i thought this is straight forward but i have been spending hours but no result.
run_once: True