Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buzztaiki/353a2c1363ed067a471752f3dda3e934 to your computer and use it in GitHub Desktop.
Save buzztaiki/353a2c1363ed067a471752f3dda3e934 to your computer and use it in GitHub Desktop.
include_tasks と import_tasks
$ ANSIBLE_STDOUT_CALLBACK=default ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *****************************************************************************************************************************************************************************
TASK [include_tasks] *************************************************************************************************************************************************************************
included: /home/taiki/src/github.com/smartoperation/cobit/infra/aws-playbook/test2.yml for localhost
TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "import_tasks は変数の上書きはできない"
}
TASK [show arg and arg2] *********************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "arg=hoge, arg2=this"
}
TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "import_tasks は変数の上書きはできないからこそ、二重評価もされない"
}
TASK [show arg and arg2] *********************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "arg=hoge, arg2=that"
}
TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "include_tasks なら変数の上書きができる"
}
TASK [include_tasks] *************************************************************************************************************************************************************************
included: /home/taiki/src/github.com/smartoperation/cobit/infra/aws-playbook/test3.yml for localhost
TASK [show arg and arg2] *********************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "arg=what?, arg2=is"
}
TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "include_tasks で同じ変数を使うと recursive loop detected in template string でエラーになる"
}
PLAY RECAP ***********************************************************************************************************************************************************************************
localhost : ok=9 changed=0 unreachable=0 failed=0
- hosts: localhost
connection: local
gather_facts: False
tasks:
- include_tasks: test2.yml
vars:
arg: hoge
- debug:
msg: import_tasks は変数の上書きはできない
- import_tasks: test3.yml
vars:
arg: what?
arg2: this
- debug:
msg: import_tasks は変数の上書きはできないからこそ、二重評価もされない
- import_tasks: test3.yml
vars:
arg: "{{ arg }}"
arg2: that
- debug:
msg: include_tasks なら変数の上書きができる
- include_tasks: test3.yml
vars:
arg: what?
arg2: is
- debug:
msg: include_tasks で同じ変数を使うと recursive loop detected in template string でエラーになる
# - include_tasks: test3.yml
# vars:
# arg: "{{ arg }}"
- name: show arg and arg2
debug:
msg: "arg={{ arg }}, arg2={{ arg2 }}"
@buzztaiki
Copy link
Author

基本的に import_tasks は変数を指定しないで使うのがよいのだと思う。静的に解決するという意味でも。
include_tasks で使う変数は可能なら role と同様に prefix 付きにするのが良いのでしょう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment