简体   繁体   English

ansible include_vars订购问题

[英]ansible include_vars ordering issue

#file: cmservers.yml

 - hosts: cmservers
   tasks:
     - include_vars: /var/cm/local/ansible/vars/cmusers.yml
     - debug: var=users
   roles:
     - ansible-users-master

I am finding that the roles tasks are being executed first, however the users list needs to be defined first, which is not happening. 我发现角色任务首先被执行,但是需要首先定义用户列表,这不会发生。 Any help appreciated. 任何帮助赞赏。

Alternatively you can use vars_files directive in the playbook. 或者,您可以在playbook中使用vars_files指令。 This comes handy when you store your variables in a role vars/main.yml , but there are some additional variables, or some private data (you might use Ansible Vault for that) to include while executing playbook. 当您将变量存储在角色vars/main.yml ,这很方便,但是在执行playbook时还有一些其他变量或一些私有数据(您可以使用Ansible Vault)。

#file: cmservers.yml

 - hosts: cmservers
   vars_files:
     - /var/cm/local/ansible/vars/cmusers.yml
   roles:
     - ansible-users-master

Since include_vars is a task it's better to use it in roles/hanlders and use vars_files in playbooks instead. 由于include_vars是一项任务,因此最好在角色/ hanlders中使用它,而在playbooks中使用vars_files It is also easier to pass Ansible command line variables with vars_files . 使用vars_files传递Ansible命令行变量也更容易。 For further info see: http://docs.ansible.com/ansible/playbooks_variables.html 有关详细信息,请参阅: http//docs.ansible.com/ansible/playbooks_variables.html

You can use pre_tasks to make sure that some tasks are executed before role and post_tasks to make sure that some tasks are executed after the role is applied. 您可以使用pre_tasks确保在role和post_tasks之前执行某些任务,以确保在应用角色后执行某些任务。 So changing your tasks to pre_tasks will fix the problem. 因此,将您的tasks更改为pre_tasks将解决问题。

- hosts: cmservers

  pre_tasks:
    - include_vars: /var/cm/local/ansible/vars/cmusers.yml
    - debug: var=users

  roles:
    - ansible-users-master

You can check the role documentation for further details. 您可以查看角色文档以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM