首页 > 解决方案 > Ansible:delegate_to 组无法正常工作

问题描述

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

剧本.yml

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

主要的.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 }}'不起作用。有人可以提出任何解决方法吗?

标签: ansibleansible-inventory

解决方案


当我尝试如上所述放入 main.yml 时,我给出了一个错误 hosts无论如何ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path. 是我的解决方案:

剧本.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

推荐阅读