【问题标题】:Ansible playbook - does include_role and delegate_to work togetherAnsible playbook - include_role 和 delegate_to 是否一起工作
【发布时间】:2020-07-23 10:28:49
【问题描述】:

include_role 和 delegate_to 在 Ansible 2.9 中是否一起工作,因为我试图通过运行一个角色并将其委派给主机 2(下面的代码)来执行以下剧本?

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]

我收到的错误信息如下:

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

我们可能错了,但这个看起来可能是一个问题 缺少引号。总是引用模板表达式括号 开始一个值。例如:

with_items:
  - {{ foo }}

应该写成:

with_items:
  - "{{ foo }}"

任何想法为什么会失败?

【问题讨论】:

    标签: ansible


    【解决方案1】:

    “错误!冲突的操作语句: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]
    

    未测试

    还有其他怪人:

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

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

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

    【讨论】:

    • 谢谢弗拉基米尔,我想要实现的是重新启动游戏中的主机 1,然后 include_role validate_rtr_state 和 delegate_to 主机 2,但即使我使用 delegate_to: host2 而不是 apply delegate_to,它也会失败。跨度>
    • 太复杂了。每个问题将其拆分为一个问题,并使其成为mcve!请把它最小化,把问题抽象化,尽可能简单! (例如,input_interfaces 的 set_fact 与问题无关。)帮助我们帮助您!
    • 感谢弗拉基米尔,我已经尽可能简单地剥离了剧本并将其粘贴在下面。基本上我正在尝试运行一个名为 validate_rtr_state 的 include_role 任务,并从该角色运行一个 ping 任务,并且 delegate_to 仅在 host2 上运行。
    • 这是精简后的游戏--名称:顶级剧本主机:[“host1”,“host2”]连接:本地收集事实:错误忽略错误:无任务:-set_fact:all_hosts:“{{ ansible_play_hosts }}” - 名称:调用 validate_rtr_state 角色并委托给 host2 include_role:名称:validate_rtr_state tasks_from:cisco-ios-xr_ping.yml delegate_to:“{{all_hosts[1]}}”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 2020-10-11
    • 1970-01-01
    相关资源
    最近更新 更多