vars の中のテンプレートは vars の辞書を参照する。
だから、vars で定義したのと同じ名前の変数を使うと recursive loop detected in template string
で怒られる。
playbook.yml:
- hosts: localhost
connection: local
tasks:
- debug:
var: x
vars:
x: "{{ y }}"
y: 10
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]
TASK [debug] *******************************************************************
ok: [127.0.0.1] => {
"x": "10"
}
PLAY RECAP *********************************************************************
127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0
- hosts: localhost
connection: local
tasks:
- set_fact:
x: "{{ y }}"
y: 10
- debug:
var: x
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [set_fact] ****************************************************************
fatal: [localhost]: FAILED! => {}
MSG:
The task includes an option with an undefined variable. The error was: 'y' is undefined
The error appears to have been in '/home/taiki/tmp/a.yml': line 4, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- set_fact:
^ here
to retry, use: --limit @/home/taiki/tmp/a.retry
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1
- hosts: localhost
connection: local
vars:
x: "{{ y }}"
y: 10
tasks:
- debug:
var: x
vars:
y: 20
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [debug] *******************************************************************
ok: [localhost] => {
"x": "20"
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
def debug(x):
print('debug: {}'.format(x))
return x;
class FilterModule(object):
def filters(self):
return {
'debug': debug,
}
- hosts: localhost
connection: local
vars:
x: "{{ y | debug }}"
y: 10
tasks:
- debug:
var: x
- debug:
var: x
vars:
y: 20
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]
TASK [debug] *******************************************************************
debug: 10
ok: [127.0.0.1] => {
"x": "10"
}
TASK [debug] *******************************************************************
debug: 20
ok: [127.0.0.1] => {
"x": "20"
}
PLAY RECAP *********************************************************************
127.0.0.1 : ok=3 changed=0 unreachable=0 failed=0
- hosts: localhost
connection: local
vars:
y: 10
tasks:
- set_fact:
x: "{{ y | debug }}"
- debug:
var: x
- debug:
var: x
vars:
y: 20
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]
TASK [set_fact] ****************************************************************
debug: 10
ok: [127.0.0.1]
TASK [debug] *******************************************************************
ok: [127.0.0.1] => {
"x": "10"
}
TASK [debug] *******************************************************************
ok: [127.0.0.1] => {
"x": "10"
}
PLAY RECAP *********************************************************************
127.0.0.1 : ok=4 changed=0 unreachable=0 failed=0
- hosts: localhost
connection: local
vars:
x: 10
tasks:
- set_fact:
x: 20
- debug:
var: x
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]
TASK [set_fact] ****************************************************************
ok: [127.0.0.1]
TASK [debug] *******************************************************************
ok: [127.0.0.1] => {
"x": 20
}
PLAY RECAP *********************************************************************
127.0.0.1 : ok=3 changed=0 unreachable=0 failed=0
- hosts: localhost
connection: local
vars:
x: 10
tasks:
- set_fact:
x: 20
- debug:
var: x
ansible-playbook -i hosts playbook.yml -e 'x=30'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]
TASK [set_fact] ****************************************************************
ok: [127.0.0.1]
TASK [debug] *******************************************************************
ok: [127.0.0.1] => {
"x": "30"
}
PLAY RECAP *********************************************************************
127.0.0.1 : ok=3 changed=0 unreachable=0 failed=0
下にある方が優先順位が高い(強い)。
- 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 / cached set_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)
- include_vars
- set_facts / registered vars
- role (and include_role) params
- include params
- extra vars (always win precedence)