端くれプログラマの備忘録 Ansible [Ansible] ディクショナリとリスト

[Ansible] ディクショナリとリスト

ディクショナリ

# test04.yml
---
- hosts: all
  vars:
    user:
      name: "James"
      email: "james@example.com"
      age: 21
  tasks:
    - name: debug
      debug:
        var: user
    - name: debug
      debug:
        msg: "User's name is {{ user['name'] }}"
    - name: debug
      debug:
        msg: "User's email is {{ user.email }}"
$ ansible-playbook test04.yml -i localhost, -e ansible_connection=local  
TASK [debug] ********************
ok: [localhost] => {
    "user": {
        "age": 21,
        "email": "james@example.com",
        "name": "James"
    }
}

TASK [debug] ******************
ok: [localhost] => {
    "msg": "User's name is James"
}

TASK [debug] ********************
ok: [localhost] => {
    "msg": "User's email is james@example.com"
}

リスト

# test05.yml
---
- hosts: all
  vars:
    list_example:
      - "item1"
      - "item2"
      - "item3"
  tasks:
    - name: debug
      debug:
        msg: "{{ list_example[1] }}"
$ ansible-playbook test05.yml -i localhost, -e ansible_connection=local  
TASK [debug] *****
ok: [localhost] => {
    "msg": "item2"
}

参考サイト

Ansible で扱う変数 – 赤帽エンジニアブログ
https://rheb.hatenablog.com/entry/ansible_variables
2022-04-07