Ansible 2.7.9使用host_vars的问题(未定义的变量)

时间:2019-04-05 13:33:24

标签: variables ansible host

Ansible 2.7.9未使用host_vars

我已经设置了一个非常简单的设置,其中包含3个主机,主要用于测试目的。我有主持人:

- ansible1 (this is where I store the code)
- ansible2
- ansible3

我的库存:

[ansible@ansible1 ~]$ cat /etc/ansible/hosts
[common]
ansible1
ansible2
ansible3

我的cfg看起来像这样:

[ansible@ansible1 ~]$ cat /etc/ansible/ansible.cfg
[defaults]
roles_path = /etc/ansible/roles
inventory  = /etc/ansible/hosts
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
pipelining = True
control_path = /tmp/ansible-ssh-%%h-%%p-%%r
pipelining = False
[accelerate]
[selinux]
[colors]

我已经定义了一个名为common的主要剧本,它称为common:

[ansible@ansible1 ~]$ ls /etc/ansible/roles/
common  common.retry  common.yml
[ansible@ansible1 ~]$ cat /etc/ansible/roles/common.yml
--- # Playbook for webservers
- hosts: common
  roles:
    - common
[ansible@ansible1 ~]$

task / main.yml:

[ansible@ansible1 ~]$ cat /etc/ansible/roles/common/tasks/main.yml
- name: test ansible1
  lineinfile:
    dest: /tmp/ansible.txt
    create: yes
    line: "{{ myvar }}"
- name: set ansible2
  lineinfile:
    dest: /tmp/ansible2.txt
    create: yes
    line: "hi"
[ansible@ansible1 ~]$

[ansible@ansible1 ~]$ cat /etc/ansible/roles/common/vars/main.yml
copyright_msg: "Copyrighta 2019"
myvar: "value of myvar from common/vars"

然后我在/ etc / ansible / host_vars中放置了一些信息

[ansible@ansible1 ~]$ ls /etc/ansible/hosts_vars/
ansible2.yml
[ansible@ansible1 ~]$ cat /etc/ansible/hosts_vars/ansible2.yml
myvar: "myvar from host_vars"
[ansible@ansible1 ~]$

这对剧本非常有用:

[ansible@ansible1 ~]$ ansible-playbook /etc/ansible/roles/common.yml --limit ansible2

PLAY [common] ******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [ansible2]

TASK [common : test ansible1] **************************************************
changed: [ansible2]

TASK [common : set ansible2] ***************************************************
changed: [ansible2]

PLAY RECAP *********************************************************************
ansible2                   : ok=3    changed=2    unreachable=0    failed=0

我看到了包含myvar内容的文件:

[root@ansible2 ~]# cat /tmp/ansible.txt
value of myvar from common/vars
[root@ansible2 ~]#

但是后来我不明白为什么它不从/etc/ansible/hosts_vars/ansible2.yml中获取值,实际上,如果我从/etc/ansible/roles/common/vars/main.yml中注释掉它说未定义的变量:

[ansible@ansible1 ansible]$ cat /etc/ansible/roles/common/vars/main.yml
copyright_msg: "Copyrighta 2019"
myvar: "value of myvar from common/vars"

2 个答案:

答案 0 :(得分:0)

这与预期的一样,在执行剧本时main.yml将自动获得源代码。将此文件视为全局变量。

ansible2.yml之所以无法获得资源,是因为ansible希望您在执行时明确地获得该资源。

您可以将以下代码用于该(通用)代码。

---
- name: play
  hosts: "{{ hosts }}"
  tasks:
    - include_vars: "{{ hosts }}.yml"

触发->

ansible-playbook -i inventory --extra-vars "hosts=ansible2"

答案 1 :(得分:0)

Ansible将优先级用于vars中的值:

From least to most important
role defaults
inventory file or script group vars
inventory group_vars/all
playbook group_vars/all
inventory group_vars/*
playbook group_vars/*
inventory file or script host vars
inventory host_vars/*
playbook host_vars/*
host facts
play vars
play vars_prompt
play vars_files
role vars (defined in role/vars/main.yml)
block vars (only for tasks in block)
task vars (only for the task)
role (and include_role) params
include params
include_vars
set_facts / registered vars
extra vars (always win precedence)

因此最好不要使用角色/变量,因为它比host_vars优先,因此我应该改用具有较低优先级的角色/默认值。