141004 ansible as a better shell script

11
Ansible as a better shell script @24motz 2014-10-04 LT 駆駆駆駆 08 http://www.ansible.com/home

description

Ansible as a better shell script 2014年10月4日 LT駆動開発08 Takuya Nishimoto @24motz

Transcript of 141004 ansible as a better shell script

Page 1: 141004 ansible as a better shell script

Ansible as a better shell script

@24motz2014-10-04 LT 駆動開発 08

http://www.ansible.com/home

Page 2: 141004 ansible as a better shell script
Page 3: 141004 ansible as a better shell script

やっていることNVDA 日本語版の更新チェックを数える(1) サーバーに ssh する(2) /var/log/httpd を ls してファイル名確認(3) grep キーワード ファイル名 | wc

[xx/Oct/2014:xx:xx:xx +0000] serverName xxx.xxx.xxx.xxx - - "GET /updateCheck/?language=ja_JP&autoCheck=True&x64=True&installed=True&version=2014.3jp&versionType=nvdajp&osVersion=6.1.7601+Service+Pack+1 HTTP/1.0" 200 - "- -> /updateCheck/index.py" "Python-urllib/1.17"

Page 4: 141004 ansible as a better shell script

最初の自動化 =ssh の代用品サーバーに ssh/var/log/httpd を ls してファイル名を確認

$ ansible -i hosts --sudo -m shell --args "ls /var/log/httpd" serverName$ ansible -i hosts --sudo -m shell --args "grep autoCheck=True /var/log/httpd/access_log-$LATEST | wc" serverName

Page 5: 141004 ansible as a better shell script

playbook を作ってみた$ ansible-playbook -i hosts playbook.yml---- hosts: serverName sudo: yes tasks: - name: set latest shell: ls /var/log/httpd | grep access_log | tail -1 | sed -e s/access_log-//g register: latest

Page 6: 141004 ansible as a better shell script

shell #2 tasks: - name: get nvda_all shell: grep autoCheck=True /var/log/httpd/access_log-{{ latest.stdout }} | wc | awk '{ print $1 }' register: nvda_all

Page 7: 141004 ansible as a better shell script

set_fact tasks: - name: set fact set_fact: latest_: "{{ latest.stdout }}" nvda_all_: "{{ nvda_all.stdout }}" nvda_2014_3jp_: "{{ nvda_2014_3jp.stdout }}" win62_: "{{ win62.stdout }}”

Page 8: 141004 ansible as a better shell script

debug tasks: - name: show msg debug: msg="{{ latest_ }} all:{{ nvda_all_ }} 2014.3jp:{{ nvda_2014_3jp_ }} 2014.2jp:{{ nvda_2014_2jp_ }} 2014.1jp:{{ nvda_2014_1jp_ }} 2013.3jp:{{ nvda_2013_3jp_ }} win51:{{ win51_ }} win60:{{ win60_ }} win61:{{ win61_ }} win62:{{ win62_ }} win63:{{ win63_ }} x64:{{ x64_ }} installed:{{ installed_ }}"

Page 9: 141004 ansible as a better shell script

結果PLAY [serverName] ************************************************************

GATHERING FACTS *************************************************************** ok: [serverName]

TASK: [set latest] ************************************************************ changed: [serverName]

TASK: [set fact] ************************************************************** ok: [serverName]

TASK: [show msg] ************************************************************** ok: [serverName] => { "msg": "20140928 all:3509 2014.3jp:2308 2014.2jp:730 2014.1jp:315 2013.3jp:152 win51:228 win60:124 win61:2163 win62:106 win63:888 x64:1909 installed:3387"}

PLAY RECAP ******************************************************************** serverName : ok=16 changed=13 unreachable=0 failed=0

Page 10: 141004 ansible as a better shell script

最近やってみたこと$ ansible-playbook --extra-vars “hosts=abc" - hosts: '{{ hosts }}'

- file: path=/data mode=0755 state=directory

- copy: src=hoge.tgz dest=/tmp/hoge.tgz

- service : name=httpd state=started

- shell: tar xfz /tmp/hoge.tgz args: - chdir: /data/hoge

Page 11: 141004 ansible as a better shell script

これから試すこと - fetch : リモートから管理マシンにファイルコピー

- unarchive: copy + shell tar xf

- local_action: ローカルで実行

- debug msg="{{ 'hello world'.upper()[:5] }}"