Ansibleを使って管理サーバの状態をサクッと確認してみよう(ansible-playbook使用編)

## はじめに
[Ansibleを使って管理サーバの状態をサクッと確認してみよう](http://qiita.com/takachan/items/3712661ef6f306cdf7d2)
につづいて、ansible-playbookを作成して実行してみます。

## playbook作成
```
vi ./confirm_openssl.yml
```
## playbook内容
```confirm_openssl.yml
---
- hosts: all
  sudo: yes
  vars:
  	pkg: openssl
  
  tasks:
	- name: confirm openssl version
  	shell: yum list "{{pkg}}"
	register: result
	- debug: var=result.stdout_lines
```

## playbook実行
```
ansible-playbook confirm_openssl.yml -i hosts
```

## playbook実行結果
```
PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [stg-web-02]
ok: [stg-web-01]
ok: [bastion]
ok: [dev]

TASK: [confirm openssl version] ***********************************************
changed: [stg-web-01]
changed: [stg-web-02]
changed: [dev]
changed: [bastion]

TASK: [debug var=result.stdout_lines] *****************************************
ok: [stg-web-02] => {
    "var": {
        "result.stdout_lines": [
            "Loaded plugins: priorities, update-motd, upgrade-helper",
            "Installed Packages",
            "openssl.x86_64                1:1.0.1k-1.84.amzn1                  @amzn-updates",
            "Available Packages",
            "openssl.i686                  1:1.0.1k-10.87.amzn1                 amzn-updates ",
            "openssl.x86_64                1:1.0.1k-10.87.amzn1                 amzn-updates "
        ]
    }
}
ok: [stg-web-01] => {
    "var": {
        "result.stdout_lines": [
            "Loaded plugins: priorities, update-motd, upgrade-helper",
            "Installed Packages",
            "openssl.x86_64                1:1.0.1k-1.84.amzn1                  @amzn-updates",
            "Available Packages",
            "openssl.i686                  1:1.0.1k-10.87.amzn1                 amzn-updates ",
            "openssl.x86_64                1:1.0.1k-10.87.amzn1                 amzn-updates "
        ]
    }
}
ok: [dev] => {
    "var": {
        "result.stdout_lines": [
            "Loaded plugins: priorities, update-motd, upgrade-helper",
            "Installed Packages",
            "openssl.x86_64                 1:1.0.1k-1.82.amzn1                 @amzn-updates",
            "Available Packages",
            "openssl.i686                   1:1.0.1k-1.84.amzn1                 amzn-updates ",
            "openssl.x86_64                 1:1.0.1k-1.84.amzn1                 amzn-updates "
        ]
    }
}
ok: [bastion] => {
    "var": {
        "result.stdout_lines": [
            "Loaded plugins: priorities, update-motd, upgrade-helper",
            "Installed Packages",
            "openssl.x86_64                1:1.0.1k-1.84.amzn1                  @amzn-updates",
            "Available Packages",
            "openssl.i686                  1:1.0.1k-10.86.amzn1                 amzn-updates ",
            "openssl.x86_64                1:1.0.1k-10.86.amzn1                 amzn-updates "
        ]
    }
}

PLAY RECAP ********************************************************************
dev                   : ok=3    changed=1    unreachable=0    failed=0
stg-web-01            : ok=3    changed=1    unreachable=0    failed=0
stg-web-02            : ok=3    changed=1    unreachable=0    failed=0
bastion               : ok=3    changed=1    unreachable=0    failed=0
```

## おまけ
パッケージ確認->パッケージ更新->パッケージ再確認の流れをplaybookで記載

```update_openssl.yml
---
- hosts: all
  sudo: yes
  vars:
  	pkg: openssl
  
  tasks:
	- name: before openssl version
  	  shell: yum list "{{pkg}}"
	  register: result
	- debug: var=result.stdout_lines
	
	- name: update "{{pkg}}"
	  yum: name="{{pkg}}" state=latest
	
	- name: after openssl version
  	  shell: yum list "{{pkg}}"
	  register: result
	- debug: var=result.stdout_lines

```