包含多个 import_playbook 文件中的变量

luc*_*mon 2 yaml ansible

这是使用 Ansible 2.7 时出现的错误

我正在尝试将其中包含变量的文件包含到我的具有多个 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 }}"
Run Code Online (Sandbox Code Playgroud)

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

---
- name: master
  hosts: localhost

- import_playbook: create_CPG.yml
   include_vars: properties/variables.yml
Run Code Online (Sandbox Code Playgroud)

运行“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
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以仅调用包含此 import_playbook.txt 变量的文件。

谢谢

Vla*_*tka 9

(Ansible 2.12 于 2022 年 6 月 14 日更新)

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

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

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

- import_playbook: create_CPG.yml
Run Code Online (Sandbox Code Playgroud)

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

- import_playbook: create_CPG.yml
  vars:
    var1: value of var1
Run Code Online (Sandbox Code Playgroud)

但是,您目前无法声明vars_files 。下面的导入

- import_playbook: create_CPG.yml
  vars_files:
    - properties/variables.yml
Run Code Online (Sandbox Code Playgroud)

将失败并出现以下错误:

错误!“vars_files”不是 PlaybookInclude 的有效属性

请参阅 Ansible 问题使用 import_playbook 时支持 vars_files #36806。实施后这将解决您的问题。


解决方法

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

shell> cat hosts
host1 var1=1
host2 var1=2
Run Code Online (Sandbox Code Playgroud)

, 播放的文件

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') }}
Run Code Online (Sandbox Code Playgroud)

,剧本

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
Run Code Online (Sandbox Code Playgroud)

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

shell> cat vars_file_default.yml
var2: play default
Run Code Online (Sandbox Code Playgroud)
shell> cat vars_file_play_A.yml
var2: play A
Run Code Online (Sandbox Code Playgroud)
shell> cat vars_file_play_B.yml
var2: play B
Run Code Online (Sandbox Code Playgroud)

给出

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
Run Code Online (Sandbox Code Playgroud)

变量的范围

变量的范围更加复杂。请参阅确定变量范围。主要作用域分为三个:global、play 和 host。例如,给定库存

shell> cat hosts
host1 var1=1
host2 var1=2
host3 var1=3
Run Code Online (Sandbox Code Playgroud)

, 文件

shell> cat my_vars.yml 
var4: included in a task
Run Code Online (Sandbox Code Playgroud)

,以及剧本

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') }}
Run Code Online (Sandbox Code Playgroud)

给出以下结果:

  • 主办范围。inventory 中声明的变量var1在整个 playbook 中可供主机使用

  • Play范围:第一个play中声明的变量var2仅适用于第一个play中的所有主机。该变量在第二次播放中未定义。

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

  • - 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
Run Code Online (Sandbox Code Playgroud)

如果导入 playbook,结果是相同的。例如,给定文件

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') }}
Run Code Online (Sandbox Code Playgroud)

以及添加到之前剧本中的导入内容

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

- import_playbook: pb-import.yml
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)