问题原文
原帖链接 · Dafonz · CC BY-SA 3.0
I can't figure out variables and I'm hoping some kind person can point me in the right direction.
I have three files:
group_vars/all--- env2: serverhosts[all:vars] env=development [webservers] 138.78.334.247site.yml--- - name: Provision an {{ env }} {{ env2 }} db server hosts: all become: yes become_user: root vars: - update_apt_cache: yes
When I run the command:
ansible-playbook -i development site.yml
My output shows:
PLAY [Provision an {{ env }} {{ env2 }} db server] *****************************
Instead of replacing the vars.
I've also tried calling site.yml from another YAML file using include:, but that didn't work either.
I can't seem to figure out what I'm doing wrong here since it seems straightforward from the docs.
I'm running Ansible 2.2.1.
The above is a simplified example of what I'm trying to accomplish which is below:
---
- name: Provision a {{ application_name }} web server
hosts: all
become: yes
become_user: root
remote_user: "{{ server_user }}"
vars:
- update_apt_cache: yes
vars_files:
- env_vars/{{ env }}.yml
roles:
- base
- db
- mysql
- web
- nginx
社区高分解答
原帖链接 · techraf · CC BY-SA 3.0
It's pretty complicated as there are several aspects involved. But it's easy to check with trial-and-error.
The answer
You can use variables in play and task names as long as all the variables used in the name are defined and available at a specific level.
In your example both the env and env2 are group variables and they do not get substituted at the play level.
When printing the play name in the output log, Ansible does not yet know which hosts it will run on, therefore it also does not know what values are defined for these hosts (and the value might differ depending on the host).
However, at the task level the values are unanimously defined.
Consider:
---
- name: Here variables do not work {{ env }} {{ env2 }}
hosts: localhost
gather_facts: false
tasks:
- name: Here variables work {{ env }} {{ env2 }}
debug:
Will produce:
PLAY [Here variables do not work {{ env }} {{ env2 }}] *************************
TASK [Here variables work development server] **********************************
ok: [localhost] => {
"msg": "Hello world!"
}
Regarding the last example (Provision a {{ application_name }} web server):
I don't know where you got it from, but it seems incomplete. A very similar one here contains a static reference to the variable file:
vars_files:
- env_vars/base.yml
The value of application_name defined in the env_vars/base.yml is thus unanimously rendered in the play name.
You, on the other hand, tried to use group vars instead of a static value and that's why the values do not get substituted (they are not yet determined).
Additional catch
If you added a variable which is not defined to the task name from the above example, which worked otherwise, for example:
- name: Here variables work {{ env }} {{ env2 }} {{ unknown }}
debug:
You would get:
TASK [These variables will not work {{ env }} {{ env2 }} {{ unknown }}] ********
Even the variables that are properly defined did not get substituted.
Similarly trying to use a host-scoped fact, for example ansible_hostname will also fail:
tasks:
- name: These variables will not work {{ env }} {{ env2 }} {{ ansible_hostname }}
debug:
You would get:
TASK [These variables will not work {{ env }} {{ env2 }} {{ ansible_hostname }}] ***
And your example is like the last one:
You try to print the group variables in a play name, i.e. on a level on which group variables are not yet defined.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
