LoginSignup
37
58

Ansibleの使い方

Last updated at Posted at 2017-01-01

最新版は以下に記載しました。
モジュール編
ansible.posixモジュール編

デバッグ編
ループ処理編
マジック変数、ファクト変数編
配列、連想配列編
Handlersセクション編

#1 構成
検証環境は、下記2台のホストで構成しています。

構成
 master1(管理ホスト兼対象ホスト)                         node1(対象ホスト)
     +-------------------------------- ssh --------------+
    192.168.0.10                                      192.168.0.30
ホスト名 IPアドレス OS版数 ansible版数 役割
master1 192.168.0.10 CentOS7.2 2.2 管理ホスト(*1)兼対象ホスト(*2)
node1 192.168.0.30 CentOS7.2 - 対象ホスト(*2)

(*1) 管理ホスト:ansibleコマンドを実行して、対象ホストに設定を行うホスト
(*2) 対象ホスト:設定が行われるホスト

#2 事前準備
master1(管理ホスト)とnode1(対象ホスト)でそれぞれ行う。

  • rpmパッケージをインストールする。
  • sshパスフレーズなしでログインできるようにする。

##2.1 管理ホスト(master1)で行う設定

rpmパッケージのインストール
管理ホストにansibleをインストールする。
なお、epel-releaseを事前にインストールしておく必要がある。
[root@master1 ~]# yum -y install epel-release
[root@master1 ~]# yum -y install ansible
-以下、略-

ansibleの版数を確認する。
[root@master1 ~]# ansible --version
ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

docker-pythonをインストールする。docker_imageモジュールを使うときに必要。
[root@master1 ansible]# yum -y install docker-python
sshパスフレーズなしでログイン可能にする
[root@master1 ~]# pwd
/root
[root@master1 ~]# mkdir .ssh
[root@master1 ~]# chmod 700 .ssh/
[root@master1 ~]# ls -ld .ssh/
drwx------ 2 root root 6  1月  1 18:29 .ssh/
[root@master1 ~]# cd .ssh/

秘密鍵、公開鍵を作成する。
[root@master1 .ssh]# ssh-keygen -t dsa
-以下、略-

作成した鍵を確認する。
[root@master1 .ssh]# ls
id_dsa  id_dsa.pub

公開鍵(id_dsa.pub)を対象ホスト(node1)に転送する。
[root@master1 .ssh]# scp id_dsa.pub root@node1:/root
-以下、略-

自分自身(master1)もパスフレーズ入力なしでsshログインできるように設定する。
[root@master1 .ssh]# pwd
/root/.ssh

公開鍵をauthorized_keysファイルに追加する。
[root@master1 .ssh]# cat id_dsa.pub >> authorized_keys
[root@master1 .ssh]# ls
authorized_keys  id_dsa  id_dsa.pub

PubkeyAuthenticationを有効にする。
[root@master1 .ssh]# vi /etc/ssh/sshd_config
PubkeyAuthentication yes

sshdを再起動する。
[root@master1 .ssh]# systemctl restart sshd

##2.2 対象ホスト(node1)で行う設定

rpmパッケージのインストール
docker-pythonをインストールする。docker_imageモジュールを使うときに必要。
[root@master1 ansible]# yum -y install docker-python
sshパスフレーズなしでログイン可能にする
[root@node1 ~]# pwd
/root
[root@node1 ~]# ls -la id_dsa.pub
-rw-r--r-- 1 root root 602  1月  1 18:38 id_dsa.pub

公開鍵をauthorized_keysファイルに追加する。
[root@node1 ~]# cat id_dsa.pub >> .ssh/authorized_keys

PubkeyAuthenticationを有効にする。
[root@node1 ~]# vi /etc/ssh/sshd_config
PubkeyAuthentication yes

sshdを再起動する。
[root@node1 ~]# systemctl restart sshd

#3 モジュールの使い方

モジュールとは、対象ホストでおこなう操作のことです。
モジュールを使うことで、対象ホストでファイルの作成といったことができます。

##3.1 fileモジュール
###3.1.1 ディレクトリを作成する(state=directory)

インベントリファイルの作成
インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30
playbookファイルの作成
playbookファイルを作成する。
[root@master1 ansible]# cat test.yaml
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: ディレクトリを作成する。
      file: path=/tmp/node state=directory
[root@master1 ansible]#

作成したファイルを確認する。
[root@master1 ansible]# ls
hosts  test.yaml
ansible-playbookの実行
ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [ディレクトリを作成する。] ************************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0
実行結果の確認
[root@node1 tmp]# pwd
/tmp
[root@node1 tmp]# ls -ld node
drwxr-xr-x 2 root root 6  1月  3 09:58 node

あと始末(次の実験のため、ディレクトリを削除しておく)
[root@node1 tmp]# rm -f -r *
[root@node1 tmp]# ls
[root@node1 tmp]#

###3.1.2 ファイルを作成する(state=touch)

インベントリファイルの作成
インベントリファイルを作成する。
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30
playbookファイルの作成
playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: ディレクトリを作成する。
      file: path=/tmp/test state=directory
    - name: ファイルを作成する。
      file: path=/tmp/test/test.txt state=touch
ansible-playbookの実行
ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [ディレクトリを作成する。] ************************************************************
changed: [192.168.0.30]

TASK [ファイルを作成する。] **************************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=3    changed=2    unreachable=0    failed=0
実行結果の確認
[root@node1 test]# pwd
/tmp/test
[root@node1 test]# ls -la test.txt
-rw-r--r-- 1 root root 0  1月  3 11:06 test.txt

あと始末(次の実験のため、ディレクトリを削除しておく)
[root@node1 test]# cd ..
[root@node1 tmp]# rm -f -r *
[root@node1 tmp]# ls
[root@node1 tmp]#

##3.2 shellモジュール(対象ホストで任意のコマンドを実行する)

インベントリファイルの作成
インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30
playbookファイルの作成
playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: ディレクトリを作成する。
      file: path=/tmp/test state=directory
    - name: ファイルを作成する。
      file: path=/tmp/test/test.txt state=touch
    - name: ホスト名をファイルに書き込む
      shell: hostname > /tmp/test/test.txt

作成したファイルを確認する。
[root@master1 ansible]# ls
hosts  test.yaml
ansible-playbookの実行
ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [ディレクトリを作成する。] ************************************************************
changed: [192.168.0.30]

TASK [ファイルを作成する。] **************************************************************
changed: [192.168.0.30]

TASK [ホスト名をファイルに書き込む] **********************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=4    changed=3    unreachable=0    failed=0
実行結果の確認
[root@node1 test]# pwd
/tmp/test
[root@node1 test]# ls
test.txt

node1のホスト名が書き込まれていることがわかる。
[root@node1 test]# cat test.txt
node1

あと始末(次の実験のため、ディレクトリを削除しておく)
[root@node1 test]# cd ../
[root@node1 tmp]# rm -f -r *
[root@node1 tmp]# ls

##3.3 copyモジュール(管理ホストのファイルを対象ホストに転送する)

テスト用ファイルを作成する。
[root@master1 ansible]# pwd
/root/ansible
[root@master1 ansible]# ls
hosts  test.yaml
[root@master1 ansible]# touch hostname.txt
[root@master1 ansible]# hostname > hostname.txt
[root@master1 ansible]# cat hostname.txt
master1

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: ディレクトリを作成する。
      file: path=/tmp/test state=directory
    - name: 管理ホストのファイルを対象ホストに転送する。
      copy: src=/root/ansible/hostname.txt dest=/tmp/test/hostname.txt

[root@master1 ansible]# ls
hostname.txt  hosts  test.yaml

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [ディレクトリを作成する。] ************************************************************
changed: [192.168.0.30]

TASK [管理ホストのファイルを対象ホストに転送する。] **************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=3    changed=2    unreachable=0    failed=0

[root@master1 ansible]#


--------------------
実行結果を確認する。
--------------------
[root@node1 test]# pwd
/tmp/test

master1のホスト名が書き込まれていることがわかる。
[root@node1 test]# cat hostname.txt
master1

あと始末(次の実験のため、ディレクトリを削除しておく)
[root@node1 test]# cd ../
[root@node1 tmp]# rm -f -r *
[root@node1 tmp]# ls
[root@node1 tmp]#

##3.4 scriptモジュール(対象ホストでシェルスクリプトを実行する)

テスト用スクリプトを作成する(対象ホストで実行)
[root@master1 ansible]# vi test.sh
[root@master1 ansible]# cat test.sh
#!/usr/bin/bash
hostname > /tmp/test/hostname.txt

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: ディレクトリを作成する。
      file: path=/tmp/test state=directory
    - name: 対象ホストでシェルスクリプトを実行する。
      script: test.sh

作成したファイルを確認する。
[root@master1 ansible]# ls
hosts  test.sh  test.yaml

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [ディレクトリを作成する。] ************************************************************
changed: [192.168.0.30]

TASK [対象ホストでシェルスクリプトを実行する。] ****************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=3    changed=2    unreachable=0    failed=0

[root@master1 ansible]#


--------------------
実行結果を確認する。
--------------------
[root@node1 test]# pwd
/tmp/test
[root@node1 test]# cat hostname.txt
node1

あと始末(次の実験のため、ディレクトリを削除しておく)
[root@node1 tmp]# rm -f -r *
[root@node1 tmp]# ls
[root@node1 tmp]#

##3.5 yumモジュール(rpmパッケージのインストール、アンインストールを行う)

###3.5.1 最新版数のインストール

-----------------------------------------------------
1. 最新パッケージをインストールする場合(state=latest)
-----------------------------------------------------
インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: 最新のbcパッケージをインストールする。
      yum: name=bc state=latest
[root@master1 ansible]#

作成したファイルを確認する。
[root@master1 ansible]# ls
hosts  test.yaml

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [最新のbcパッケージをインストールする。] ****************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#


--------------------
実行結果を確認する。
--------------------
rpmパッケージがインストールできたことがわかる。
[root@node1 tmp]# bc --version
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
[root@node1 tmp]#


-----------------------------------------------------
2. パッケージをアンインストールする場合(state=removed)
-----------------------------------------------------
playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: 最新のbcパッケージをインストールする。
      yum: name=bc state=removed
[root@master1 ansible]#

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [最新のbcパッケージをインストールする。] ****************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#


--------------------
実行結果を確認する。
--------------------
rpmパッケージがアンインストールできたことがわかる。
[root@node1 tmp]# bc --version
-bash: /usr/bin/bc: そのようなファイルやディレクトリはありません
[root@node1 tmp]#

###3.5.2 指定した版数のインストール
テスト用のrpmパッケージとしてflannelを使う

flannelがインストールされているかどうかを確認する。インストールされていないことがわかる。
[root@master1 ansible]# rpm -qa |grep flannel
[root@master1 ansible]#

利用可能なflannelの版数を調べる。
[root@master1 ansible]# yum --showduplicates list flannel
-中略-
利用可能なパッケージ
flannel.x86_64                               ★0.2.0-10.el7                           extras
flannel.x86_64                                 0.5.3-8.el7                            extras
flannel.x86_64                                 0.5.3-9.el7                            extras
flannel.x86_64                               ■0.5.5-1.el7                             extras
[root@master1 ansible]#

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

--------------------------------------------
1. 一番古い版数のflannelをインストールする
--------------------------------------------
playbookファイルを作成する。一番古いflannel(上記★印)をインストールする。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: master  #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: 指定した版数のrpmをダウンロードする
      yum: name=flannel-0.2.0-10.el7.x86_64 state=present
[root@master1 ansible]#

作成したファイルを確認する。
[root@master1 ansible]# ls
hosts  test.yaml
[root@master1 ansible]#

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [master] ******************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.10]

TASK [指定した版数のrpmをダウンロードする] *****************************************************
changed: [192.168.0.10]

PLAY RECAP *********************************************************************
192.168.0.10               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#

flannleの版数を確認する。一番古い版数のflannel(上記★印)がインストールされたことがわかる。
[root@master1 ansible]# rpm -qa|grep flannel
flannel-0.2.0-10.el7.x86_64
[root@master1 ansible]#

次の実験のため、インストールしたflannelを削除する。
[root@master1 ansible]# yum -y remove flannel
-以下、省略-

[root@master1 ansible]# rpm -qa|grep flannel
[root@master1 ansible]#


--------------------------------------------
2. 一番新しい版数のflannelをインストールする
--------------------------------------------
playbookファイルを作成する。一番新しいflannel(上記■印)をインストールする。
[root@master1 ansible]# cat test.yaml
- hosts: master  #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: 指定した版数のrpmをダウンロードする
      yum: name=flannel-0.5.5-1.el7.x86_64 state=present
[root@master1 ansible]#

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [master] ******************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.10]

TASK [指定した版数のrpmをダウンロードする] *****************************************************
changed: [192.168.0.10]

PLAY RECAP *********************************************************************
192.168.0.10               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#

flannleの版数を確認する。一番新しい版数のflannel(■印)がインストールされたことがわかる。
[root@master1 ansible]# rpm -qa|grep flannel
flannel-0.5.5-1.el7.x86_64
[root@master1 ansible]#

##3.6 get_urlモジュール(HTTP,HTTPS,FTPを使ってファイルをダウンロードする)

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node  #対象ホストを指定する。
  tasks:       #実行するtaskを指定する。
    - name: /tmpにprometheusのyamlファイルをダウンロードする
      get_url: url=https://raw.githubusercontent.com/prometheus/prometheus/master/documentation/examples/prometheus.yml dest=/tmp
[root@master1 ansible]#


作成したファイルを確認する。
[root@master1 ansible]# ls
hosts  test.yaml

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [/tmpにprometheusのyamlファイルをダウンロードする] ***************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#


--------------------
実行結果を確認する。
--------------------
[root@node1 tmp]# pwd
/tmp

prometheusのyamlファイルがダウンロードできたことがわかる。
[root@node1 tmp]# ls
prometheus.yml

あと始末(次の実験のため、ディレクトリを削除しておく)
[root@node1 tmp]# rm -f -r *
[root@node1 tmp]# ls
[root@node1 tmp]#

##3.7 unarchiveモジュール(圧縮ファイルの解凍を行う)

###3.7.1 圧縮ファイルを対象ホストに転送した後、解凍する

テスト用の圧縮ファイルを作成する。
[root@master1 ansible]# head -c 1000 /dev/urandom > a.txt
[root@master1 ansible]# head -c 1000 /dev/urandom > b.txt
[root@master1 ansible]# tar cvfz test.tar.gz a.txt b.txt
a.txt
b.txt

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30
[root@master1 ansible]#

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node  #対象ホストを指定する。
  tasks:       #実行するtaskを指定する。
    - name: 圧縮ファイルを解凍する
      unarchive: src=test.tar.gz dest=/tmp
[root@master1 ansible]#


作成したファイルを確認する。
[root@master1 ansible]# ls
a.txt  b.txt  hosts  test.tar.gz  test.yaml
[root@master1 ansible]#

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [圧縮ファイルを解凍する] *************************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#


--------------------
実行結果を確認する。
--------------------
[root@node1 tmp]# pwd
/tmp

master1からnode1に転送した圧縮ファイルが解凍されていることがわかる。
[root@node1 tmp]# ls
a.txt  b.txt
[root@node1 tmp]#

あと始末(次の実験のため、ファイルを削除しておく)
[root@node1 tmp]# rm -f -r *
[root@node1 tmp]# ls
[root@node1 tmp]#

###3.7.2 対象ホストの圧縮ファイルを解凍する場合(copy=noを使う)

-----------------------------------------
node1でテスト用圧縮ファイルを作成する。
-----------------------------------------
[root@node1 ansible]# pwd
/root/ansible
[root@node1 ansible]# head -c 1000 /dev/urandom > a.txt
[root@node1 ansible]# head -c 1000 /dev/urandom > b.txt
[root@node1 ansible]# tar cvfz test.tar.gz a.txt b.txt
a.txt
b.txt
[root@node1 ansible]# ls
a.txt  b.txt  test.tar.gz
[root@node1 ansible]#

-----------------------------------------
master1でテスト用ファイルを作成する。
-----------------------------------------
インベントリファイルを作成する。
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30
[root@master1 ansible]#

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node  #対象ホストを指定する。
  tasks:       #実行するtaskを指定する。
    - name: 対象ホストで圧縮ファイルを解凍する
      unarchive: src=/root/ansible/test.tar.gz dest=/tmp copy=no
[root@master1 ansible]#

master1でも同じ名前の圧縮ファイルを作成する。
[root@master1 ansible]# head -c 1000 /dev/urandom > c.txt
[root@master1 ansible]# head -c 1000 /dev/urandom > d.txt
[root@master1 ansible]# tar cvfz test.tar.gz c.txt d.txt
c.txt
d.txt

作成したファイルを確認する。
[root@master1 ansible]# ls
c.txt  d.txt  hosts  test.tar.gz  test.yaml
[root@master1 ansible]#


ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [対象ホストで圧縮ファイルを解凍する] *******************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#


--------------------
実行結果を確認する。
--------------------
[root@node1 tmp]# pwd
/tmp

node1で作成した圧縮ファイルが解凍されていることがわかる。
[root@node1 tmp]# ls -l
合計 8
-rw-r--r-- 1 root root 1000  1月  3 12:21 a.txt
-rw-r--r-- 1 root root 1000  1月  3 12:21 b.txt

あと始末(次の実験のため、ファイルを削除しておく)
[root@node1 tmp]# rm -f -r *
[root@node1 tmp]# ls
[root@node1 tmp]#

##3.8 userモジュール(ユーザの追加、削除等を行う)

-----------------------------------------------
1. ユーザを追加する
-----------------------------------------------
インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node  #対象ホストを指定する。
  tasks:       #実行するtaskを指定する。
    - name: 対象ホストでuser1というユーザを追加する
      user: name=user1
[root@master1 ansible]#

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [対象ホストでuser1というユーザを追加する] **************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#

--------------------
実行結果を確認する。
--------------------
対象ホストでユーザを確認する。user1が追加されたことがわかる。
[root@node1 tmp]# cat /etc/passwd|grep user1
user1:x:1001:1001::/home/user1:/bin/bash


-----------------------------------------------
2. ユーザを削除する (state=absentを指定する)
-----------------------------------------------
インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node  #対象ホストを指定する。
  tasks:       #実行するtaskを指定する。
    - name: 対象ホストでuser1というユーザを削除する
      user: name=user1 state=absent

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [対象ホストでuser1というユーザを削除する] **************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#

--------------------
実行結果を確認する。
--------------------
対象ホストでユーザを確認する。user1が削除されたことがわかる。
[root@node1 tmp]# cat /etc/passwd|grep user1
[root@node1 tmp]#

##3.9 templateモジュール(テンプレートファイルを元にファイルを作成する)

テンプレートファイルを作成する。
[root@master1 ansible]# vi template.j2
[root@master1 ansible]# cat template.j2
user {{ myname }}

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

playbookファイルを作成する。変数mynameにuser1を指定する、という設定内容。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node  #対象ホストを指定する。
  vars:
    myname: user1
  tasks:      #実行するtaskを指定する。
    - name: テンプレートファイルをもとにファイルを作成する。
      template: src=template.j2 dest=/tmp/test.conf
[root@master1 ansible]#


ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [テンプレートファイルをもとにファイルを作成する。] ************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0


--------------------
実行結果を確認する。
--------------------
テンプレートファイルをもとに作成できたことがわかる。
[root@node1 ~]# cat /tmp/test.conf
user user1

##3.10 docker_imageモジュール(dockerイメージをpullする)
playbookファイルの内容は以下のとおり。
・master1 では、prometheusとnode-exporterの2つのイメージをpullする。
・node1 では、node-exporterのイメージをpullする。

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: master  #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: prometheusイメージをpullする
      docker_image: name=prom/prometheus
    - name: node-exporterイメージをpullする
      docker_image: name=prom/node-exporter
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: node-exporterイメージをpullする
      docker_image: name=prom/node-exporter


ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [master] ******************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.10]

TASK [prometheusイメージをpullする] ***************************************************
changed: [192.168.0.10]

TASK [node-exporterイメージをpullする] ************************************************
changed: [192.168.0.10]

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [node-exporterイメージをpullする] ************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.10               : ok=3    changed=2    unreachable=0    failed=0
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#

--------------------------------
master1で実行結果を確認する。
--------------------------------
[root@master1 ansible]# docker images prom/prometheus
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
docker.io/prom/prometheus   latest              bdeacb538ef9        5 weeks ago         79.23 MB

[root@master1 ansible]# docker images prom/node-exporter
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
docker.io/prom/node-exporter   latest              7faa2f21a307        5 weeks ago         14.56 MB
[root@master1 ansible]#

--------------------------------
node1で実行結果を確認する。
--------------------------------
[root@node1 tmp]# docker images prom/prometheus
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

[root@node1 tmp]# docker images prom/node-exporter
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
docker.io/prom/node-exporter   latest              7faa2f21a307        5 weeks ago         14.56 MB
[root@node1 tmp]#

##3.11 debugモジュール

###3.11.1 コマンドの実行結果を表示する。

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

playbookファイルを作成する。
[root@master1 ansible]# vi test.yml
[root@master1 ansible]# cat test.yml
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: コマンドの実行結果をresultに設定する
      shell: ls -l
      register: result
    - name: 結果を表示する
      debug: var=result
[root@master1 ansible]#

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [コマンドの実行結果をresultに設定する] ***************************************************
changed: [192.168.0.30]

TASK [結果を表示する] *****************************************************************
ok: [192.168.0.30] => {
    "result": {
        "changed": true,
        "cmd": "ls -l",
        "delta": "0:00:00.013831",
        "end": "2017-01-05 21:08:40.477085",
        "rc": 0,  ★コマンド(ls -l)の実行結果。0は正常終了したことを表す。
        "start": "2017-01-05 21:08:40.463254",
        "stderr": "",
        "stdout": "合計 4\n-rw-------. 1 root root 1328 12月 11 12:00 anaconda-ks.cfg\ndrwxr-xr-x  2 root root   48  1月  3 12:21 ansible",
        "stdout_lines": [
            "合計 4",
            "-rw-------. 1 root root 1328 12月 11 12:00 anaconda-ks.cfg",
            "drwxr-xr-x  2 root root   48  1月  3 12:21 ansible"
        ],
        "warnings": []
    }
}

PLAY RECAP *********************************************************************
192.168.0.30               : ok=3    changed=1    unreachable=0    failed=0

[root@master1 ansible]#


###3.11.2 コマンドの実行結果(result.rc)によって実行する処理を振り分ける。

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30
[root@master1 ansible]#

playbookファイルを作成する。
コマンドが異常終了したときにデバッグ情報を表示する、という内容。
しかし、ls -lは正常終了するので、デバッグ情報は表示されない。
[root@master1 ansible]# vi test.yml
[root@master1 ansible]# cat test.yml
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: コマンドの実行結果をresultに設定する
      shell: ls -l
      register: result
    - name: コマンドの実行結果によってデバッグ情報を表示する
      debug: var=result
      when: result.rc != 0  ###★この1行だけ追加した。

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yml -i hosts

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [コマンドの実行結果をresultに設定する] ***************************************************
changed: [192.168.0.30]

TASK [コマンドの実行結果によってデバッグ情報を表示する] ************************************************
skipping: [192.168.0.30]  ★コマンドが正常終了したので、デバッグ情報は表示されない。

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#

#4 そのほか
##4.1 繰り返し(with_items)

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node #対象ホストを指定する。
  tasks:      #実行するtaskを指定する。
    - name: /tmp配下にtest1,test2,test3ディレクトリを作成する
      file: path=/tmp/{{ item }} state=directory
      with_items:
        - test1
        - test2
        - test3
[root@master1 ansible]#

[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [/tmp配下にtest1,test2,test3ディレクトリを作成する] *************************************
changed: [192.168.0.30] => (item=test1)
changed: [192.168.0.30] => (item=test2)
changed: [192.168.0.30] => (item=test3)

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#


--------------------
実行結果を確認する。
--------------------
[root@node1 tmp]# pwd
/tmp

test1,test2,test3ができていることがわかる。
[root@node1 tmp]# ls
test1  test2  test3

あと始末(次の実験のため、ディレクトリを削除しておく)
[root@node1 tmp]# rm -f -r *
[root@node1 tmp]# ls
[root@node1 tmp]#

##4.2 条件付き実行(when)

###4.2.1 条件として使える変数を確認する。

変数がたくさんあるので、とりあえず、ansible_os_familyとansible_distributionにどんな値が設定されているかを表示する。

[root@master1 ansible]# ansible -m setup -i hosts 192.168.0.30|grep -e ansible_os_family -e ansible_distribution
        "ansible_distribution": "CentOS",
        "ansible_distribution_major_version": "7",
        "ansible_distribution_release": "Core",
        "ansible_distribution_version": "7.2.1511",
        "ansible_os_family": "RedHat",

###4.2.2 条件付きで実行する

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: node #対象ホストを指定する。
  tasks:      #実行するtaskを指定する。
    - name: ディストリビューションがCentOSなら実行する
      file: path=/tmp/centos.txt state=touch
      when: ansible_distribution == "CentOS"
    - name: ディストリビューションがubuntuなら実行する
      file: path=/tmp/ubuntu.txt state=touch
      when: ansible_distribution == "Ubuntu"
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [node] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [ディストリビューションがCentOSなら実行する] ************************************************
changed: [192.168.0.30]

TASK [ディストリビューションがubuntuなら実行する] ************************************************
skipping: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#


--------------------
実行結果を確認する。
--------------------
[root@node1 tmp]# pwd
/tmp

ディストリビューションがCentOSであることがわかる。
[root@node1 tmp]# ls
centos.txt

あと始末(次の実験のため、ファイルを削除しておく)
[root@node1 tmp]# rm -f -r *
[root@node1 tmp]# ls
[root@node1 tmp]#

##4.3 インベントリファイルのホスト名を使う(inventory_hostname)

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
master1
[node]
master2
[root@master1 ansible]#

playbookファイルを作成する。
gather_factsにnoを指定することで、
ホストに情報を読みにいかなくなる。つまりsetupモジュールが実行されなくなる。
つまり、ansible_xxx といった情報は取得しない。
[root@master1 ansible]# vi test.yml
[root@master1 ansible]# cat test.yml
- hosts: all
  gather_facts: no
  tasks:
    - debug: msg="etcd_host:{{inventory_hostname}}"

ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook -i hosts test.yml

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

TASK [debug] *******************************************************************
ok: [master2] => {
    "msg": "etcd_host:master2"   ★インベントリファイルのホスト名が表示できた。
}
ok: [master1] => {
    "msg": "etcd_host:master1"   ★インベントリファイルのホスト名が表示できた。
}

PLAY RECAP *********************************************************************
master1                    : ok=1    changed=0    unreachable=0    failed=0
master2                    : ok=1    changed=0    unreachable=0    failed=0

#5 ansibleコマンドのオプション

##5.1 実行するタスクを表示する(--list-tasks オプション)

テスト用playbookファイルの中身を確認する。
[root@master1 ansible]# cat test.yaml
- hosts: master  #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: prometheusイメージをpullする
      docker_image: name=prom/prometheus
    - name: node-exporterイメージをpullする
      docker_image: name=prom/node-exporter
    - name: /etc/prometheusディレクトリを作成する
      file: path=/etc/prometheus state=directory
    - name: prometheus.yamlをダウンロードする
      get_url: url=https://raw.githubusercontent.com/prometheus/prometheus/master/documentation/examples/prometheus.yml dest=/etc/prometheus
    - name: prometheus.ymlを書き換える
      lineinfile: >-
        dest=/etc/prometheus/prometheus.yml
        state=present
        backrefs=yes
        regexp='\- targets: '
        line='      - targets: ['192.168.0.30:9100']'
    - name: node_exporterを起動する
      shell: docker run -d -p 9100:9100 --net="host" prom/node-exporter
    - name: prometheusを起動する
      shell: docker run -d -p 9090:9090 -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: node-exporterイメージをpullする
      docker_image: name=prom/node-exporter
    - name: node_exporterを起動する
      shell: docker run -d -p 9100:9100 --net="host" prom/node-exporter
[root@master1 ansible]#

実行するタスクを表示する。
[root@master1 ansible]# ansible-playbook test.yaml --list-tasks -i hosts

playbook: test.yaml

  play #1 (master): master      TAGS: []
    tasks:
      prometheusイメージをpullする      TAGS: []
      node-exporterイメージをpullする   TAGS: []
      /etc/prometheusディレクトリを作成する     TAGS: []
      prometheus.yamlをダウンロードする TAGS: []
      prometheus.ymlを書き換える        TAGS: []
      node_exporterを起動する   TAGS: []
      prometheusを起動する      TAGS: []

  play #2 (node): node  TAGS: []
    tasks:
      node-exporterイメージをpullする   TAGS: []
      node_exporterを起動する   TAGS: []
[root@master1 ansible]#

##5.2 実行するホストを表示する(--list-hosts オプション)

検証に使うplaybookファイルは、5.1と同じものを使う。

[root@master1 ansible]# ansible-playbook test.yaml --list-hosts -i hosts

playbook: test.yaml

  play #1 (master): master      TAGS: []
    pattern: [u'master']
    hosts (1):
      192.168.0.10

  play #2 (node): node  TAGS: []
    pattern: [u'node']
    hosts (1):
      192.168.0.30
[root@master1 ansible]#

##5.3 実行するホストを絞り込む(-l オプション)

インベントリファイルの内容を確認する。masterグループとnodeグループがあることがわかる。
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30
[root@master1 ansible]#

playbookファイルの内容を確認する。
[root@master1 ansible]# cat test.yaml
- hosts: master  #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: 指定した版数のrpmをダウンロードする
      yum: name=flannel-0.5.5-1.el7.x86_64 state=present
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - name: 指定した版数のrpmをダウンロードする
      yum: name=flannel-0.5.5-1.el7.x86_64 state=present
[root@master1 ansible]#


masterグループのホストだけ(★印)、コマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts -l master

PLAY ★[master] ******************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.10]

TASK [指定した版数のrpmをダウンロードする] *****************************************************
changed: [192.168.0.10]

PLAY RECAP *********************************************************************
192.168.0.10               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#


nodeグループのホストだけ(★印)、コマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts -l node

PLAY ★[node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [指定した版数のrpmをダウンロードする] *****************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#


##5.4 ansible-playbookコマンド実行時の詳細情報表示(-v,-vv,-vvv オプション)

-vvvが一番詳細に情報を表示する。-vが一番おおまかな情報を表示する。

もっとも詳細な情報を表示する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts -vvv
Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: test.yaml ************************************************************
2 plays in test.yaml

PLAY [master] ******************************************************************

TASK [setup] *******************************************************************
Using module file /usr/lib/python2.7/site-packages/ansible/modules/core/system/setup.py
<192.168.0.10> ESTABLISH SSH CONNECTION FOR USER: None
-以下、略-

##5.5 他のyamlファイルを読み込む(include)

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[master]
192.168.0.10
[node]
192.168.0.30
[root@master1 ansible]#

playbookファイルを作成する。
[root@master1 ansible]# vi test.yaml
[root@master1 ansible]# cat test.yaml
- hosts: master  #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - include: master/master.yaml
- hosts: node    #対象ホストを指定する。
  tasks:         #実行するtaskを指定する。
    - include: node/node.yaml
[root@master1 ansible]#

[root@master1 ansible]# cat master/master.yaml
- name: flannelパッケージをダウンロードする。
  yum: name=flannel-0.5.5-1.el7.x86_64 state=present
[root@master1 ansible]#

[root@master1 ansible]# cat node/node.yaml
- name: flannelパッケージをダウンロードする。
  yum: name=flannel-0.2.0-10.el7.x86_64 state=present
[root@master1 ansible]#


作成したファイルを確認する。
[root@master1 ansible]# ls -l
合計 8
-rw-r--r-- 1 root root  42  1月  3 09:34 hosts
drwxr-xr-x 2 root root  24  1月  4 21:24 master
drwxr-xr-x 2 root root  22  1月  4 21:25 node
-rw-r--r-- 1 root root 274  1月  4 21:25 test.yaml
[root@master1 ansible]#


ansible-playbookコマンドを実行する。
[root@master1 ansible]# ansible-playbook test.yaml -i hosts

PLAY [master] ******************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.10]

TASK [flannelパッケージをダウンロードする。] **************************************************
changed: [192.168.0.10]

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.30]

TASK [flannelパッケージをダウンロードする。] **************************************************
changed: [192.168.0.30]

PLAY RECAP *********************************************************************
192.168.0.10               : ok=2    changed=1    unreachable=0    failed=0
192.168.0.30               : ok=2    changed=1    unreachable=0    failed=0

[root@master1 ansible]#



--------------------
実行結果を確認する。
--------------------
ホスト(master1)でrpmパッケージの版数を確認する。
[root@master1 ansible]# rpm -qa|grep flannel
flannel-0.5.5-1.el7.x86_64
[root@master1 ansible]#


ホスト(node1)でrpmパッケージの版数を確認する。
[root@node1 ~]# rpm -qa|grep flannel
flannel-0.2.0-10.el7.x86_64
[root@node1 ~]#

##5.6 playbookファイルの実行順序を確認する。

インベントリファイルの定義順序はplaybookファイルの実行順序に関係ない。
playbookファイルのtasksが実行順序に関係することがわかった。
つまり、playbookファイルのtasksを上から下に順に実行していく。

インベントリファイルを作成する。
[root@master1 ansible]# vi hosts
[root@master1 ansible]# cat hosts
[etcd]
master1
master2
[master]
master1
[node]
master2


[root@master1 ansible]# vi test.yml
[root@master1 ansible]# cat test.yml
- hosts: etcd
  tasks:
    - name:
      debug: msg="etcd:{{inventory_hostname}}"
- hosts: master
  tasks:
    - name:
      debug: msg="master:{{inventory_hostname}}"
- hosts: node
  tasks:
    - name:
      debug: msg="node:{{inventory_hostname}}"
[root@master1 ansible]#


playbookファイルの定義を上から順に実行していることがわかる。
[root@master1 ansible]# ansible-playbook -i hosts test.yml

PLAY [etcd] ********************************************************************

TASK [setup] *******************************************************************
ok: [master2]
ok: [master1]

TASK [debug] *******************************************************************
ok: [master1] => {
    "msg": "etcd:master1"
}
ok: [master2] => {
    "msg": "etcd:master2"
}

PLAY [master] ******************************************************************

TASK [setup] *******************************************************************
ok: [master1]

TASK [debug] *******************************************************************
ok: [master1] => {
    "msg": "master:master1"
}

PLAY [node] ********************************************************************

TASK [setup] *******************************************************************
ok: [master2]

TASK [debug] *******************************************************************
ok: [master2] => {
    "msg": "node:master2"
}

PLAY RECAP *********************************************************************
master1                    : ok=4    changed=0    unreachable=0    failed=0
master2                    : ok=4    changed=0    unreachable=0    failed=0

[root@master1 ansible]#

#6 フィルタ
##6.1 regex_replace
Ansible公式ページ:Filters

後方参照(その1)
[root@master ansible]# cat test.yaml
- hosts: localhost
  gather_facts: no
  vars:
    test1: "{{ '12345' | regex_replace('(1)2345', '\\1') }}"
    test2: "{{ '12345' | regex_replace('1(2)345', '\\1') }}"
    test3: "{{ '12345' | regex_replace('(1)(2)(3)(4)(5)', '\\5\\4\\3\\2\\1') }}"
  tasks:
    - debug: msg="test1={{ test1 }}"
    - debug: msg="test2={{ test2 }}"
    - debug: msg="test3={{ test3 }}"
[root@master ansible]#


[root@master ansible]# ansible-playbook test.yaml
 [WARNING]: provided hosts list is empty, only localhost is available


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

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "test1=1"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "test2=2"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "test3=54321"
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0
後方参照(その2)
[root@master ansible]# cat test.yaml
# http://docs.ansible.com/ansible/playbooks_filters.html#id8
- hosts: localhost
  gather_facts: no
  vars:
    # convert "ansible" to "able"
    test1: "{{ 'ansible' | regex_replace('^a.*i(.*)$', 'a\\1') }}"
    # convert "foobar" to "bar"
    test2: "{{ 'foobar' | regex_replace('^f.*o(.*)$', '\\1') }}"
    # convert "localhost:80" to "localhost, 80" using named groups
    test3: "{{ 'localhost:80' | regex_replace('^(?P<host>.+):(?P<port>\\d+)$', '\\g<host>, \\g<port>') }}"
  tasks:
    - debug: msg="test1={{ test1 }}"
    - debug: msg="test2={{ test2 }}"
    - debug: msg="test3={{ test3 }}"
[root@master ansible]#

playbookを実行する。
[root@master ansible]# ansible-playbook test.yaml
 [WARNING]: provided hosts list is empty, only localhost is available


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

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "test1=able"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "test2=bar"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "test3=localhost, 80"
}

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

[root@master ansible]#

#X 参考情報
Ansible Documentation
Ansible 入門 - ビーブレイクシステムズ
Ansible でコマンドの出力を後の task で使う
ゼロから始めた構成管理

[SoftwareDesigne 2016年1月号]

37
58
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
37
58