6
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansible小手先テクニック - inventory で配列を宣言する

Last updated at Posted at 2017-01-23

Ansible の inventory の記述は

var = 'a'

のように、iniファイル形式でしか記述できず、配列やハッシュの表現が単純にはできない。
from_json を使って以下のようにすればできる。

inventory.
[all:vars]
var_array_json = '["A", "B", "C"]'
var_array = '{{var_array_json | from_json}}'
var_hash_json = '{ "a": 1, "b": 2 }'
var_hash = '{{var_hash_json | from_json}}'

変数を確認する Playbook を適当に用意して、

playbook.yml
- hosts: localhost
  tasks:
    - debug:
        var: var_array
    - debug:
        var: var_hash

実行してみる。

$ ansible-playbook -i inventory playbook.yml

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

TASK [setup] *******************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "var_array": [
        "A", 
        "B", 
        "C"
    ]
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "var_hash": {
        "a": 1, 
        "b": 2
    }
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0   

期待値通りの結果を得られる。

でも、あまり使うことはないとは思う。
json 記述はちょっと見ずらいし。

6
9
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
6
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?