ansible delegate_to 运行多次

slo*_*ang 1 local ansible

我正在尝试使用delegate_to localhostconnection: local在远程主机上运行某些任务和其他任务。但是,当我使用“delegate_to: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"
Run Code Online (Sandbox Code Playgroud)

我的任务:

- name: host name
  shell: echo `hostname` >> /tmp/123
  #delegate_to: localhost
  #connection: local
Run Code Online (Sandbox Code Playgroud)

如果我的评论delegate_to: localhostconnection: local,我会用它里面自己的主机名的每个远程主机上得到一个文件/ tmp / 123。预期结果。

但是,如果我取消注释其中任何一个,该任务将在本地主机上执行 6 次。这意味着 localhost 上的 /tmp/ls 将在其中打印 6 次 localhost 的主机名。

我的目标很简单,我只想按照 playbook 中的定义在所有主机上运行某些任务hosts: groupa:groupb,并在 localhost上运行某些任务,但仅执行 1 次。我认为这是直截了当的,但我一直在花费数小时但没有结果。

sad*_*k-f 5

如果您hosts包含groupa:groupb那么是有意义的有 6 个条目(它在本地主机上运行任务 6 次),您需要run_once: true在任务级别添加该选项。或修改剧本以仅在本地主机上运行。