Ansible 剧本 - include_role 和 delegate_to 一起工作吗

bba*_*ann 4 ansible

当我尝试通过运行角色并将其委托给主机 2(下面的代码)来执行以下剧本时,include_role 和 delegate_to 是否可以在 Ansible 2.9 中协同工作?

Ansible 手册

- name: top level playbook
  hosts: ["host1", "host2"]
  connection: local
  gather_facts: true
  ignore_errors: no
  tasks:
    - set_fact:
        playbook_dir: /Users/OneDrive 
        validation_overall: 'pass'
        result: {}
        all_hosts: "{{ ansible_play_hosts }}"

    - name: import hostvars
      include_vars:
        dir: '{{ playbook_dir }}/test_env_vars/hostvars'
        files_matching: '{{ inventory_hostname }}.*'

    - name: initialise required input variables
      set_fact:
        input_interfaces: "{{ e_input_interfaces }}"  

    # delegate role to host2
    - name: "call validate_rtr_state role with host '{{ansible_hostname}}' for hosts in '{{ansible_play_hosts}}'"
      include_role:
        name: validate_rtr_state
        tasks_from: cisco-ios-xr_ping.yml
      apply:
        delegate_to: "{{all_hosts[1]}}"
      loop: "{{ansible_play_hosts}}"
      loop_control:
        loop_var: all_hosts[1]
Run Code Online (Sandbox Code Playgroud)

我收到的错误消息如下:

ERROR! conflicting action statements: apply, include_role

The error appears to be in '/home/bbann/Ansible-Networking/ha_failover_top_level_reload.yml': line 46, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    # delegate role to tusredrweca908
    - name: "call validate_rtr_state role with host '{{ansible_hostname}}' for hosts in '{{ansible_play_hosts}}'"
      ^ here
Run Code Online (Sandbox Code Playgroud)

我们可能是错的,但这看起来可能是缺少引号的问题。当模板表达式括号开始值时,始终引用它们。例如:

with_items:
  - {{ foo }}
Run Code Online (Sandbox Code Playgroud)

应写为:

with_items:
  - "{{ foo }}"
Run Code Online (Sandbox Code Playgroud)

有什么想法为什么会失败吗?

Vla*_*tka 6

问:“错误!冲突的操作语句:apply、include_role”

答: 的缩进apply是错误的。这是任务的一个参数include_role

# delegate role to host2
    - name: "call validate_rtr_state role with host '{{ ansible_hostname }}' for hosts in '{{ ansible_play_hosts }}'"
      include_role:
        name: validate_rtr_state
        tasks_from: cisco-ios-xr_ping.yml
        apply:
          delegate_to: "{{ all_hosts[1] }}"
      loop: "{{ ansible_play_hosts }}"
      loop_control:
        loop_var: all_hosts[1]
Run Code Online (Sandbox Code Playgroud)

(未测试)

还有其他怪人:

  1. loop_var是第二个主机的名字吗?即角色是否将第二台主机的名称实现为变量?

  2. 剧中是否有 2 位主持人都与connection: local

  3. 为什么apply delegate_to: xy不是delegate_to: xy任务?