2
0

More than 3 years have passed since last update.

Ansible: vars_files を使用した loop処理

Last updated at Posted at 2020-04-07

Ansible でのループ処理を変数ファイルから読みこむ方法を確認しました。
(ループ処理(loopモジュール) と変数読み込み(vars_files)両方の内容がなかなか見つけられなかったので...)

ループ処理は以前は with_items をよく使用していたと思いますが、 loop を使うように記載がありました。(2020/4/7時点)

Ansibleマニュアル: loop
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-items
抜粋: "with_items is replaced by loop and the flatten filter."


環境

Ansible サーバー
- CentOS 7.6.1810 (Core)
-- Ansible 2.8.2
-- Python 2.7.5


・Ansible のクライアントサーバー
- AIX 7.2TL3SP3
-- Python 2.7.15

***AIX でもAnsibleのクライアントになれます! ***


playbooks

test.yml (処理実行のplaybook)

---
- hosts: all
  become: true
  vars_files:
    - variables.yml        #<- 指定の変数ファイル

  tasks:
  - name: variable1 test
    shell: |-
       set -x
       echo {{ item }}
    loop: "{{ variable1|flatten(levels=1) }}"

  - name: variable2 test
    shell: |-
       set -x
       echo {{ item }}
    loop: "{{ variable2|flatten(levels=1) }}"

・variables.yml (読み込み用変数ファイル)

# variables
variable1:
    - A
    - B
variable2:
    - C
    - D


実行ログ

test.yml 実行ログ

$ sudo ansible-playbook -i tmp/ip -u root --private-key=id_rsa '--ssh-common-args=-o StrictHostKeyChecking=no' -e ansible_python_interpreter=auto_silent test.yml

PLAY [all] ***********************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
Tuesday 07 April 2020  03:56:53 -0500 (0:00:00.051)       0:00:00.051 *********
ok: [192.168.0.xxx]

TASK [variable1 test] ************************************************************************************************************
Tuesday 07 April 2020  03:59:30 -0500 (0:02:36.992)       0:02:37.043 *********
changed: [192.168.0.xxx] => (item=A)
changed: [192.168.0.xxx] => (item=B)

TASK [variable2 test] ************************************************************************************************************
Tuesday 07 April 2020  03:59:31 -0500 (0:00:01.385)       0:02:38.429 *********
changed: [192.168.0.xxx] => (item=C)
changed: [192.168.0.xxx] => (item=D)

PLAY RECAP ***********************************************************************************************************************
192.168.0.xxx              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Tuesday 07 April 2020  03:59:32 -0500 (0:00:01.217)       0:02:39.647 *********
===============================================================================
Gathering Facts --------------------------------------------------------------------------------------------------------- 156.99s
variable1 test ------------------------------------------------------------------------------------------------------------ 1.39s
variable2 test ------------------------------------------------------------------------------------------------------------ 1.22s

以上です。

2
0
1

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
2
0