Chapter 12

変数の参照方法

y_mrok
y_mrok
2021.10.17に更新

参照の基本ルール

変数を参照するときの基本ルールです。

  • 変数を {{}} でくくって参照する
  • パラメーターに設定する値に変数を使用するなど : の後に変数を使用する場合は "{{}}" でくくる ( ダブルクォーテーションマークの代わりにシングルクォーテーションマークを使用してもよい)

参照方法

カレントディレクトリー内の「 textfile.txt 」ファイルを管理対象ノード oshikoji の「 /tmp 」ディレクトリーへコピーするプレイブックを例に、変数の参照方法を確認します。

プレイで使用するインベントリーファイルです。

hosts.yml
---
all:
  hosts:
    marutamachi:
  children:
    web:
      hosts:
        takeyamachi:
        ebisugawa:
        nijyo:
    mail:
      hosts:
        nijyo:
    database:
      hosts:
        oshikoji:
        oike:

プレイを実行する前の管理対象ノード oshikoji の「/tmp」ディレクトリーの内容です。

y_mrok@ctrl:~/code/exam5$ ansible oshikoji -i hosts.yml -a 'ls -l /tmp/'
oshikoji | CHANGED | rc=0 >>
total 8
drwx------ 2 vagrant vagrant 4096 Sep  4 08:53 ansible_ansible.legacy.command_payload_6sb_rs0y
drwx------ 3 root    root    4096 Sep  4 08:00 systemd-private-d5f50fed0f2e42bb98a2a0a428281eae-chrony.service-2q4R7I
y_mrok@ctrl:~/code/exam5$ 

マッピングの場合

「参照の基本ルール」に従って参照します。

filecopy1.yml
---
- name: Example of variable reference
  hosts: oshikoji
  gather_facts: no

  vars:
    source_path: ./textfile.txt         # {{ source_path }}
    destination_path: /tmp/             # {{ destination_path }}

  tasks:
    - name: Copying a file from a control node to a managed node
      ansible.builtin.copy:
        src: "{{ source_path }}"
        dest: "{{ destination_path }}"
    - name: View source and destination
      ansible.builtin.debug:
        msg: "コピー元のファイルは  {{ source_path }} です。コピー先は {{ destination_path }} です。"

プレイブックの実行ログとプレイを実行した後の管理対象ノード oshikoji の「/tmp」ディレクトリーの内容です。

y_mrok@ctrl:~/code/exam5$ ansible-playbook -i hosts.yml filecopy1.yml 

PLAY [Example of variable reference] *****************************************************************************************************************

TASK [Copying a file from a control node to a managed node] ******************************************************************************************
changed: [oshikoji]

TASK [View source and destination] *******************************************************************************************************************
ok: [oshikoji] => {
    "msg": "コピー元のファイルは  ./textfile.txt です。コピー先は /tmp/ です。"
}

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

y_mrok@ctrl:~/code/exam5$ ansible oshikoji -i hosts.yml -a 'ls -l /tmp/'
oshikoji | CHANGED | rc=0 >>
total 12
drwx------ 2 vagrant vagrant 4096 Sep  4 09:03 ansible_ansible.legacy.command_payload_1gxf0tzq
drwx------ 3 root    root    4096 Sep  4 08:00 systemd-private-d5f50fed0f2e42bb98a2a0a428281eae-chrony.service-2q4R7I
-rw-r--r-- 1 vagrant vagrant   43 Sep  4 09:03 textfile.txt
y_mrok@ctrl:~/code/exam5$ 

マッピングをネストした場合

「参照の基本ルール」に次のルールが追加になります。

  • マッピングの下位の変数を [''] でくくって参照する

今回、上位の変数は file_path 、下位の変数は source_path と destination_path です。

filecopy2.yml
---
- name: Example of variable reference
  hosts: oshikoji
  gather_facts: no

  vars:
    file_path:
      source_path: ./textfile.txt       # file_path['source_path']
      destination_path: /tmp/           # file_path['destination_path']

  tasks:
    - name: Copying a file from a control node to a managed node
      ansible.builtin.copy:
        src: "{{ file_path['source_path'] }}"
        dest: "{{ file_path['destination_path'] }}"
    - name: View source and destination
      ansible.builtin.debug:
        msg: "コピー元のファイルは  {{ file_path['source_path'] }} です。コピー先は {{ file_path['destination_path'] }} です。"

プレイブックの実行ログとプレイを実行した後の管理対象ノード oshikoji の「/tmp」ディレクトリーの内容です。

y_mrok@ctrl:~/code/exam5$ ansible-playbook -i hosts.yml filecopy2.yml 

PLAY [Example of variable reference] *****************************************************************************************************************

TASK [Copying a file from a control node to a managed node] ******************************************************************************************
changed: [oshikoji]

TASK [View source and destination] *******************************************************************************************************************
ok: [oshikoji] => {
    "msg": "コピー元のファイルは  ./textfile.txt です。コピー先は /tmp/ です。"
}

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

y_mrok@ctrl:~/code/exam5$ ansible oshikoji -i hosts.yml -a 'ls -l /tmp/'
oshikoji | CHANGED | rc=0 >>
total 12
drwx------ 2 vagrant vagrant 4096 Sep  4 09:11 ansible_ansible.legacy.command_payload_cc8l3vl0
drwx------ 3 root    root    4096 Sep  4 08:00 systemd-private-d5f50fed0f2e42bb98a2a0a428281eae-chrony.service-2q4R7I
-rw-r--r-- 1 vagrant vagrant   43 Sep  4 09:11 textfile.txt
y_mrok@ctrl:~/code/exam5$ 

マッピングの中にシーケンスをネストした場合

「参照の基本ルール」に次のルールが追加になります。

  • 要素番号を [] でくくって参照する

要素番号はシーケンスの定義順に 0 , 1 , 2 , ・・・ ( ゼロオリジン ) です。

filecopy3.yml
---
- name: Example of variable reference
  hosts: oshikoji
  gather_facts: no

  vars:
    file_path:
      - ./textfile.txt                  # file_path[0]
      - /tmp/                           # file_path[1]

  tasks:
    - name: Copying a file from a control node to a managed node
      ansible.builtin.copy:
        src: "{{ file_path[0] }}"
        dest: "{{ file_path[1] }}"
    - name: View source and destination
      ansible.builtin.debug:
        msg: "コピー元のファイルは  {{ file_path[0] }} です。コピー先は {{ file_path[1] }} です。"

プレイブックの実行ログとプレイを実行した後の管理対象ノード oshikoji の「/tmp」ディレクトリーの内容です。

_mrok@ctrl:~/code/exam5$ ansible-playbook -i hosts.yml filecopy3.yml 

PLAY [Example of variable reference] *****************************************************************************************************************

TASK [Copying a file from a control node to a managed node] ******************************************************************************************
changed: [oshikoji]

TASK [View source and destination] *******************************************************************************************************************
ok: [oshikoji] => {
    "msg": "コピー元のファイルは  ./textfile.txt です。コピー先は /tmp/ です。"
}

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

y_mrok@ctrl:~/code/exam5$ ansible oshikoji -i hosts.yml -a 'ls -l /tmp/'
oshikoji | CHANGED | rc=0 >>
total 12
drwx------ 2 vagrant vagrant 4096 Sep  4 11:17 ansible_ansible.legacy.command_payload_dkglqnel
drwx------ 3 root    root    4096 Sep  4 08:00 systemd-private-d5f50fed0f2e42bb98a2a0a428281eae-chrony.service-2q4R7I
-rw-r--r-- 1 vagrant vagrant   43 Sep  4 11:17 textfile.txt
y_mrok@ctrl:~/code/exam5$ 

ファクト変数の参照方法

「基本の参照ルール」および「参照方法」と同じですが、次のルールがあります。

  • 最上位の変数名は ansible_facts
  • 下位の変数の指定時はプレフィックス ansible_ を省略する

参照例

主なファクト変数の値を確認します。

viewfacts.yml
---
- name: Display the Fact variable.
  hosts: oshikoji

  tasks:
    - name: Displays the distribution name.
      ansible.builtin.debug:
        msg: "Distribution name -> {{ ansible_facts['distribution'] }}"
    - name: Displays the distribution name.
      ansible.builtin.debug:
        msg: "OS family -> {{ ansible_facts['os_family'] }}"
    - name: Displays the IPv4 address
      ansible.builtin.debug:
        msg: "IPv4 address -> {{ item }}"
      loop: "{{ ansible_facts['all_ipv4_addresses'] }}"
    - name: Displays hostname.
      ansible.builtin.debug:
        msg: "hostname -> {{ ansible_facts['hostname'] }}"
    - name: Displays fqdn.
      ansible.builtin.debug:
        msg: "hostname -> {{ ansible_facts['fqdn'] }}"

実行ログです。

y_mrok@ctrl:~/code/exam5$ ansible-playbook -i hosts.yml viewfacts.yml 

PLAY [Display the Fact variable.] ********************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************
ok: [oshikoji]

TASK [Displays the distribution name.] ***************************************************************************************************************
ok: [oshikoji] => {
    "msg": "Distribution name -> Debian"
}

TASK [Displays the distribution name.] ***************************************************************************************************************
ok: [oshikoji] => {
    "msg": "OS family -> Debian"
}

TASK [Displays the IPv4 address] *********************************************************************************************************************
ok: [oshikoji] => (item=192.168.111.105) => {
    "msg": "IPv4 address -> 192.168.111.105"
}
ok: [oshikoji] => (item=10.0.2.15) => {
    "msg": "IPv4 address -> 10.0.2.15"
}

TASK [Displays hostname.] ****************************************************************************************************************************
ok: [oshikoji] => {
    "msg": "hostname -> oshikoji"
}

TASK [Displays fqdn.] ********************************************************************************************************************************
ok: [oshikoji] => {
    "msg": "hostname -> oshikoji.example.jp"
}

PLAY RECAP *******************************************************************************************************************************************
oshikoji                   : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

y_mrok@ctrl:~/code/exam5$ 

演習問題

演習問題はこのリンクをクリックしてください。