Skip to content

Instantly share code, notes, and snippets.

@buzztaiki
Last active February 10, 2020 01:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buzztaiki/0ef7e483ca077fa36347036fd0b069bd to your computer and use it in GitHub Desktop.
Save buzztaiki/0ef7e483ca077fa36347036fd0b069bd to your computer and use it in GitHub Desktop.
ansible の vars の謎が一つとけた

最初に思ったこと

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   

set_fact で set_fact 内の変数を参照

- 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   

vars のネスト

- 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,
        }

vars

- 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   

set_fact

- 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   

vars と set_fact

set_fact は vars には勝てる

- 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   

set_fact は extra_vars には勝てない

- 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   

優先順位

https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable

下にある方が優先順位が高い(強い)。

  • 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment