python製 æ§æ管çã½ãã
chef(ã¾ã¨ãã«ä½¿ã£ããã¨ãªãã®ã§è¯ãç¥ããªããã©)ã¯ånodeã管çãµã¼ãã¸ã¢ã¯ã»ã¹ããæ¹å¼ã§ãansibleã¯ãµã¼ãããånodeã¸ã¢ã¯ã»ã¹ããpushæ¹å¼ããªã®ã§ä¸é¨ã®moduleãå©ç¨ããªãéããånodeã¸ä½ããinstallããå¿ è¦ã¯ãªãã管çãµã¼ãããsshã§ã¢ã¯ã»ã¹ãã§ããã°è¯ãã
ãã¡ã¤ã«ãã³ãã¼ãããã³ãã³ããå®è¡ããããªã©ã®å¦çãå®ç¾©ãã¦ãããã®ãmoduleã
ããããã¾ã¨ãããã®ãplaybookã
ã¾ã¨ãã£ã¦æ¸ãã¦ãããã©å¾åç²ãã¦ãã¦ãã åæãã¦ããã ããªã®ã§ãã¾ãã¾ã¨ã¾ã£ã¦ãªãã
(ã»ãã¨ã«åæè¨å®ã®å°å ¥é¨åãããã£ãç¨åº¦ã ãã©)chefã®ä»çµã¿ã¯ç解ãè¾ãã£ããã®ã«å¯¾ãã¦ãansible ã¯ããããããã¨æãã¾ããå人çã«ã
install
githubããåã£ã¦ãããepelããyumã§å
¥ãã
# yum install -y --enablerepo=epel ansible
- è¨å®ãã¡ã¤ã« (yumã§installããå ´å)
- /etc/ansible/ansible.cfg
- /etc/ansible/hosts
- /usr/share/ansible/module_name
hosts
yumã§installããã¨defaultã®ãã¡ã¤ã«ã¯/etc/ansible/hostsã«ãªãã
åã«hosts(ip, hostname) ãæ¸ãã¦ããã ãã§ãè¯ãããiniå½¢å¼ã§ã°ã«ã¼ãã«ãããã¨ãå¯è½ãã¾ããwww-[1:5]ãwww-[01:09]ãapp-[a:e] ã®ããã«[]ã使ãã¨è¤æ°ã®ãã¹ãã«å±éãã¦ãããã
å®è¡
> # ansible
ping module ã§åhostsã¸ã®çéã確èªãã¦ããã
# ansible all -m ping sv01 | success >> { "changed": false, "ping": "pong" } sv02 | success >> { "changed": false, "ping": "pong" }
moduleãæå®ããªãå ´åã¯defaultã®command moduleã«ãªã(-a ã§æå®ããå¼æ°ãå®è¡ãã)ã
commandã¨ä¼¼ãshell moduleããã£ã¦ãååã®éãshellã®ç°å¢å¤æ°($PATH, $HOMEãªã©)ã使ã£ãã'||'ã'&&'ãªã©ãå¼æ°ã§ä½¿ãããªãã¤ã¬ã¯ãã使ãããå ´åã¯shell module ã使ããä»ã«ãmoduleã¯çãã ããããyumã®å ´åã¯/usr/share/ansible 以ä¸ã«å
¥ã£ã¦ãã
# ansible all -a 'uptime' sv01 | success | rc=0 >> 14:47:58 up 15 days, 8:23, 0 users, load average: 0.00, 0.00, 0.00 sv02 | success | rc=0 >> 14:47:58 up 39 days, 12:09, 0 users, load average: 0.00, 0.00, 0.00
module
cliã§ãdocumentè¦ããã
# ansible-doc module_name
ãã¡ã¤ã«ãã³ãã¼ãããã³ãã³ããå®è¡ããããªã©ã®å¦çãå®ç¾©ãã¦ãããã®ãmoduleã
å ¬å¼ã®ããã¥ã¡ã³ããã£ã¨æãèªã¿ãã¦ã¨ãããã使ã£ã¦ã¿ãmoduleã¡ã¢ã
System
- ping
- 確èªç¨
- user
- user追å ãåé¤ãgroup, shell ãªã©ãæå®å¯è½
- passwordã¯cryptedãªæååã§æå®
- python -c 'import crypt; print crypt.crypt("This is my Password", "$1$SomeSalt$")' ãªã©ã§çæã§ãã
- service
- service(init.d ã®script) ã®èµ·åãåæ¢ãchkconfig ãªã©ã®bootæã®èµ·åè¨å®ãªã©
- action: service name=nginx state=started enabled=yes
Commands
- command
- å¼æ°ãcommandã¨ãã¦å®è¡
- shell
- shellã使ã£ã¦å¼æ°ãcommandã¨ãã¦å®è¡
- shellãªã®ã§'>>'ã$HOME ãªã©ã使ãã
- script
- localã®scriptãremoteã«è»¢éãã¦remoteã§å®è¡ãã
- ~/.ansible/tmp/以ä¸ã«ä¸æãã¡ã¤ã«ãä½ããã¦ãå®è¡å¾ã«åé¤ãããã¿ãã
- "action: script /tmp/test.pl hoge fuga" ãå®è¡ãã¦ãremoteã§psè¦ã¦ãã
- "bash -c chmod +x /root/.ansible/tmp/ansible-1371288988.93-24846179799534/test.pl; /root/.ansible/tmp/ansible-1371288988.93-24846179799534/test.pl hoge fuga" ã¨ãã£ã
File
- copyãtemplateã§backupãªãã·ã§ã³ãyesã«ããã¨ããã¡ã¤ã«ã«å¤æ´ããã£ãå ´åã«æ¥ä»ä»ãã§backupãã¦ããã
- copy
- localã®ãã¡ã¤ã«ãremoteã«copy
- copy src=/path/to/src dest=/path/to/dest
- copyæã«ownerãmode(権é) ãæå®å¯è½
- fetch
- remotenã®ãã¡ã¤ã«ãlocalã®directoryã«copy
- fetch src=/path/to/src dest=/path/to/dest
- destdirã®ä¸ã«åremoteã®hostãã¨ã« /path/to/dest/hostname/path/to/src ã®ããã«ãã£ã¬ã¯ããªãä½ããã
- file
- æ¢åã®fileã®æ¨©éããªã¼ããå¤æ´ããããsymlinkãä½æããããã
- file path=/path/to/file owner=foo mode=0644
- file src=/path/to/src dest=/path/to/dest state=link
- template
- localã®templateãã¡ã¤ã«ãremoteã«copy
- varsã§è¨å®ããå¤æ°ãå®ç¾©ãã¦ããã°copyã«å±éãã¦ããã
Network
- get_url
- æå®ããURLãããã¡ã¤ã«ãdownloadãã
- get_url url=http://example.com/file dest=/path/to/file
- uri
- æå®ããURLã«ã¢ã¯ã»ã¹ãã
- "ånode"ã«pyhonã®urlparse httplib2 moduleãå¿
è¦
- pip ãªã©ã§installãã¦ãã
- installããã¦ããªãã¨"msg: httplib2 is not installed"ã¨ããã¨ã©ã¼ã¡ãã»ã¼ã¸ãåºã
- httpã§ã¢ã¯ã»ã¹ãããã®ã¯clientãªã®ã§ãclientå´ã«httplib2 moduleãããã
- å½åãansible = clientã«ã¤ã³ã¹ãã¼ã«ä¸è¦ã¨ããã®ãæãè¾¼ãã§ãããã©ãããã¯ansibleèªä½ãä¸è¦ãªã®ã§ãã£ã¦ãã®ä»ã®python moduleçã¯ç¨éã«ãã£ã¦ã¯å¿ è¦
Notification
mail, irc, jabber ãªã©ã使ã£ã¦éç¥ããmodule
- jabber
- to "user ID or name of the room, when using room use a slash to indicate your nick."
- ã¨ãããã©ãroomã使ã£ã¦ãªãå ´åã§ãæ«å°¾ã«"/"ãå ¥ããªãã¨ã¨ã©ã¼ã«ãªã
invalid output was: Traceback (most recent call last): File "/root/.ansible/tmp/ansible-1371788441.16-271641304974135/jabber", line 1052, in <module> main() File "/root/.ansible/tmp/ansible-1371788441.16-271641304974135/jabber", line 103, in main to, nick = re.split( r'/', module.params['to']) ValueError: need more than 1 value to unpack
# grep -n 'to, nick' ../library/notification/jabber 103: to, nick = re.split( r'/', module.params['to'])
ããã§ã"/"ã§splitãã¦ããã©ãtoã«"/"ãå«ãã§ãªãå ´åã«ãé
åã®æ°ãä¸è´ããªãããã¨ã©ã¼ã«ãªãï¼
playbook
moduleãçµã¿åãããä¸é£ã®å¦çãå®ç¾©ãããå®ç¾©ããyamlã¯ansible-playbook ã§ä½¿ãã
# ansible-playbook filename.yml
âã®docã®sample
--- - hosts: webservers vars: http_port: 80 max_clients: 200 user: root tasks: - name: ensure apache is at the latest version action: yum pkg=httpd state=latest - name: write the apache config file action: template src=/srv/httpd.j2 dest=/etc/httpd/conf/httpd.conf notify: - restart apache - name: ensure apache is running action: service name=httpd state=started handlers: - name: restart apache action: service name=httpd state=restarted
playbookã®ä¸»ãªå 容
- Basics
hosts, user ãªã©ãè¨å®ãuserã«root以å¤ãæå®ããå ´åãsudoã§ããããè¨å®ãã¦ããã¦"sudo: yes" ã¨æ¸ãããsudoå®è¡æã«ãã¹ã¯ã¼ããå¿
è¦ãªå ´åã¯ansible-playbook ã«--ask-sudo-pass (-K) ãæå®ããã
vers ã«ã¯å¤æ°ãè¨å®ãããé
åãè¨å®å¯è½ãplaybookã®ä¸ããtemplate (jinja2)ã§å©ç¨ããã
These variables can be used later in the playbook like this:
$varname or ${varname} or {{ varname }}
ã¨ããããtemplateã®ä¸ã§ã¯å
¨ã¦ä½¿ããããplaybookã®yamlå
ã§ã¯$varname, ${varname} ãã使ããªãã£ããpathã¯å¼æ°ã§{{ varname }} ã使ãã¨ããã®ã¾ã¾"{{ varname }}"ã¨ããæåã§æ±ããã¦ãæ³å®ãã¦ããåä½ã«ãªããªãã«å¤±æããã
--- - hosts: webservers user: root vars: http_port: 80 van_halen_port: 5150 other: 'magic' resolvers: - 192.168.1.10 - 192.168.1.20
- Tasks List
ansibleã§å®è¡ããããå¦çãè¨è¿°ããã
name: æ¨æºåºåã«è¡¨ç¤ºãããdescriptionã
action: moduleã¨å¼æ°ãæå®ãã
actionã®çç¥ããæ¸ãæ¹ã§ãmoduleããã®ã¾ã¾yamlã®keyã«ãããã¨ãå¯è½ã
tasks: - name: install latest nginx action: yum name=nginx state=latest enablerepo=epel - name: make sure nginx is running action: service name=nginx state=started
ããã¯âã§ãOKã
tasks: - name: install latest nginx yum: name=nginx state=latest enablerepo=epel - name: make sure nginx is running service: name=nginx state=started
ã¨ã©ã¼ãç¡è¦ãã
失æããã¨ãã«ã¨ã©ã¼ãç¡è¦ãããå ´åã¯taskså
ã®ãªã¹ãã®é
ç®ã§"ignore_errors: True" ã¨ãã¦ããã
ä¾ãã°atdãµã¼ãã¹ãOSèµ·åæã«èµ·åããªãããã«ããã(chkconfig atd off)æã¯ã
service: name=atd enabled=no ãªã©ã¨æ¸ãã°ããããã ãã©ãatd ãinstallããã¦ããªãå ´åã¯ã¨ã©ã¼ã«ãªã£ã¦å¦çãæ¢ã¾ããèµ·åããªãããã«ããè¨å®ãªã®ã§ãinstallããã¦ããªãå ´åã¯ãã®ã¾ã¾ã¨ã©ã¼ãç¡è¦ãã¦å¦çãé²ãã¦æ¬²ããã¨ãã£ãå ´åã¯tasksã次ã®ããã«æ¸ãã
tasks: - name: make atd not running service: name=atd state=stopped enabled=no ignore_errors: True
追è¨
ansible variables
ansible_os_family => "Redhat" ãªã©ansibleãå®ç¾©ãã¦ããå¤æ°ãsetup ã¢ã¸ã¥ã¼ã«ã§ç¢ºèªå¯è½ã
# ansible hostname -m setup (snip) "ansible_lsb": { "codename": "Final", "description": "CentOS release 5.9 (Final)", "id": "CentOS", "major_release": "5", "release": "5.9" }, (snip)