3
1

More than 3 years have passed since last update.

Ansible Tips: key部分に変数を使用したdictionaryを作成する

Last updated at Posted at 2020-08-27

備忘

課題: key部分に変数を使用したdictionaryを作成したい

例えば以下の様なplaybookにより、"FOO": "BAR"を得たい。

- hosts: localhost
  gather_facts: false
  vars:
    foo: FOO
    bar: BAR
  tasks:
    - debug:
        msg:
          '{{ foo }}': '{{ bar }}'

しかし実行するとキーには変数を設定できず、意図通りにはならない。

$ ansible-playbook -i localhost, site.yml

PLAY [localhost] ****************************************************************************************************

TASK [debug] ********************************************************************************************************
ok: [localhost] => {
    "msg": {
        "{{ foo }}": "BAR"
    }
}

PLAY RECAP **********************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

対応(NEW):

以下の様に、シンプルにYAMLフロースタイルで記述すれば良い。

---
- hosts: localhost
  gather_facts: false
  vars:
    foo: FOO
    bar: BAR
  tasks:
  - debug:
      msg: "{{ { foo: bar } }}"

実行結果

 $ ansible-playbook -i localhost, -c local a.yml

PLAY [localhost] ****************************************************************************************************

TASK [debug] ********************************************************************************************************
ok: [localhost] => {
    "msg": {
        "FOO": "BAR"
    }
}

PLAY RECAP **********************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

以下backup(当初の記述)

対応: items2dictフィルター

(他にもっと良い方法があるとは思うのですが、)とりあえずitems2dictフィルターを用いる事で回避可能。

- hosts: localhost
  gather_facts: false
  vars:
    foo: FOO
    bar: BAR
  tasks:
    - debug:
        msg: '{{ baz|items2dict }}'
      vars:
        baz:
          - key: '{{ foo }}'
            value: '{{ bar }}'
    - debug:
        msg: "{{ [{'key': foo, 'value': bar}]|items2dict }}"
$ ansible-playbook -i localhost, site.yml

PLAY [localhost] ****************************************************************************************************

TASK [debug] ********************************************************************************************************
ok: [localhost] => {
    "msg": {
        "FOO": "BAR"
    }
}

TASK [debug] ********************************************************************************************************
ok: [localhost] => {
    "msg": {
        "FOO": "BAR"
    }
}

PLAY RECAP **********************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

参考

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1