141004 ansible as a better shell script
-
Upload
takuya-nishimoto -
Category
Technology
-
view
390 -
download
2
description
Transcript of 141004 ansible as a better shell script
Ansible as a better shell script
@24motz2014-10-04 LT 駆動開発 08
http://www.ansible.com/home
やっていること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"
最初の自動化 =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
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
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
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 }}”
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_ }}"
結果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
最近やってみたこと$ 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
これから試すこと - fetch : リモートから管理マシンにファイルコピー
- unarchive: copy + shell tar xf
- local_action: ローカルで実行
- debug msg="{{ 'hello world'.upper()[:5] }}"