在这种情况下,请确保host\u vars相对于playbook
我的测试。yml公司
:
user> find roles/mytest
roles/mytest
roles/mytest/tasks
roles/mytest/tasks/main.yml
user> cat roles/mytest/tasks/main.yml
---
- name: test myvar
shell: echo "{{ myvar }}" > /tmp/myvar
user> cat hosts2
[test]
10.102.32.42
user> cat host_vars/10.102.32.42
myvar: "this is 10.102.32.42"
user> cat mytest.yml
---
- hosts: test
roles:
- mytest
user> ansible-playbook --inventory-file=hosts2 mytest.yml
PLAY [test] *******************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************
ok: [10.0.0.4]
TASK [mytest : test myvar] ****************************************************************************************************
changed: [10.102.32.42]
PLAY RECAP ********************************************************************************************************************
10.102.32.42 : ok=2 changed=1 unreachable=0 failed=0
user> ssh 10.102.32.42 "cat /tmp/myvar"
this is 10.102.32.42
请注意
group\u变量
,则,
host\u变量
文件夹与某些角色文件夹无关。考虑到这些文件夹中设置的任何变量可能是针对多个角色的通用变量,或者它们可能与特定主机相关,
非特定角色
提到
Ansible Best Practices
;:
production # inventory file for production servers
staging # inventory file for staging environment
group_vars/
group1 # here we assign variables to particular groups
group2 # ""
host_vars/
hostname1 # if systems need specific variables, put them here
hostname2 # ""
library/ # if any custom modules, put them here (optional)
module_utils/ # if any custom module_utils to support modules, put them here (optional)
filter_plugins/ # if any custom filter plugins, put them here (optional)
site.yml # master playbook
webservers.yml # playbook for webserver tier
dbservers.yml # playbook for dbserver tier
roles/
common/ # this hierarchy represents a "role"
tasks/ #
main.yml # <-- tasks file can include smaller files if warranted
handlers/ #
main.yml # <-- handlers file
templates/ # <-- files for use with the template resource
ntp.conf.j2 # <------- templates end in .j2
files/ #