Include vars from a file for multiple import_playbooks

北慕城南 提交于 2021-01-28 06:50:30

问题


This is an error using Ansible 2.7

I am trying to include a files with vars in it into my playbook that has multiple import_playbooks.

I have 3 files.

  1. one with all the vars
  2. one with a playbook and a task
  3. one with an import_playbook

My 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 }}"

The playbook where I will call my tasks and my variables:

---
- name: master
  hosts: localhost

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

I get this error when running "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

Is there a way to call a file with variables for only this import_playbook.

thank you


回答1:


include_vars and import_playbook are tasks. The correct syntax is below.

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

There is no isolation of variables. Scope of the variables is the playbook. Hence, to answer the question

Is there a way to call a file with variables for only this import_playbook?

No. It is not. The included variables are visible in the whole playbook.




回答2:


The import_playbook module doesn't accept - include_vars as an argument.

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

For calling include_vars specific you can use the include_vars module inside that playbook



来源:https://stackoverflow.com/questions/56023040/include-vars-from-a-file-for-multiple-import-playbooks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!