【发布时间】:2020-10-15 03:49:11
【问题描述】:
我正在尝试使用delegate_to localhost 或connection: local 运行某些任务以及远程主机上的其他任务。但是,当我使用“delegate_to: localhost”时,任务会在 localhost 上多次执行
我的库存
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"
我的任务:
- name: host name
shell: echo `hostname` >> /tmp/123
#delegate_to: localhost
#connection: local
如果我评论delegate_to: localhost 和connection: local,我将在每个远程主机上获得一个文件/tmp/123,其中包含自己的主机名。预期结果。
但是,如果我取消注释其中任何一个,该任务将在 localhost 上执行 6 次。这意味着 localhost 上的 /tmp/ls 将在其中打印 6 次 localhost 的主机名。
我的目标很简单,我只想按照 playbook hosts: groupa:groupb 中的定义在所有主机上运行某些任务,并在 localhost 上运行某些任务,但只运行 1 次。我认为这是直截了当的,但我已经花了几个小时但没有结果。
【问题讨论】:
-
试试
run_once: True