gpt4 book ai didi

ansible - 为多个 import_playbook 包含文件中的变量

转载 作者:行者123 更新时间:2023-12-05 09:13:34 27 4
gpt4 key购买 nike

这是使用 Ansible 2.7 的错误

我正在尝试将一个带有 vars 的文件包含到我的具有多个 import_playbook 的剧本中。

我有 3 个文件。

  1. 包含所有变量
  2. 有剧本和任务的人
  3. 一个有 import_playbook 的

我的剧本:

---
- name: Create CPG
hosts: localhost

tasks:
- name: Create CPG "{{ cpg_name }}"
hpe3par_cpg:
storage_system_ip: "{{ storage_system_ip }}"
storage_system_username: "{{ storage_system_username }}"
storage_system_password: "{{ storage_system_password }}"
state: present
cpg_name: "{{ cpg_name }}"
#domain: "{{ domain }}"
growth_increment: "{{ growth_increment }}"
growth_increment_unit: "{{ growth_increment_unit }}"
growth_limit: "{{ growth_limit }}"
growth_limit_unit: "{{ growth_limit_unit }}"
growth_warning: "{{ growth_warning }}"
growth_warning_unit: "{{ growth_warning_unit }}"
raid_type: "{{ raid_type }}"
set_size: "{{ set_size }}"
high_availability: "{{ high_availability }}"
disk_type: "{{ disk_type }}"

我将在其中调用任务和变量的剧本:

---
- name: master
hosts: localhost

- import_playbook: create_CPG.yml
include_vars: properties/variables.yml

运行“ansible-playbook create_master.yml”时出现此错误

ERROR! Syntax Error while loading YAML.
mapping values are not allowed in this context

The error appears to have been in '/home/simon/Documents/Ansible/create_MasterPlaybook.yml': line 6, column 16, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- import_playbook: create_CPG.yml
include_vars: properties/variables.yml
^ here

有没有办法只为这个 import_playbook 调用一个带有变量的文件。

谢谢

最佳答案

<支持>(2022 年 6 月 14 日更新 Ansible 2.12)

问:有没有办法只为这个 import_playbook 调用一个带有变量的文件?

A:不,不是。任务中包含的变量在整个剧本中对受影响的主机可见。 include_vars是一项任务。正确的语法是

- name: master
hosts: localhost
tasks:
- include_vars: properties/variables.yml

- import_playbook: create_CPG.yml

import_playbook不是任务。它是一个模块,“包含一个包含要执行的播放列表的文件”。您可以在导入的剧本范围内声明vars。例如,

- import_playbook: create_CPG.yml
vars:
var1: value of var1

但是,您现在不能声明vars_files。下面导入

- import_playbook: create_CPG.yml
vars_files:
- properties/variables.yml

将因错误而失败:

ERROR! 'vars_files' is not a valid attribute for a PlaybookInclude

参见 Ansible 问题 Support vars_files when using import_playbook #36806 .这将在实现时解决您的问题。


解决方法

将文件名放入变量中并使用vars。例如,给定库存

shell> cat hosts
host1 var1=1
host2 var1=2

,播放文件

shell> cat pb-import.yml
- hosts: host1,host2
gather_facts: false
vars_files:
- "{{ my_vars_file|default('vars_file_default.yml') }}"
tasks:
- debug:
msg: |-
var1: {{ var1|d('undef') }}
var2: {{ var2|d('undef') }}

,剧本

shell> cat pb.yml
- import_playbook: pb-import.yml

- import_playbook: pb-import.yml
vars:
my_vars_file: vars_file_play_A.yml

- import_playbook: pb-import.yml
vars:
my_vars_file: vars_file_play_B.yml

,以及包含各种剧本变量的文件

shell> cat vars_file_default.yml
var2: play default
shell> cat vars_file_play_A.yml
var2: play A
shell> cat vars_file_play_B.yml
var2: play B

给予

shell> ansible-playbook pb.yml

PLAY [host1,host2] ***************************************************************************

TASK [debug] *********************************************************************************
ok: [host1] =>
msg: |-
var1: 1
var2: play default
ok: [host2] =>
msg: |-
var1: 2
var2: play default

PLAY [host1,host2] ***************************************************************************

TASK [debug] *********************************************************************************
ok: [host1] =>
msg: |-
var1: 1
var2: play A
ok: [host2] =>
msg: |-
var1: 2
var2: play A

PLAY [host1,host2] ***************************************************************************

TASK [debug] *********************************************************************************
ok: [host1] =>
msg: |-
var1: 1
var2: play B
ok: [host2] =>
msg: |-
var1: 2
var2: play B

PLAY RECAP ***********************************************************************************
host1: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host2: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

<支持>

变量范围

变量的范围更复杂。参见 Scoping variables .主要有三个作用域:global、play 和 host。例如,给定库存

shell> cat hosts
host1 var1=1
host2 var1=2
host3 var1=3

, 文件

shell> cat my_vars.yml 
var4: included in a task

和剧本

shell> cat pb.yml
- hosts: host1,host2
gather_facts: false
vars:
var2: play
tasks:
- include_vars: my_vars.yml
- debug:
msg: |-
var1: {{ var1|d('undef') }}
var2: {{ var2|d('undef') }}
var3: {{ var3|d('undef') }}
var4: {{ var4|d('undef') }}

- hosts: host1,host2,host3
gather_facts: false
tasks:
- debug:
msg: |-
var1: {{ var1|d('undef') }}
var2: {{ var2|d('undef') }}
var3: {{ var3|d('undef') }}
var4: {{ var4|d('undef') }}

给出如下结果:

  • 宿主范围。在 list 中声明的​​变量 var1 在整个 playbook 中对主机可用

  • 播放范围:在第一个播放中声明的变量var2 仅对第一个播放中的所有主机可用。第二部剧中变量未定义。

  • 全局范围。在命令行中声明为额外变量的变量 var3 对所有游戏中的所有主机全局可用。

  • -include_vars:my_vars.yml 中声明的变量 var4* 仅对受影响的主机可用,即第一个播放中的主机 host1,host2 .该变量未定义到 host3

shell> ansible-playbook pb.yml -e var3=global

PLAY [host1,host2] ***************************************************************************

TASK [include_vars] **************************************************************************
ok: [host1]
ok: [host2]

TASK [debug] *********************************************************************************
ok: [host1] =>
msg: |-
var1: 1
var2: play
var3: global
var4: included in a task
ok: [host2] =>
msg: |-
var1: 2
var2: play
var3: global
var4: included in a task

PLAY [host1,host2,host3] *********************************************************************

TASK [debug] *********************************************************************************
ok: [host1] =>
msg: |-
var1: 1
var2: undef
var3: global
var4: included in a task
ok: [host2] =>
msg: |-
var1: 2
var2: undef
var3: global
var4: included in a task
ok: [host3] =>
msg: |-
var1: 3
var2: undef
var3: global
var4: undef

PLAY RECAP ***********************************************************************************
host1: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host2: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host3: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

如果导入剧本,结果是一样的。例如,给定文件

shell> cat pb-import.yml
- hosts: host1,host2,host3
gather_facts: false
tasks:
- debug:
msg: |-
var1: {{ var1|d('undef') }}
var2: {{ var2|d('undef') }}
var3: {{ var3|d('undef') }}
var4: {{ var4|d('undef') }}

以及添加到上一个剧本的导入

- import_playbook: pb-import.yml
vars:
var2: play3

- import_playbook: pb-import.yml

给予

PLAY [host1,host2,host3] *********************************************************************

TASK [debug] *********************************************************************************
ok: [host1] =>
msg: |-
var1: 1
var2: play3
var3: global
var4: included in a task
ok: [host2] =>
msg: |-
var1: 2
var2: play3
var3: global
var4: included in a task
ok: [host3] =>
msg: |-
var1: 3
var2: play3
var3: global
var4: undef

PLAY [host1,host2,host3] *********************************************************************

TASK [debug] *********************************************************************************
ok: [host1] =>
msg: |-
var1: 1
var2: undef
var3: global
var4: included in a task
ok: [host2] =>
msg: |-
var1: 2
var2: undef
var3: global
var4: included in a task
ok: [host3] =>
msg: |-
var1: 3
var2: undef
var3: global
var4: undef

关于ansible - 为多个 import_playbook 包含文件中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56023040/

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