how to use include_vars in ansible

孤街醉人 提交于 2019-11-30 01:37:15

问题


I have created my own custom library, I added my custom library in the common folder of my repository. In that I need to pass variables dynamically. It's a confidential password, so I am using "vault" in ansible.

In that my requirement is how to pass include_vars in the tasks\main.yml before hosts.

e.g: mytasks.yml

- include_vars: sample_vault.yml
- include: sample_tasks.yml
- hosts: localhost
  tasks:
    name: "free task"
    command: ls -a

my directory structure like this:

myfolder
  - common 
      -library   
         -my file.py
      - sample_tasks.yml

  - mytasks
      -mytasks.yml(my main master playbook file)
      -sample_vault.yml  (note:i create this using vault for confidential purpose)
      - roles
        -myrole

Here I need to run sample_tasks file using a variables passed in sample_vault.yml file before I execute the hosts tasks using ansible. If I use extra variable means password is visible so I don't need that.

When I use include_vars in my tasks/main.yml file, it shows the following error:

ERROR! 'include_vars' is not a valid attribute for a Play


回答1:


You can't use include_vars this way, it's only available for use under tasks.
If sample_tasks.yml is a list of tasks, you also can't use it on playbook level. See my other answer for explanation.

You can use vars_files like this:

- hosts: localhost
  vars_files:
    - sample_vault.yml
  tasks:
    name: "free task"
    command: ls -a

Or pass a file as extra variables:

ansible-playbook --ask-vault-pass -e @sample_vault.yml myplaybook.yml


来源:https://stackoverflow.com/questions/39805949/how-to-use-include-vars-in-ansible

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