Chapter 30

プラグイン

y_mrok
y_mrok
2022.01.27に更新

プラグイン

プラグイン[1]を使用して Ansible を機能拡張できます。

主なプラグイン

lookup プラグイン

プレイブックから外部ソース ( ファイルやデータベース 等 ) にアクセスする機能を提供します。

構文

lookup('プラグイン名', 引数)

file

コントロールノード上のファイルの内容を読み込みます。

引数

引数 内容
ファイル名 読み取るファイル

使用例

samplefile.txt
taro,password@taro
jiro,password@jiro
hanako,password@hanako
hosts.yml
---
all:
  hosts:
    marutamachi:
      ansible_host: 192.168.111.101
      ansible_user: vagrant
      ansible_password: vagrant
lookup1.yml
---
- name: Example usage of the lookup plugin.
  hosts: all
  gather_facts: no

  tasks:
    - name: Load a text file.
      ansible.builtin.debug:
        msg: "{{ contents }}"
      vars:
        contents: "{{ lookup('file', 'samplefile.txt') }}"

変数 contents にファイル「 samplefile.txt 」の内容を読み込みました。ファイル内の改行は \n に置換されます。

y_mrok@ctrl:~/code/exam30$ ansible-playbook -i hosts.yml lookup1.yml 

PLAY [Example usage of the lookup plugin.] ***************************************************************************************************

TASK [Load a text file.] *********************************************************************************************************************
ok: [marutamachi] => {
    "msg": "taro,password@taro\njiro,password@jiro\nhanako,password@hanako"
}

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

y_mrok@ctrl:~/code/exam30$ 

csvfile

csv ファイルから指定したキーの値に合致する行の特定の項目を読み込みます。

引数

引数 内容
キー 検索する行のキーの値
file= 検索対象の CSV ファイル名
delimiter= csv ファイルの区切り文字
※デフォルト : タブ文字
col= 取り出す項目の番号 ( ゼロオリジン )
※デフォルト : 1

使用例

使用例

samplefile.txt
taro,password@taro
jiro,password@jiro
hanako,password@hanako
hosts.yml
---
all:
  hosts:
    marutamachi:
      ansible_host: 192.168.111.101
      ansible_user: vagrant
      ansible_password: vagrant
lookup2.yml
---
- name: Example usage of the lookup plugin.
  hosts: all
  gather_facts: no

  tasks:
    - name: Read the password for the user account taro.
      ansible.builtin.debug:
        msg: "{{ lookup('csvfile', 'taro file=samplefile.txt delimiter=, col=1') }}"

ファイル「 samplefile.txt 」内から "taro" のパスワード "password@taro" を読み込みました。

y_mrok@ctrl:~/code/exam30$ ansible-playbook -i hosts.yml lookup2.yml 

PLAY [Example usage of the lookup plugin.] ***************************************************************************************************

TASK [Read the password for the user account taro.] ******************************************************************************************
ok: [marutamachi] => {
    "msg": "password@taro"
}

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

y_mrok@ctrl:~/code/exam30$ 

演習問題

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

脚注
  1. プラグインの詳細は「 Working With Plugins 」を参照ください。 ↩︎