gpt4 book ai didi

Ansible playbook - include_role 和 delegate_to 一起工作

转载 作者:行者123 更新时间:2023-12-04 09:24:29 24 4
gpt4 key购买 nike

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 }}"
任何想法为什么会失败?

最佳答案

"ERROR! conflicting action statements: 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]
未测试
还有其他奇葩:
  • loop_var是第二位主持人的名字吗?即角色真的将第二台主机的名称作为变量实现吗?
  • 剧中有2位主持人,都是connection: local ?
  • 为什么apply delegate_to: xy而不是 delegate_to: xy任务?
  • 关于Ansible playbook - include_role 和 delegate_to 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63043177/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com