回答:delegate_to组无法正常工作

本地

在我的dockerized Ansible 2.8中,我尝试更改已使用以下方式动态添加到清单中的远程主机上的ssh设置 add_host

playbook.yml

# configure new VMs
- name: Configure new Azure VM
  hosts: localhost
  connection: local
  gather_facts: no
  roles:
    - az-vm-configure
  tags:
    - az-vm-configure

main.yml

- name: Configure inventory
  include: inventory.yml

- name: Configure sshd
  include: sshd.yml
  delegate_to: '{{ groups.new[0] }}'

当我使用以下构造时,它可以正常工作:delegate_to: '{{ groups.new[0] }}'但是,当我尝试为组中的所有主机实现该功能时:

delegate_to: '{{ item }}'
with_items: "{{ groups['new'] }}"

我的任务忽略了上面的构造,并尝试在本地主机上执行任务:任务执行结果

似乎delegate_to: '{{ item }}'在这种情况下不起作用。有人可以建议任何解决方法吗?

本地

当我如上所述尝试放置hostsmain.yml中时,出现错误,ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.这是我的解决方案:

playbook.yml

# configure VM
- name: Add new VM to inventory
  hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - include_role:
        name: az-vm-configure
        tasks_from: inventory.yml
  tags:
    - az-vm-configure

- name: Configure new Azure VM
  hosts: new
  gather_facts: no
  tasks:
    - include_role:
        name: az-vm-configure
        tasks_from: sshd.yml
  tags:
    - az-vm-configure

角色/az-vm-configure/tasks/main.yml

- include_tasks: '{{ tasks }}'
  with_items:
    - inventory.yml
    - sshd.yml
  loop_control:
    loop_var: tasks

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章