
ãã®å®çšçãªã¬ã€ãã§ã¯ãAnsibleã玹ä»ããŠããŸãã Ansibleã®ãã¹ããšããŠæ©èœããä»®æ³ãã·ã³ãŸãã¯å®ãã·ã³ãå¿
èŠã§ãã Vagrantã®ç°å¢ã«ã¯ããã®ãã¥ãŒããªã¢ã«ãä»å±ããŠããŸãã
Ansibleã¯ããªã¢ãŒãæ§æç®¡ççšã®ãœãããŠã§ã¢ãœãªã¥ãŒã·ã§ã³ã§ãã ãªã¢ãŒããã·ã³ãèšå®ã§ããŸãã ä»ã®åæ§ã®ã·ã¹ãã ãšã®äž»ãªéãã¯ãAnsibleãæ¢åã®SSHã€ã³ãã©ã¹ãã©ã¯ãã£ã䜿çšããã®ã«å¯ŸããŠãä»ïŒã·ã§ãããããããªã©ïŒã¯ç¹å¥ãªPKIç°å¢ã®ã€ã³ã¹ããŒã«ãå¿
èŠãšããããšã§ãã
ãã®ããã¥ã¢ã«ã«ã¯ã次ã®ãããã¯ãå«ãŸããŠããŸãã
- Ansibleããã³Vagrantã®ã€ã³ã¹ããŒã«
- ã€ã³ãã³ããªãã¡ã€ã«
- ã·ã§ã«ãã³ããŒã¢ãžã¥ãŒã«ããã¡ã¯ãã³ã¬ã¯ã·ã§ã³ã倿°
- ãã¹ãã®ã°ã«ãŒãã§å®è¡ãã
- ãã¬ã€ããã¯
- äŸïŒã¯ã©ã¹ã¿ãŒãäžããApacheãšHAproxyããŒããã©ã³ãµãŒãã€ã³ã¹ããŒã«ããŠæ§æããŸã
- ãšã©ãŒåŠçãããŒã«ããã¯
- æ§æãã³ãã¬ãŒã
- 圹å²
Ansibleã¯ããããããã·ã¥ã¢ãŒãã䜿çšããŸããèšå®ã¯ãã¹ããã·ã³ãããããã·ã¥ããããŸãã éåžžãä»ã®CMã·ã¹ãã ã¯å察ã®ããšãè¡ããŸããããŒãã¯ãã¹ããã·ã³ããæ§æãååŸããŸãã
ãã®ã¢ãŒãã¯ãããŒãã®ãªã¢ãŒãæ§æã«å
¬çã«ã¢ã¯ã»ã¹å¯èœãªãã¹ããã·ã³ãå¿
èŠãšããªããããè峿·±ããã®ã§ãã ãã®ããŒãã¯ã¢ã¯ã»ã¹å¯èœã§ãªããã°ãªããŸããïŒåŸã§é衚瀺ã®ããŒããèšå®ãåãåãããšãã§ããããšãããããŸãïŒã
Ansibleã«å¿
èŠãªãã®
次ã®Pythonã¢ãžã¥ãŒã«ãå¿
èŠã§ãã
Debian / Ubuntuã§ã次ãå®è¡ããŸãã
sudo apt-get install python-yaml python-jinja2 python-paramiko python-crypto
ã/ .sshã«ãããŒãã¢ãå¿
èŠã§ãã
Ansibleãã€ã³ã¹ããŒã«ãã
ãœãŒã¹ãã
develãã©ã³ãã¯åžžã«å®å®ããŠããããã䜿çšããŸãã gitã®ã€ã³ã¹ããŒã«ãå¿
èŠã«ãªãå ŽåããããŸãïŒDebian / Ubuntuã«sudo apt-get install git
ïŒã
git clone git://github.com/ansible/ansible.git cd ./ansible
ããã§ãAnsibleç°å¢ãããŠã³ããŒãã§ããŸãã
source ./hacking/env-setup
debããã±ãŒãžãã
sudo apt-get install make fakeroot cdbs python-support git clone git://github.com/ansible/ansible.git cd ./ansible make deb sudo dpkg -i ../ansible_1.1_all.deb (version may vary)
ãã®ãã¥ãŒããªã¢ã«ã§ã¯ããã®ç¹å®ã®æ¹æ³ã䜿çšããããšãåæãšããŠããŸãã
Vagrantãã€ã³ã¹ããŒã«ãã
Vagrantã䜿çšãããšãä»®æ³ãã·ã³ãç°¡åã«äœæããŠVirtualBoxã§å®è¡ã§ããŸãã Vagrantfileã«ã¯ããã¥ã¢ã«ãä»å±ããŠããŸãã
Vagrantãèµ·åããã«ã¯ãã€ã³ã¹ããŒã«ããå¿
èŠããããŸãã
次ã®ã³ãã³ãã§ä»®æ³ãã·ã³ãåæåããŸãã ãããã¯ã¹ããæåã§ããŠã³ããŒãããå¿
èŠã¯ãªãããšã«æ³šæããŠãã ããã ãã®ãã¥ãŒããªã¢ã«ã«ã¯ã宿ããVagrantfile
æ¢ã«å«ãŸããŠãããäœæ¥ã«å¿
èŠãªãã¹ãŠã®ãã®ãå«ãŸããŠããŸãã
vagrant up
ã³ãŒããŒã泚ãã§ãã ããïŒvagrant-hostmasterã䜿çšããå Žåã¯ãrootãã¹ã¯ãŒããå
¥åããå¿
èŠããããŸãïŒã åé¡ãçºçããå Žåã¯ãVagrantãã¥ãŒããªã¢ã«ãã芧ãã ãã ã
ä»®æ³ãã·ã³ã«SSHããŒã远å ãã
ç¶è¡ããã«ã¯ãä»®æ³ãã·ã³ã®authorized_keys
ã«ãŒãã«ããŒã远å ããå¿
èŠããããŸãã ããã¯ãªãã·ã§ã³ã§ãïŒAnsibleã¯sudoããã³ãã¹ã¯ãŒãèªèšŒã䜿çšã§ããŸãïŒããã¯ããã«ç°¡åã§ãã
Ansibleã¯ãã®ã¿ã¹ã¯ã«çæ³çã§ããããã䜿çšããŸãã ãã ãããŸã äœã説æããŸããã ãã ç§ãä¿¡ããŠã
ansible-playbook -c paramiko -i step-00/hosts step-00/setup.yml --ask-pass --sudo
ãã¹ã¯ãŒããšããŠvagrantãå
¥åããŸãã ãæ¥ç¶ãæåŠãããŸããããšã©ãŒãçºçããå Žåã¯ããã¡ã€ã¢ãŠã©ãŒã«ã®èšå®ã確èªããŠãã ããã
次ã«ãããŒãssh-agentã«è¿œå ããŸãïŒ ssh-add
ïŒã
åšåº«
次ã«ãã€ã³ãã³ããªãã¡ã€ã«ãæºåããå¿
èŠããããŸãã ããã©ã«ãã®å Žæã¯/etc/ansible/hosts
ã§ãã
ãã ããå¥ã®ãã¹ã䜿çšããããã«Ansibleãæ§æã§ããŸãã ãããè¡ãã«ã¯ãç°å¢å€æ°ïŒ ANSIBLE_HOSTS
ïŒãŸãã¯-i
ãã©ã°ã䜿çšããŸãã
次ã®ã€ã³ãã³ããªãã¡ã€ã«ãäœæããŸããã
host0.example.org ansible_ssh_host=192.168.33.10 ansible_ssh_user=root host1.example.org ansible_ssh_host=192.168.33.11 ansible_ssh_user=root host2.example.org ansible_ssh_host=192.168.33.12 ansible_ssh_user=root
ansible_ssh_host
ã¯ãæ¥ç¶ãäœæããããã¹ãã®IPã¢ãã¬ã¹ãå«ãç¹å¥ãªå€æ°ã§ãã ãã®å Žåãgem vagrant-hostmasterã䜿çšããå Žåã¯ãªãã·ã§ã³ã§ãã ãŸããä»®æ³ãã·ã³ã«ä»ã®ã¢ãã¬ã¹ãã€ã³ã¹ããŒã«ããŠæ§æããå Žåã¯ãIPã¢ãã¬ã¹ã倿Žããå¿
èŠããããŸãã
ansible_ssh_user
ã¯ãæå®ãããã¢ã«ãŠã³ãïŒãŠãŒã¶ãŒïŒã§æ¥ç¶ããããAnsibleã«æç€ºããå¥ã®ç¹å¥ãªå€æ°ã§ãã ããã©ã«ãã§ã¯ãAnsibleã¯çŸåšã®ã¢ã«ãŠã³ãããŸãã¯ã/ .ansible.cfgïŒ remote_user
ïŒã§æå®ãããå¥ã®ããã©ã«ãå€ã䜿çšããŸãã
確èªãã
Ansibleãã€ã³ã¹ããŒã«ãããã®ã§ããã¹ãŠãæ©èœããããšã確èªããŸãããã
ansible -m ping all -i step-01/hosts
ããã§ãAnsibleã¯åãã¹ãã§ping
ã¢ãžã¥ãŒã«ãå®è¡ããããšããŸãïŒã¢ãžã¥ãŒã«ã«ã€ããŠã¯åŸã»ã©èª¬æããŸãïŒã åºåã¯æ¬¡ã®ããã«ãªããŸãã
host0.example.org | success >> { "changed": false, "ping": "pong" } host1.example.org | success >> { "changed": false, "ping": "pong" } host2.example.org | success >> { "changed": false, "ping": "pong" }
ãããïŒ 3ã€ã®ãã¹ãã¯ãã¹ãŠæ£åžžã«åäœããŠãããAnsibleã¯ããããšéä¿¡ã§ããŸãã
ããŒããšã®éä¿¡
ããã§æºåãã§ããŸããã åã®ã»ã¯ã·ã§ã³ã§æ¢ã«ããªãã¿ã®ããŒã ã§ããansible
éãã§ã¿ãŸãããã ãã®ã³ãã³ãã¯ãAnsibleãããŒããšå¯Ÿè©±ããããã«äœ¿çšãã3ã€ã®ã³ãã³ãã®1ã€ã§ãã
äœã䟿å©ãªããšãããŸããã
æåŸã®ã³ãã³ãã§ã¯ã -m ping
ã¯ã pingã¢ãžã¥ãŒã«ã䜿çšãããããšãæå³ããŠããŸããã ããã¯ãAnsibleã§å©çšå¯èœãªå€ãã®ã¢ãžã¥ãŒã«ã®1ã€ã§ãã ping
ã¢ãžã¥ãŒã«ping
éåžžã«åçŽã§ãåŒæ°ãå¿
èŠãšããŸããã åŒæ°ãå¿
èŠãšããã¢ãžã¥ãŒã«ã¯ã -a
åŒæ°ãååŸã§ããŸãã ããã€ãã®ã¢ãžã¥ãŒã«ãèŠãŠã¿ãŸãããã
ã·ã§ã«ã¢ãžã¥ãŒã«
ãã®ã¢ãžã¥ãŒã«ã䜿çšãããšããªã¢ãŒããã¹ãã§ã·ã§ã«ã³ãã³ããå®è¡ã§ããŸãã
ansible -i step-02/hosts -m shell -a 'uname -a' host0.example.org
åºåã¯æ¬¡ã®ããã«ãªããŸãã
host0.example.org | success | rc=0 >> Linux host0.example.org 3.2.0-23-generic-pae
ç°¡åïŒ
ã³ããŒã¢ãžã¥ãŒã«
copy
ã¢ãžã¥ãŒã«ã䜿çšãããšããã¹ããã·ã³ãããªã¢ãŒããã¹ãã«ãã¡ã€ã«ãã³ããŒã§ããŸãã /etc/motd
ã/tmp
ããŒãã«ã³ããŒããå¿
èŠããããšæ³åããŠãã ããã
ansible -i step-02/hosts -m copy -a 'src=/etc/motd dest=/tmp/' host0.example.org
çµè«ïŒ
host0.example.org | success >> { "changed": true, "dest": "/tmp/motd", "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "size": 0, "src": "/root/.ansible/tmp/ansible-1362910475.9-246937081757218/motd", "state": "file" }
AnsibleïŒããæ£ç¢ºã«ã¯ãããŒãã§å®è¡ãããŠããã³ããŒã¢ãžã¥ãŒã«ïŒã¯ãJSON圢åŒã®æçšãªæ
å ±ã®æã§å¿çããŸããã åŸã§ãããã©ã®ããã«äœ¿çšã§ããããèŠãŠãããŸãã
Ansibleã«ã¯å·šå€§ãª
ã·ã¹ãã ã§å®è¡ã§ããã»ãŒãã¹ãŠãã«ããŒããã¢ãžã¥ãŒã«ã®ãªã¹ã ã é©åãªã¢ãžã¥ãŒã«ãèŠã€ãããªãã£ãå Žåãã¢ãžã¥ãŒã«ã®äœæã¯éåžžã«ç°¡åãªã¿ã¹ã¯ã§ãïŒPythonã§äœæããå¿
èŠã¯ãããŸãããäž»ãªããšã¯ãJSONãçè§£ããããšã§ãïŒã
å€ãã®ãã¹ãã1ã€ã®ããŒã
äžèšã¯ãã¹ãŠçŽ æŽãããã§ãããå€ãã®ãã¹ãã管çããå¿
èŠããããŸãã ãã£ãŠã¿ãŸãããã ããŒãã«é¢ããäºå®ãåéããããšãã°ãããŒãã«ã€ã³ã¹ããŒã«ãããŠããUbuntuã®ããŒãžã§ã³ã調ã¹ãããšããŸãã ããã¯éåžžã«ç°¡åã§ãã
ansible -i step-02/hosts -m shell -a 'grep DISTRIB_RELEASE /etc/lsb-release' all
all
ã¯ãã€ã³ãã³ããªãã¡ã€ã«å
ã®ãã¹ãŠã®ãã¹ãããæå³ããŸãã åºåã¯æ¬¡ã®ããã«ãªããŸãã
host1.example.org | success | rc=0 >> DISTRIB_RELEASE=12.04 host2.example.org | success | rc=0 >> DISTRIB_RELEASE=12.04 host0.example.org | success | rc=0 >> DISTRIB_RELEASE=12.04
ããå€ãã®äºå®
ç°¡åã§ã·ã³ãã«ã ãã ããããå€ãã®æ
å ±ïŒIPã¢ãã¬ã¹ãRAMãµã€ãºãªã©ïŒãå¿
èŠãªå Žåããã®ã¢ãããŒãã¯ããã«äžäŸ¿ã«ãªãããšããããŸãã 解決çã¯ã setup
ã¢ãžã¥ãŒã«ã䜿çšããããšã§ãã 圌ã¯ãããŒãããã®ãã¡ã¯ãã®åéãå°éãšããŠããŸãã
詊ããŠãã ããïŒ
ansible -i step-02/hosts -m setup host0.example.org
çãïŒ
"ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.0.60" ], "ansible_all_ipv6_addresses": [], "ansible_architecture": "x86_64", "ansible_bios_date": "01/01/2007", "ansible_bios_version": "Bochs" }, ---snip--- "ansible_virtualization_role": "guest", "ansible_virtualization_type": "kvm" }, "changed": false, "verbose_override": true
åºåã¯åçŽåã®ããã«ççž®ãããŠããŸããããã®æ
å ±ããå€ããåŠã¶ããšãã§ããŸãã ç¹å®ã®äœãã«èå³ãããå Žåã¯ãããŒããã£ã«ã¿ãªã³ã°ããããšãã§ããŸãã
ããšãã°ããã¹ãŠã®ãã¹ãã§äœ¿çšå¯èœãªã¡ã¢ãªéã調ã¹ãå¿
èŠããããŸãã ç°¡åã§ãïŒ ansible -i step-02/hosts -m setup -a 'filter=ansible_memtotal_mb' all
ïŒ
host2.example.org | success >> { "ansible_facts": { "ansible_memtotal_mb": 187 }, "changed": false, "verbose_override": true } host1.example.org | success >> { "ansible_facts": { "ansible_memtotal_mb": 187 }, "changed": false, "verbose_override": true } host0.example.org | success >> { "ansible_facts": { "ansible_memtotal_mb": 187 }, "changed": false, "verbose_override": true }
ããŒãã¯ãäžèšã§åçããééã£ãé åºã§åçããããšã«æ³šæããŠãã ããã Ansibleã¯ãã¹ããšäžŠè¡ããŠéä¿¡ããŸãïŒ
ãšããã§ã setup
ã¢ãžã¥ãŒã«ã䜿çšããå ŽåãåŒfilter=
*
ãæå®ã§ããŸãã ã·ã§ã«ã®ããã«ã
ãã¹ãéžæ
all
ããã¹ãŠã®ãã¹ãããæå³ããããšãããããŸããããAnsibleã«ã¯
ãã¹ããéžæããä»ã®æ¹æ³ã®æ ïŒ
host0.example.org:host1.example.org
ã¯host0.example.orgããã³
host1.example.orghost*.example.org
ã¯ãååããhostãã§å§ãŸãã.example.orgãã§çµãããã¹ãŠã®ãã¹ãã§èµ·åãããŸãïŒã·ã§ã«ãšåãïŒ
ãã¹ãã®ã°ã«ãŒãå
ã€ã³ãã³ããªå
ã®ãã¹ãã¯ã°ã«ãŒãåã§ããŸãã ããšãã°ã debian
ã°ã«ãŒãã web-servers
ã°ã«ãŒãã debian
ã°ã«ãŒããªã©ãäœæã§ããŸãã
[debian] host0.example.org host1.example.org host2.example.org
ççž®ããããšãã§ããŸãïŒ
[debian] host[0-2].example.org
åã°ã«ãŒããæå®ããå Žåã¯ã [groupname:children]
ã䜿çšããŠãããã«åã°ã«ãŒãã远å ããŸãã ããšãã°ãããŸããŸãªLinuxãã£ã¹ããªãã¥ãŒã·ã§ã³ããããæ¬¡ã®ããã«ç·šæã§ããŸãã
[ubuntu] host0.example.org [debian] host[1-2].example.org [linux:children] ubuntu debian
倿°ãèšå®ãã
ã€ã³ãã³ããªãã¡ã€ã«ããã¹ã倿°ãã¡ã€ã«ã倿°ã°ã«ãŒããã¡ã€ã«ãªã©ãããã€ãã®å Žæã§ãã¹ãã®å€æ°ã远å ã§ããŸãã
éåžžããã¹ãŠã®å€æ°ãã°ã«ãŒã/ãã¹ã倿°ãã¡ã€ã«ã«èšå®ããŸãïŒããã«ã€ããŠã¯åŸã§èª¬æããŸãïŒã ãã ããå€ãã®å Žåããã¹ãIPã¢ãã¬ã¹ãèšå®ããansible_ssh_host
ãªã©ãã€ã³ãã³ããªãã¡ã€ã«ã§çŽæ¥å€æ°ã䜿çšããŸãã ããã©ã«ãã§ã¯ãAnsibleã¯SSHçµç±ã§æ¥ç¶ãããšãã«ãã¹ãåã解決ããŸãã ãã ãããã¹ããåæåãããšããIPã¢ãã¬ã¹ããŸã ãªãå ŽåããããŸãã ãã®å Žåã ansible_ssh_host
ã圹ç«ã¡ãŸãã
ïŒéåžžã®ansible
ã§ã¯ãªãïŒ ansible-playbook
ansible
ã䜿çšãã--extra-vars
ïŒãŸãã¯-e
ïŒãã©ã°ã䜿çšããŠå€æ°ãèšå®ã§ããŸãã æ¬¡ã®ã¹ãããã§ã¯ã ansible-playbook
ããŒã ã«ã€ããŠèª¬æããŸãã
ansible_ssh_port
ããansible_ssh_port
ãSSHæ¥ç¶ããŒãã®èšå®ã«äœ¿çšãããŸãã
[ubuntu] host0.example.org ansible_ssh_host=192.168.0.12 ansible_ssh_port=2222
Ansibleã¯ãã°ã«ãŒãããã³ãã¹ã倿°ãã¡ã€ã«ã§è¿œå ã®å€æ°ãæ¢ããŸãã 圌ã¯ãã¡ã€ã³ã€ã³ãã³ããªãã¡ã€ã«ããããã£ã¬ã¯ããªå
ã®group_vars
ããã³host_vars
ãã£ã¬ã¯ããªã§ãããã®ãã¡ã€ã«ãæ¢ããŸãã
Ansibleã¯ååã§ãã¡ã€ã«ãæ€çŽ¢ããŸãã ããšãã°ãåè¿°ã®ã€ã³ãã³ããªãã¡ã€ã«ã䜿çšããå ŽåãAnsibleã¯ãã¡ã€ã«å
ã§host0.example.org
倿°ãæ¢ããŸãã
group_vars/linux
group_vars/ubuntu
host_vars/host0.example.org
ãããã®ãã¡ã€ã«ãååšããªãå Žåã¯äœãèµ·ãããŸããããååšããå Žåã¯äœ¿çšãããŸãã
ã¢ãžã¥ãŒã«ãã€ã³ãã³ããªãããã³å€æ°ã«ç²Ÿéããã®ã§ãæåŸã«Playbookã§ã®Ansibleã®çã®åã«ã€ããŠèª¿ã¹ãŸãããã
Playbook Ansible
ãã¬ã€ããã¯ã®æŠå¿µã¯éåžžã«ã·ã³ãã«ã§ããããã¯ãAnsibleãŠãŒãã£ãªãã£ã§äœ¿çšãããã®ãšåæ§ã®Ansibleã³ãã³ãïŒã¿ã¹ã¯ãã¿ã¹ã¯ïŒã®åãªãã³ã¬ã¯ã·ã§ã³ã§ãã ãããã®ã¿ã¹ã¯ã¯ãç¹å®ã®ããŒã/ã°ã«ãŒãã®ã»ããã察象ãšããŠããŸãã
Apacheã®äŸïŒå¥å "Hello WorldïŒ" InsibleïŒ
ã€ã³ãã³ããªãã¡ã€ã«ã次ã®ããã«èŠãããšããä»®å®ãç¶ããŸãïŒ hosts
ãšåŒã³ãŸãããïŒïŒ
[web] host1.example.org
ãã¹ãŠã®ãã¹ãã¯DebianããŒã¹ã®ã·ã¹ãã ã§ãã
æ³šïŒ ansible_ssh_host
ã䜿çšããŠå®éã®ãã¹ãIPã¢ãã¬ã¹ãèšå®ã§ããããšãæãåºããŠãã ããïŒãããŠããã®æŒç¿ã§ãããè¡ããŸãïŒã ã€ã³ãã³ããªã倿ŽããŠãå®éã®ãã¹ãåã䜿çšããããšãã§ããŸãã ãããã«ããŠããå®å
šã«å®éšã§ãããã·ã³ã䜿çšããŠãã ããã ãŸããå®éã®ãã¹ãã§ã¯ã ansible_ssh_user=root
ã远å ããŠãç°ãªãããã©ã«ãèšå®ã§ã®æœåšçãªåé¡ãåé¿ããŸãã
web
ã°ã«ãŒãã®ãã·ã³ã«Apacheãã€ã³ã¹ããŒã«ãããã¬ã€ããã¯ãäœæããŸãããã
- hosts: web tasks: - name: Installs apache web server apt: pkg=apache2 state=installed update_cache=true
é©åãªAnsibleã¢ãžã¥ãŒã«ã䜿çšããŠäœããããã®ããèšãã ãã§ãã ããã§ã¯ãDebianããã±ãŒãžãã€ã³ã¹ããŒã«ã§ããaptã¢ãžã¥ãŒã«ã䜿çšããŸãã ãŸãããã®ã¢ãžã¥ãŒã«ã«ãã£ãã·ã¥ã®æŽæ°ãäŸé ŒããŸãã
ãã®ã¿ã¹ã¯ã«ã¯ååãå¿
èŠã§ãã ããã¯å¿
é ã§ã¯ãããŸããããå©äŸ¿æ§ã®ããã«æãŸãããã®ã§ãã
ãŸããå
šäœçã«ã¯ãšãŠãç°¡åã§ããïŒ ããã§ããã¬ã€ããã¯ãéå§ã§ããŸãïŒ apache.yml
ãšåŒã³ãŸãããïŒïŒ
ansible-playbook -i step-04/hosts -l host1.example.org step-04/apache.yml
ããã§ã step-04/hosts
ã¯ã€ã³ãã³ããªãã¡ã€ã«ã§ãã- -l
ããã¹ãã§å®è¡ããããã«å¶éããŸãã
ãããŠapache.yml
ã¯ç§ãã¡ã®ãã¬ã€ããã¯ã§ãã
ã³ãã³ããå®è¡ãããšã次ã®ãããªåºåã衚瀺ãããŸãã
PLAY [web] ********************* GATHERING FACTS ********************* ok: [host1.example.org] TASK: [Installs apache web server] ********************* changed: [host1.example.org] PLAY RECAP ********************* host1.example.org : ok=2 changed=1 unreachable=0 failed=0
æ³šïŒ cowsay
ã€ã³ã¹ããŒã«ããŠããå Žåãçãéãéããããšã«æ°ä»ããããããŸãã:-)æ°ã«å
¥ããªããã°ã次ã®ããã«ç¡å¹ã«ã§ããŸãïŒ export ANSIBLE_NOCOWS="1"
ã
è¡ããšã«åºåãåæããŸãããã
PLAY [web] *********************
Ansibleã¯ããã¬ã€ã¯web
ã°ã«ãŒãã§å®è¡ãããããšã瀺ããŠãweb
ã Playã¯ãAnsibleãã¹ãé¢é£ã®åœä»€ã®ã³ã¬ã¯ã·ã§ã³ã§ãã ãã¬ã€ããã¯ã«å¥ã®-host: blah
ãããå Žåãããã衚瀺ãããŸãïŒãã ããæåã®ãã¬ã€ãå®äºããåŸïŒã
GATHERING FACTS ********************* ok: [host1.example.org]
setup
ã¢ãžã¥ãŒã«ã䜿çšãããšãã®ããšãèŠããŠããŸããïŒ ååçã®åã«ãAnsibleã¯åãã¹ãã§ãããå®è¡ãããã¡ã¯ããåéããŸãã ãããå¿
èŠãªãå ŽåïŒããšãã°ããã¹ãã«é¢ããæ
å ±ãå¿
èŠãªãããïŒããã¹ãè¡gather_facts: no
äžã«ïŒ tasks:
ãšåãã¬ãã«ã§ïŒ gather_facts: no
远å ã§ããŸãã
TASK: [Installs apache web server] ********************* changed: [host1.example.org]
ãããæãéèŠãªããšã§ããæåã§å¯äžã®ã¿ã¹ã¯ãèµ·åããã changed
ãšè¡šç€ºãããŠããããã host1.example.org
äœãã倿Žããããšãhost1.example.org
ãŸãã
PLAY RECAP ********************* host1.example.org : ok=2 changed=1 unreachable=0 failed=0
æåŸã«ãAnsibleã¯2ã€ã®ã¿ã¹ã¯ãå®äºãããã®ãã¡ã®1ã€ããã¹ãäžã®äœãã倿ŽããŸããïŒããã¯Apacheã¿ã¹ã¯ã§ãããã»ããã¢ããã¢ãžã¥ãŒã«ã¯äœã倿ŽããŸããïŒã
ãããããäžåºŠå®è¡ããŠãäœãèµ·ãããèŠãŠã¿ãŸãããã
$ ansible-playbook -i step-04/hosts -l host1.example.org step-04/apache.yml PLAY [web] ********************* GATHERING FACTS ********************* ok: [host1.example.org] TASK: [Installs apache web server] ********************* ok: [host1.example.org] PLAY RECAP ********************* host1.example.org : ok=2 changed=0 unreachable=0 failed=0
倿Žãããå€ã¯ã0ãã«çãããªããŸããã ããã¯å®å
šã«æ£åžžã§ãããAnsibleã®äž»ãªæ©èœã®1ã€ã§ãããã¬ã€ããã¯ã¯ãäœãããããšãããå Žåã«ã®ã¿äœããå®è¡ããŸãã ããã¯ã ã¹ãçæ§ãšåŒã°ããŸãã ããã¯ããã¬ã€ããã¯ãäœåã§ãå®è¡ã§ããããšãæå³ããŸãããæçµçã«ã¯åãç¶æ
ã®ãã·ã³ã«ãªããŸãïŒ shell
ã¢ãžã¥ãŒã«ã«å€¢äžã«ãªããªããŠããAnsibleã¯ãã以äžäœãã§ããŸããïŒã
Apache Suiteã®æ¹å
Apacheãã€ã³ã¹ããŒã«ããŸãããä»®æ³ãã¹ããèšå®ããŸãããã
ãã¬ã€ããã¯ã®æ¹å
ãµãŒããŒã«å¿
èŠãªä»®æ³ãã¹ãã¯1ã€ã ãã§ãããããã©ã«ããããå
·äœçãªãã®ã«å€æŽããå¿
èŠããããŸãã ãããã£ãŠãçŸåšã®ä»®æ³ãã¹ããåé€ããä»®æ³ãã¹ããéä¿¡ããã¢ã¯ãã£ãã«ããŠãApacheãåèµ·åããå¿
èŠããããŸãã
files
ãšãããã£ã¬ã¯ããªãäœæããhost1.example.orgã®èšå®ã远å ããŠã awesome-app
ãšããååãä»ããŸãããã
<VirtualHost *:80> DocumentRoot /var/www/awesome-app Options -Indexes ErrorLog /var/log/apache2/error.log TransferLog /var/log/apache2/access.log </VirtualHost>
ããã§ããã¬ã€ããã¯ãå°ãç¡å¹ã«ãªããå®äºã§ãã
- hosts: web tasks: - name: Installs apache web server apt: pkg=apache2 state=installed update_cache=true - name: Push default virtual host configuration copy: src=files/awesome-app dest=/etc/apache2/sites-available/ mode=0640 - name: Deactivates the default virtualhost command: a2dissite default - name: Deactivates the default ssl virtualhost command: a2dissite default-ssl - name: Activates our virtualhost command: a2ensite awesome-app notify: - restart apache handlers: - name: restart apache service: name=apache2 state=restarted
è¡ããïŒ
$ ansible-playbook -i step-05/hosts -l host1.example.org step-05/apache.yml PLAY [web] ********************* GATHERING FACTS ********************* ok: [host1.example.org] TASK: [Installs apache web server] ********************* ok: [host1.example.org] TASK: [Push default virtual host configuration] ********************* changed: [host1.example.org] TASK: [Deactivates the default virtualhost] ********************* changed: [host1.example.org] TASK: [Deactivates the default ssl virtualhost] ********************* changed: [host1.example.org] TASK: [Activates our virtualhost] ********************* changed: [host1.example.org] NOTIFIED: [restart apache] ********************* changed: [host1.example.org] PLAY RECAP ********************* host1.example.org : ok=7 changed=5 unreachable=0 failed=0
ãã£ãããïŒ èããŠã¿ãã°ãç§ãã¡ã¯å°ãå
ãè¡ã£ãŠããŸãã Apacheãåèµ·åããåã«èšå®ã®æ£ç¢ºæ§ã確èªããå¿
èŠã¯ãããŸããïŒ æ§æã«ãšã©ãŒãå«ãŸããŠããå Žåã«ãµãŒãã¹ãäžæããªãããã«ããããã
æ§æãšã©ãŒãçºçããå Žåã®åèµ·å
Apacheãã€ã³ã¹ããŒã«ããä»®æ³ãã¹ãã倿ŽããŠãµãŒããŒãåèµ·åããŸããã ããããèšå®ãæ£ããå Žåã«ã®ã¿ãµãŒããŒãåèµ·åãããå Žåã¯ã©ãã§ããããïŒ
åé¡ãããå Žåã¯ããŒã«ããã¯ããŸã
Ansibleã«ã¯ã¯ãŒã«ãªæ©èœãå«ãŸããŠããŸããäœãåé¡ãçºçãããšããã¹ãŠã®åŠçã忢ããŸãã èšå®ãæå¹ã§ãªãå Žåããã®æ©èœã䜿çšããŠãã¬ã€ããã¯ã忢ããŸãã
awesome-app
ä»®æ³ãã¹ãæ§æãã¡ã€ã«ã倿ŽããŠãç Žå£ããŠã¿ãŸãããã
<VirtualHost *:80> RocumentDoot /var/www/awesome-app Options -Indexes ErrorLog /var/log/apache2/error.log TransferLog /var/log/apache2/access.log </VirtualHost>
åã«ãèšã£ãããã«ãã¿ã¹ã¯ãå®äºã§ããªãå ŽåãåŠçã¯åæ¢ããŸãã ãã®ããããµãŒããŒãåèµ·åããåã«æ§æã®æå¹æ§ã確èªããå¿
èŠããããŸãã ãŸããä»®æ³ãã¹ãã远å ããŠããã©ã«ãã®ä»®æ³ãã¹ããåé€ããããšããå§ããŸãããããã£ãŠããã®åŸã®åèµ·åïŒãµãŒããŒäžã§çŽæ¥å®è¡ãããå¯èœæ§ããããŸãïŒã«ãã£ãŠApacheãå£ããããšã¯ãããŸããã
æåã«ãããè¡ãå¿
èŠããããŸããã ãã®ãã¬ã€ããã¯ãæ¢ã«éå§ããŠãããããããã©ã«ãã®ä»®æ³ãã¹ãã¯ãã§ã«éã¢ã¯ãã£ãåãããŠããŸãã åé¡ãããŸããïŒãã®ãã¬ã€ããã¯ã¯ä»ã®çœªã®ãªããã¹ãã§ã䜿çšã§ããã®ã§ãä¿è·ããŸãããã
- hosts: web tasks: - name: Installs apache web server apt: pkg=apache2 state=installed update_cache=true - name: Push future default virtual host configuration copy: src=files/awesome-app dest=/etc/apache2/sites-available/ mode=0640 - name: Activates our virtualhost command: a2ensite awesome-app - name: Check that our config is valid command: apache2ctl configtest - name: Deactivates the default virtualhost command: a2dissite default - name: Deactivates the default ssl virtualhost command: a2dissite default-ssl notify: - restart apache handlers: - name: restart apache service: name=apache2 state=restarted
è¡ããïŒ
$ ansible-playbook -i step-06/hosts -l host1.example.org step-06/apache.yml PLAY [web] ********************* GATHERING FACTS ********************* ok: [host1.example.org] TASK: [Installs apache web server] ********************* ok: [host1.example.org] TASK: [Push future default virtual host configuration] ********************* changed: [host1.example.org] TASK: [Activates our virtualhost] ********************* changed: [host1.example.org] TASK: [Check that our config is valid] ********************* failed: [host1.example.org] => {"changed": true, "cmd": ["apache2ctl", "configtest"], "delta": "0:00:00.045046", "end": "2013-03-08 16:09:32.002063", "rc": 1, "start": "2013-03-08 16:09:31.957017"} stderr: Syntax error on line 2 of /etc/apache2/sites-enabled/awesome-app: Invalid command 'RocumentDoot', perhaps misspelled or defined by a module not included in the server configuration stdout: Action 'configtest' failed. The Apache error log may have more information. FATAL: all hosts have already failed
ãæ°ã¥ãã®ããã«ã apache2ctl
ã¯ãšã©ãŒã³ãŒã1ãè¿ããŸããAnsibleã¯ãããèªèããŠåäœã忢ããŸãã ãããïŒ
ããŒããããã倧ããããšã¯ãªã...ãšã«ããä»®æ³ãã¹ãã远å ãããã ãã®åŸã®åèµ·åã®è©Šè¡ã§ãApacheã¯æ§æã®ãªã³ãšãªããèªããŸãã ãããã£ãŠããšã©ãŒããã£ããããŠäœæ¥ç¶æ
ã«æ»ãæ¹æ³ãå¿
èŠã§ãã
ïŒã³ã¡ã³ã翻蚳è
ïŒã³ã¡ã³ãã®@clickfreak habrayuzerã¯ãAnsible 2.xã®ç¹å¥ãªæ©èœãèŠãããææ¡ããŠããŸãïŒã
æ¡ä»¶ã®äœ¿çš
Apacheãã€ã³ã¹ããŒã«ããä»®æ³ãã¹ãã远å ããŠãµãŒããŒãåèµ·åããŸããã ãã ããäœãåé¡ãçºçããå Žåã¯ãäœæ¥ç¶æ
ã«æ»ããããšèããŠããŸãã
åé¡ã®è¿å
ããã«ã¯éæ³ã¯ãããŸããã éå»ã®ééãã¯Ansibleã®ããã§ã¯ãããŸããã ããã¯ããã¯ã¢ããã·ã¹ãã ã§ã¯ãªããéå»ã®ç¶æ³ã«å¯Ÿãããã¹ãŠãæåŠããæ¹æ³ãç¥ããŸããã ãã¬ã€ããã¯ã®å®å
šã¯ããªãã®è²¬ä»»ã§ãã Ansibleã¯ã a2ensite awesome-app
广ãå
ã«æ»ãæ¹æ³ãç¥ããŸããã
åè¿°ã®ããã«ãã¿ã¹ã¯ãå®äºã§ããªãå ŽåãåŠçã¯åæ¢ããŸããããšã©ãŒãåãå
¥ããããšãã§ããŸãïŒ ãããè¡ãå¿
èŠããããŸã ïŒã ãããã£ãŠããšã©ãŒãçºçããå Žåã«åŠçãç¶è¡ããŸããããã¹ãŠãåäœç¶æ
ã«æ»ããŸãã
- hosts: web tasks: - name: Installs apache web server apt: pkg=apache2 state=installed update_cache=true - name: Push future default virtual host configuration copy: src=files/awesome-app dest=/etc/apache2/sites-available/ mode=0640 - name: Activates our virtualhost command: a2ensite awesome-app - name: Check that our config is valid command: apache2ctl configtest register: result ignore_errors: True - name: Rolling back - Restoring old default virtualhost command: a2ensite default when: result|failed - name: Rolling back - Removing our virtualhost command: a2dissite awesome-app when: result|failed - name: Rolling back - Ending playbook fail: msg="Configuration file is not valid. Please check that before re-running the playbook." when: result|failed - name: Deactivates the default virtualhost command: a2dissite default - name: Deactivates the default ssl virtualhost command: a2dissite default-ssl notify: - restart apache handlers: - name: restart apache service: name=apache2 state=restarted
register
ããŒã¯ãŒãã¯ã apache2ctl configtest
ïŒexitã®åºåãæžã蟌ã¿ãŸã
statusãstdoutãstderrã...ïŒãããã³when: result|failed
倿°ã«
ïŒ result
ïŒã¹ããŒã¿ã¹ã倱æããŸããã
è¡ããïŒ
$ ansible-playbook -i step-07/hosts -l host1.example.org step-07/apache.yml PLAY [web] ********************* GATHERING FACTS ********************* ok: [host1.example.org] TASK: [Installs apache web server] ********************* ok: [host1.example.org] TASK: [Push future default virtual host configuration] ********************* ok: [host1.example.org] TASK: [Activates our virtualhost] ********************* changed: [host1.example.org] TASK: [Check that our config is valid] ********************* failed: [host1.example.org] => {"changed": true, "cmd": ["apache2ctl", "configtest"], "delta": "0:00:00.051874", "end": "2013-03-10 10:50:17.714105", "rc": 1, "start": "2013-03-10 10:50:17.662231"} stderr: Syntax error on line 2 of /etc/apache2/sites-enabled/awesome-app: Invalid command 'RocumentDoot', perhaps misspelled or defined by a module not included in the server configuration stdout: Action 'configtest' failed. The Apache error log may have more information. ...ignoring TASK: [Rolling back - Restoring old default virtualhost] ********************* changed: [host1.example.org] TASK: [Rolling back - Removing our virtualhost] ********************* changed: [host1.example.org] TASK: [Rolling back - Ending playbook] ********************* failed: [host1.example.org] => {"failed": true} msg: Configuration file is not valid. Please check that before re-running the playbook. FATAL: all hosts have already failed -- aborting PLAY RECAP ********************* host1.example.org : ok=7 changed=4 unreachable=0 failed=1
ãã¹ãŠãæ£åžžã«æ©èœããŠããããã§ãã Apacheãåèµ·åããŠã¿ãŸãããã
$ ansible -i step-07/hosts -m service -a 'name=apache2 state=restarted' host1.example.org host1.example.org | success >> { : true, : , : }
ããã§ãApacheã¯æ§æãšã©ãŒããä¿è·ãããŸããã 倿°ã¯ã»ãšãã©ã©ãã§ã䜿çšã§ããããããã®ãã¬ã€ããã¯ã¯ä»ã®å Žåã®Apacheã«äœ¿çšã§ããããšãå¿ããªãã§ãã ããã äžåºŠæžããŠãã©ãã§ã䜿çšããŸãã
Git
Apache, . git .
git
, , , . git
. - . , ansible-pull
.
, . PHP, libapache2-mod-php5
. git
, , , git .
:
... - name: Installs apache web server apt: pkg=apache2 state=installed update_cache=true - name: Installs php5 module apt: pkg=libapache2-mod-php5 state=installed - name: Installs git apt: pkg=git state=installed ...
Ansible . , :
- hosts: web tasks: - name: Updates apt cache apt: update_cache=true - name: Installs necessary packages apt: pkg={{ item }} state=latest with_items: - apache2 - libapache2-mod-php5 - git - name: Push future default virtual host configuration copy: src=files/awesome-app dest=/etc/apache2/sites-available/ mode=0640 - name: Activates our virtualhost command: a2ensite awesome-app - name: Check that our config is valid command: apache2ctl configtest register: result ignore_errors: True - name: Rolling back - Restoring old default virtualhost command: a2ensite default when: result|failed - name: Rolling back - Removing out virtualhost command: a2dissite awesome-app when: result|failed - name: Rolling back - Ending playbook fail: msg="Configuration file is not valid. Please check that before re-running the playbook." when: result|failed - name: Deploy our awesome application git: repo=https://github.com/leucos/ansible-tuto-demosite.git dest=/var/www/awesome-app tags: deploy - name: Deactivates the default virtualhost command: a2dissite default - name: Deactivates the default ssl virtualhost command: a2dissite default-ssl notify: - restart apache handlers: - name: restart apache service: name=apache2 state=restarted
:
$ ansible-playbook -i step-08/hosts -l host1.example.org step-08/apache.yml PLAY [web] ********************* GATHERING FACTS ********************* ok: [host1.example.org] TASK: [Updates apt cache] ********************* ok: [host1.example.org] TASK: [Installs necessary packages] ********************* changed: [host1.example.org] => (item=apache2,libapache2-mod-php5,git) TASK: [Push future default virtual host configuration] ********************* changed: [host1.example.org] TASK: [Activates our virtualhost] ********************* changed: [host1.example.org] TASK: [Check that our config is valid] ********************* changed: [host1.example.org] TASK: [Rolling back - Restoring old default virtualhost] ********************* skipping: [host1.example.org] TASK: [Rolling back - Removing out virtualhost] ********************* skipping: [host1.example.org] TASK: [Rolling back - Ending playbook] ********************* skipping: [host1.example.org] TASK: [Deploy our awesome application] ********************* changed: [host1.example.org] TASK: [Deactivates the default virtualhost] ********************* changed: [host1.example.org] TASK: [Deactivates the default ssl virtualhost] ********************* changed: [host1.example.org] NOTIFIED: [restart apache] ********************* changed: [host1.example.org] PLAY RECAP ********************* host1.example.org : ok=10 changed=8 unreachable=0 failed=0
http://192.168.33.11 .
tags: deploy
. , . , . . , "deploy" â , . , :
$ ansible-playbook -i step-08/hosts -l host1.example.org step-08/apache.yml -t deploy
X11 forwarding request failed on channel 0
PLAY [web] * ****
GATHERING FACTS * ****
ok: [host1.example.org]
TASK: [Deploy our awesome application] * ****
changed: [host1.example.org]
PLAY RECAP * ****
host1.example.org: ok=2 changed=1 unreachable=0 failed=0
-
-. .
inventory
, - , . inventory:
[web] host1.example.org ansible_ssh_host=192.168.33.11 ansible_ssh_user=root host2.example.org ansible_ssh_host=192.168.33.12 ansible_ssh_user=root [haproxy] host0.example.org ansible_ssh_host=192.168.33.10 ansible_ssh_user=root
, ansible_ssh_host
IP, . /etc/hosts
( ).
-
. :
$ ansible-playbook -i step-09/hosts step-09/apache.yml PLAY [web] ********************* GATHERING FACTS ********************* ok: [host2.example.org] ok: [host1.example.org] TASK: [Updates apt cache] ********************* ok: [host1.example.org] ok: [host2.example.org] TASK: [Installs necessary packages] ********************* ok: [host1.example.org] => (item=apache2,libapache2-mod-php5,git) changed: [host2.example.org] => (item=apache2,libapache2-mod-php5,git) TASK: [Push future default virtual host configuration] ********************* ok: [host1.example.org] changed: [host2.example.org] TASK: [Activates our virtualhost] ********************* changed: [host2.example.org] changed: [host1.example.org] TASK: [Check that our config is valid] ********************* changed: [host2.example.org] changed: [host1.example.org] TASK: [Rolling back - Restoring old default virtualhost] ********************* skipping: [host1.example.org] skipping: [host2.example.org] TASK: [Rolling back - Removing out virtualhost] ********************* skipping: [host1.example.org] skipping: [host2.example.org] TASK: [Rolling back - Ending playbook] ********************* skipping: [host1.example.org] skipping: [host2.example.org] TASK: [Deploy our awesome application] ********************* ok: [host1.example.org] changed: [host2.example.org] TASK: [Deactivates the default virtualhost] ********************* changed: [host1.example.org] changed: [host2.example.org] TASK: [Deactivates the default ssl virtualhost] ********************* changed: [host2.example.org] changed: [host1.example.org] NOTIFIED: [restart apache] ********************* changed: [host1.example.org] changed: [host2.example.org] PLAY RECAP ********************* host1.example.org : ok=10 changed=5 unreachable=0 failed=0 host2.example.org : ok=10 changed=8 unreachable=0 failed=0
, , -l host1.example.org
. , -l
. , web
.
web
, , , , : -l firsthost:secondhost:...
.
-, .
ãã¿ãŒã³
haproxy
. , apache. , - haproxy
. ã©ããã£ãŠããã®ïŒ
HAProxy
Ansible Jinja2 , Python. Jinja2- , Ansible'.
, inventory_name , , {{ inventory_hostname }}
Jinja2-. , IP- ethernet- ( Ansible setup
), {{ ansible_eth1['ipv4']['address'] }}
.
Jinja2 , .
templates/
Jinja- . haproxy.cfg.j2
. .j2
, .
global daemon maxconn 256 defaults mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms listen cluster bind {{ ansible_eth1['ipv4']['address'] }}:80 mode http stats enable balance roundrobin {% for backend in groups['web'] %} server {{ hostvars[backend]['ansible_hostname'] }} {{ hostvars[backend]['ansible_eth1']['ipv4']['address'] }} check port 80 {% endfor %} option httpchk HEAD /index.php HTTP/1.0
.
-, {{ ansible_eth1['ipv4']['address'] }}
IP eth1.
. -. [web]
, backend
. . hostvars
, (, IP, ) .
, . , . , [web]
.
HAProxy playbook
æãé£ããéšåã¯åŸãã«ãããŸãã HAproxy :
- hosts: haproxy tasks: - name: Installs haproxy load balancer apt: pkg=haproxy state=installed update_cache=yes - name: Pushes configuration template: src=templates/haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg mode=0640 owner=root group=root notify: - restart haproxy - name: Sets default starting flag to 1 lineinfile: dest=/etc/default/haproxy regexp="^ENABLED" line="ENABLED=1" notify: - restart haproxy handlers: - name: restart haproxy service: name=haproxy state=restarted
, ? : template
. , copy
. haproxy
.
⊠. inventory , . , , , haproxy- -. , .
$ ansible-playbook -i step-10/hosts step-10/apache.yml step-10/haproxy.yml PLAY [web] ********************* GATHERING FACTS ********************* ok: [host1.example.org] ok: [host2.example.org] TASK: [Updates apt cache] ********************* ok: [host1.example.org] ok: [host2.example.org] TASK: [Installs necessary packages] ********************* ok: [host1.example.org] => (item=apache2,libapache2-mod-php5,git) ok: [host2.example.org] => (item=apache2,libapache2-mod-php5,git) TASK: [Push future default virtual host configuration] ********************* ok: [host2.example.org] ok: [host1.example.org] TASK: [Activates our virtualhost] ********************* changed: [host1.example.org] changed: [host2.example.org] TASK: [Check that our config is valid] ********************* changed: [host1.example.org] changed: [host2.example.org] TASK: [Rolling back - Restoring old default virtualhost] ********************* skipping: [host1.example.org] skipping: [host2.example.org] TASK: [Rolling back - Removing out virtualhost] ********************* skipping: [host1.example.org] skipping: [host2.example.org] TASK: [Rolling back - Ending playbook] ********************* skipping: [host1.example.org] skipping: [host2.example.org] TASK: [Deploy our awesome application] ********************* ok: [host2.example.org] ok: [host1.example.org] TASK: [Deactivates the default virtualhost] ********************* changed: [host1.example.org] changed: [host2.example.org] TASK: [Deactivates the default ssl virtualhost] ********************* changed: [host2.example.org] changed: [host1.example.org] NOTIFIED: [restart apache] ********************* changed: [host2.example.org] changed: [host1.example.org] PLAY RECAP ********************* host1.example.org : ok=10 changed=5 unreachable=0 failed=0 host2.example.org : ok=10 changed=5 unreachable=0 failed=0 PLAY [haproxy] ********************* GATHERING FACTS ********************* ok: [host0.example.org] TASK: [Installs haproxy load balancer] ********************* changed: [host0.example.org] TASK: [Pushes configuration] ********************* changed: [host0.example.org] TASK: [Sets default starting flag to 1] ********************* changed: [host0.example.org] NOTIFIED: [restart haproxy] ********************* changed: [host0.example.org] PLAY RECAP ********************* host0.example.org : ok=5 changed=4 unreachable=0 failed=0
. http://192.168.33.10/ . ! HAproxy: http://192.168.33.10/haproxy?stats .
, , . .
Ansible . ansible_ssh_host
inventory, , host_vars
group_vars
.
HAProxy
HAProxy , . , , HAProxy .
( 0 256). , . , .
.
Group-
haproxy group_vars. , haproxy .
group_vars/haproxy
inventory. , . web, group_vars/web
.
haproxy_check_interval: 3000 haproxy_stats_socket: /tmp/sock
. , , - . ( Python dict) :
haproxy: check_interval: 3000 stats_socket: /tmp/sock
. . .
, host_vars
. host_vars/host1.example.com
:
haproxy_backend_weight: 100
host_vars/host2.example.com
:
haproxy_backend_weight: 150
haproxy_backend_weight
group_vars/web
, -:
host_vars
group_vars
.
, .
global daemon maxconn 256 {% if haproxy_stats_socket %} stats socket {{ haproxy_stats_socket }} {% endif %} defaults mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms listen cluster bind {{ ansible_eth1['ipv4']['address'] }}:80 mode http stats enable balance roundrobin {% for backend in groups['web'] %} server {{ hostvars[backend]['ansible_hostname'] }} {{ hostvars[backend]['ansible_eth1']['ipv4']['address'] }} check inter {{ haproxy_check_interval }} weight {{ hostvars[backend]['haproxy_backend_weight'] }} port 80 {% endfor %} option httpchk HEAD /index.php HTTP/1.0
{% if ...
? , . , - haproxy_stats_socket
( --extra-vars="haproxy_stats_sockets=/tmp/sock"
), .
, !
:
ansible-playbook -i step-11/hosts step-11/haproxy.yml
, apache, . . haproxy:
- hosts: web - hosts: haproxy tasks: - name: Installs haproxy load balancer apt: pkg=haproxy state=installed update_cache=yes - name: Pushes configuration template: src=templates/haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg mode=0640 owner=root group=root notify: - restart haproxy - name: Sets default starting flag to 1 lineinfile: dest=/etc/default/haproxy regexp="^ENABLED" line="ENABLED=1" notify: - restart haproxy handlers: - name: restart haproxy service: name=haproxy state=restarted
ã»ã - . . Ansible web
. , haproxy . , Ansible , ansible_eth1
.
!
, , ! . â , . ,
Ansible . â : B A. B, A.
«» Ansible: . , . , , . . "convention over configuration".
:
roles | |_some_role | |_files | | | |_file1 | |_... | |_templates | | | |_template1.j2 | |_... | |_tasks | | | |_main.yml | |_some_other_file.yml | |_ ... | |_handlers | | | |_main.yml | |_some_other_file.yml | |_ ... | |_vars | | | |_main.yml | |_some_other_file.yml | |_ ... | |_meta | |_main.yml |_some_other_file.yml |_ ...
.
main.yml
. , . .
vars
meta
. vars
, , . . , , â . , , â . «» (, ) . . . Ansible .
meta
, . roles
.
Apache
, apache .
:
- apache
- apache
roles/apache/handlers/main.yml
- apache
awesome-app
roles/apache/files/
:
mkdir -p step-12/roles/apache/{tasks,handlers,files}
apache.yml
main.yml
. :
- name: Updates apt cache apt: update_cache=true - name: Installs necessary packages apt: pkg={{ item }} state=latest with_items: - apache2 - libapache2-mod-php5 - git ... - name: Deactivates the default ssl virtualhost command: a2dissite default-ssl notify: - restart apache
, .
apache.yml
tasks:
handlers:
.
files/
templates/
. , Ansible , .
step-12/roles/apache/handlers/main.yml
:
- name: restart apache service: name=apache2 state=restarted
:
cp step-11/files/awesome-app step-12/roles/apache/files/
apache . .
. site.yml
, . haproxy
:
- hosts: web roles: - { role: apache } - hosts: haproxy roles: - { role: haproxy }
. haproxy:
mkdir -p step-12/roles/haproxy/{tasks,handlers,templates} cp step-11/templates/haproxy.cfg.j2 step-12/roles/haproxy/templates/
templates/
.
?:
ansible-playbook -i step-12/hosts step-12/site.yml
, "PLAY RECAP":
host0.example.org : ok=5 changed=2 unreachable=0 failed=0 host1.example.org : ok=10 changed=5 unreachable=0 failed=0 host2.example.org : ok=10 changed=5 unreachable=0 failed=0
, site.yml . -? ! limit-:
ansible-playbook -i step-12/hosts -l web step-12/site.yml
.
( : . ).