Ansibleã解決ããäž»ãªã¿ã¹ã¯ïŒ
- æ§æ管ç ã 説æããæ§æã«å¯Ÿããæéãã€æ£ãããµãŒããŒæ§æã
- ããããã³ã¹ ã æ°ããã¯ã©ãŠããµãŒããŒã®å±éã管çããïŒããšãã°ãAPIãä»ããŠãDockerãŸãã¯LXCã䜿çšïŒã
- å±é ããŠã³ã¿ã€ã ãªãã§ã¢ããªã±ãŒã·ã§ã³ãæé©ãªæ¹æ³ã§ã€ã³ã¹ããŒã«ããã³æŽæ°ããŸãã
- ãªãŒã±ã¹ãã¬ãŒã·ã§ã³ ã å±éã®ããã«ã€ã³ãã©ã¹ãã©ã¯ãã£ã³ã³ããŒãã³ãã調æŽããŸãã ããšãã°ããµãŒããŒäžã®ãœãããŠã§ã¢ãã¢ããã°ã¬ãŒãããåã«ãWebãµãŒããŒãããŒããã©ã³ãµãŒããåæãããŠããããšã確èªããŸãã
- ç£èŠãšéç¥ ã
- ãã®ã³ã° éäžãã°åéã
ä»ã®äžè¬çãªITã€ã³ãã©ã¹ãã©ã¯ãã£èªååããŒã«ãšæ¯èŒããŠãAnsibleã¯ãµãŒãã¹å¯Ÿè±¡ãµãŒããŒã«ã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³ãã€ã³ã¹ããŒã«ããå¿ èŠããªããããã€ã³ãã©ã¹ãã©ã¯ãã£ãå±éããåã®ã»ããã¢ããæéãççž®ã§ããŸãã åäœããããã«ãAnsibleã¯SSHçµç±ã§ãµãŒãã¹å¯Ÿè±¡ãµãŒããŒã«æ¥ç¶ããŸãã
ãã®ãããªããŒã«ã®éèŠæ§ã¯ãå¿ èŠãªãµãŒããŒãè¿ éã«äœæããå¿ èŠãªãœãããŠã§ã¢ãå±éããå¿ èŠããªããªã£ããšãã«äœ¿çšããã³åé€ãã䜿çšãããªãœãŒã¹ã®ã¿ãæ¯æãæ©èœã®åºçŸã«ãããã¯ã©ãŠãã§ã®ã¿å¢å ããŸãã ãã®èšäºã§ã¯ã InfoboxCloudã®ã¯ã©ãŠããµãŒããŒã§ã®å®çšçãªã³ã³ããã¹ãã§ã®åºæ¬çãªAnsibleæ©èœã«ã€ããŠèª¬æããŸãã
æ§æããå¿ èŠããããã®
InfoboxCloudã®ã¢ã«ãŠã³ããæ¢ã«ãæã¡ã§ããããšãé¡ã£ãŠããŸãã ãŸã äœæããŠããªãå Žåã¯ã äœæããŸã ã
Ansibleãæ©èœãããã«ã¯ã管çãµãŒããŒãå¿ èŠã§ãã äœæããŸãïŒUbuntu 14.04ãŸãã¯CentOS 7ã䜿çšããããšããå§ãããŸãïŒã ãŸããAnsibleã䜿çšããŠæ§æããLinuxãµãŒããŒãå°ãªããšã2ã€äœæããŸãã ãµãŒããŒã«ã¢ã¯ã»ã¹ããããã®ããŒã¿ãã¡ãŒã«ã«éä¿¡ãããŸãã
SSHãä»ããŠç®¡çãµãŒããŒã«æ¥ç¶ããŸãã
Ansibleãã€ã³ã¹ããŒã«ãã
Ubuntu 14.04 LTS
管çãµãŒããŒã«ã€ã³ã¹ããŒã«ããã«ã¯ã次ãå ¥åããŸãã
apt-key update && apt-get update && apt-get -y upgrade && apt-get -y install python-software-properties && apt-get -y install software-properties-common && apt-add-repository -y ppa:rquillo/ansible && apt-get update && apt-get -y install ansible
CentOS 7
管çãµãŒããŒã«ã€ã³ã¹ããŒã«ããã«ã¯ã次ãå ¥åããŸãã
yum -y update && yum -y install epel-release && yum -y install ansible
Ansibleã®ä»çµã¿
Ansibleã®äž»ãªã¢ã€ãã¢ã¯ã1ã€ãŸãã¯è€æ°ã®å¶åŸ¡ãµãŒããŒã®ååšã§ãããã®ãµãŒããŒãããã³ãã³ããŸãã¯ã·ãŒã±ã³ã·ã£ã«åœä»€ïŒãã¬ã€ããã¯ïŒã®ã»ããããªã¢ãŒããµãŒããŒã«éä¿¡ããSSHãä»ããŠæ¥ç¶ã§ããŸãã
ãã¹ãã€ã³ãã³ããªãã¡ã€ã«ã«ã¯ãã³ãã³ããå®è¡ããããµãŒããŒã«é¢ããæ å ±ãå«ãŸããŠããŸãã Ansible æ§æãã¡ã€ã«ã¯ãç°å¢ã®èšå®ãæå®ããã®ã«åœ¹ç«ã¡ãŸãã
åœä»€ã»ããïŒãã¬ã€ããã¯ïŒã¯ãç¹å®ã®ç¶æ³ã§å¿ èŠã«ãªãå¯èœæ§ã®ããAnsibleã«ãŒãã«ã¢ãžã¥ãŒã«ãŸãã¯ãµãŒãããŒãã£ã¢ãžã¥ãŒã«ã®æ©èœã䜿çšããŠèª¬æããã1ã€ä»¥äžã®ã¿ã¹ã¯ã§æ§æãããŸãã åœä»€ã»ããèªäœã¯ãæ¡ä»¶ãã§ãã¯ãå«ãããšãã§ããåœä»€ã®é£ç¶ã»ããã§ããæ¡ä»¶ãæºããããªãå Žåãç¹å®ã®åœä»€ãã¹ãããã§ããŸãã
Ansible APIã䜿çšããŠã¹ã¯ãªãããå®è¡ããããšãã§ããŸã ã ã©ãããŒã¹ã¯ãªããã§ãã¬ã€ããã¯ãå®è¡ããå¿ èŠãããå Žåã¯ãAPIã䜿çšããŠå®è¡ã§ããŸãã ãã¬ã€ããã¯èªäœã¯ã YAML圢åŒã§å®£èšçã«èšè¿°ãããŠããŸãã Ansibleã¯ãæ°ããã¯ã©ãŠããµãŒããŒãå±éãã ããŒã«ã«åºã¥ããŠæ§æããã·ããªãªããµããŒãããŠããŸã ã äœæ¥ã®äžéšã¯ç®¡çãµãŒããŒã§ããŒã«ã«ã¢ãŒãã§å®è¡ã§ããæ®ãã¯æåã®èµ·ååŸã«äœæããããµãŒããŒã§å®è¡ã§ããŸãã InfoboxCloudã®ããããžã§ãã³ã°ã¢ãžã¥ãŒã«ã®äœæ¥ãé²è¡äžã§ã ã
Ansible Setup
èšå®ãã¡ã€ã«ã¯INI圢åŒã§èšè¿°ãããŠããŸã ã ãã¬ã€ããã¯ãªãã·ã§ã³ãŸãã¯ç°å¢å€æ°ã®èšå®ã®äžéšãŸãã¯ãã¹ãŠãäžæžãã§ããŸãã
ã³ãã³ããå®è¡ãããšãAnsibleã¯æ¬¡ã®å Žæã«æ§æãã¡ã€ã«ãååšãããã©ããã確èªããŸãã
- ç°å¢å€æ°ANSIBLE_CONFIGããã§ãã¯ãããæ§æãã¡ã€ã«ãæãããšãã§ããŸãã
- ./ansible.cfg-çŸåšã®ãã£ã¬ã¯ããª
- ã/ .ansible.cfg-ããŒã ãã£ã¬ã¯ããªå
- /etc/ansible/ansible.cfg-ããã±ãŒãžãããŒãžã£ãŒã«ããansibleã®ã€ã³ã¹ããŒã«äžã«çæããããã£ã¬ã¯ããªå ã
ç°å¢å€æ°ã«ããèšå®
ã»ãšãã©ã®æ§æãã©ã¡ãŒã¿ãŒã¯ãæ§æãã©ã¡ãŒã¿ãŒã®ååã®åã«æ¥é éšANSIBLE_ã䜿çšããŠïŒå€§æåã§ïŒç°å¢å€æ°ãä»ããŠèšå®ã§ããŸãã
äŸïŒ
export ANSIBLE_SUDO_USER = root
ãã®åŸãå€æ°ANSIBLE_SUDO_USERããã¬ã€ããã¯ã§äœ¿çšã§ããŸãã
ansible.cfgã§ã®èšå®
Ansibleæ§æãªãã·ã§ã³ã¯å€æ°ãããŸãã ãããã®ããã€ããèŠãŠã¿ãŸãããïŒ
- hostfile ïŒãã®ãã©ã¡ãŒã¿ãŒã¯ãAnsibleãæ¥ç¶ã§ãããã¹ãã¢ãã¬ã¹ã®ãªã¹ããå«ãã€ã³ãã³ããªãã¡ã€ã«ãžã®ãã¹ãæããŸã ã
äŸïŒ hostfile = / etc / ansible / hosts - library ïŒAnsibleã¢ãžã¥ãŒã«ãä¿åãããŠãããã£ã¬ã¯ããªãžã®ãã¹ã äŸïŒ library = / usr / share / ansible
- forks ïŒAnsibleãçæã§ããããã»ã¹ã®æ°ã ããã©ã«ãã§ã¯ã5ã€ã®ããã»ã¹ãã€ã³ã¹ããŒã«ãããŸãã
äŸïŒ forks = 5 - sudo_user ïŒAnsibleããªã¢ãŒããµãŒããŒã§ã³ãã³ããå®è¡ããããã©ã«ããŠãŒã¶ãŒã
äŸïŒ sudo_user = root - remote_port ïŒSSHæ¥ç¶ã®ããŒãïŒããã©ã«ãã¯22ïŒã
äŸïŒ remote_port = 22 - host_key_checking ïŒãã®ãã©ã¡ãŒã¿ãŒã䜿çšãããšããã¹ãäžã®SSHããŒã®æ€èšŒãç¡å¹ã«ã§ããŸãã ããã©ã«ãã§ã¯ãæ€èšŒãå®è¡ãããŸãã
äŸïŒ host_key_checking = False - timeout ïŒSSHçµç±ã®æ¥ç¶è©Šè¡ã®ã¿ã€ã ã¢ãŠãå€ã
äŸïŒ ã¿ã€ã ã¢ãŠã= 60 - log_path ïŒãã°ãã¡ã€ã«ãä¿åãããã¹ã ããã©ã«ãã§ã¯ãAnsibleã¯ãããããŸã£ããä¿åããŸãããããã®ãã©ã¡ãŒã¿ãŒãæå®ããããšã§ãã°ãæå¹ã«ã§ããŸãã
äŸïŒ log_path = /var/log/ansible.log
æåã®Ansibleèšå®ãã¡ã€ã«ãæžããŸã
InfoboxCloudã§æåã®Ansibleæ§æãã¡ã€ã«ãäœæããŸãããã Ansibleãã€ã³ã¹ããŒã«ãããŠããäœææžã¿ã®ç®¡çãµãŒããŒã«SSHã§æ¥ç¶ããŸãã ãansibleãå®éšçšã®ãã£ã¬ã¯ããªãäœæããããã²ãŒãããŸãã
mkdir ~/ansible cd ~/ansible
ãŸããAnsibleã¢ãžã¥ãŒã«ãä¿åãããã©ã«ããŒãšãã°ãä¿åãããã©ã«ããŒãäœæããŸãã
mkdir ~/ansible/modules mkdir ~/ansible/logs
次ã®å 容ã§ansible.cfgãã¡ã€ã«ãäœæããŸãã
[defaults] hostfile = ~/ansible/inventory sudo_user = root log_path = ~/ansible/logs/ansible.log
ãã¹ãã€ã³ãã³ããªã§ãµãŒãã¹å¯Ÿè±¡ã®ãµãŒããŒãæå®ããŸã
å®éšã®ããã«ã以åã«æ§æãããµãŒããŒãããã€ãäœæããŸããã Ansibleã«äœæãäŒããã°ã«ãŒãåããå¿ èŠããããŸãã ãããè¡ãã«ã¯ãã/ ansible / inventoryãã£ã¬ã¯ããªã«æ¬¡ã®å 容ã®ã€ã³ãã³ããªãã¡ã€ã«ãäœæããŸãã
[experiments] ip__ ip__
ãµãŒããŒã®ip_addressesã¯ã InfoboxCloudã³ã³ãããŒã«ããã«ã§è¡šç€ºã§ããŸãã
åãå°åã®ãµãŒããŒã§Ansible管çãµãŒããŒã䜿çšããã«ã¯ãããŒã«ã«IPã¢ãã¬ã¹ãæå®ããŠãå éšãããã¯ãŒã¯ã§äœæ¥ã§ããããšã«æ³šæããŠãã ããã
ã«ã¹ã¿ã ãµãŒããŒãžã®ã¢ã¯ã»ã¹ã«äœ¿çšããã管çãµãŒããŒã§ããŒãçæããå¿ èŠããããŸãã
ããã¯ã次ã®ã³ãã³ãã䜿çšããŠå®è¡ãããŸãã
ssh-keygen
ãã¹ãŠã®è³ªåã«ã€ããŠã¯ãEnterããŒãæŒãã ãã§ãã
次ã«ãå ¬ééµãã«ã¹ã¿ã ãµãŒããŒã«ã³ããŒããå¿ èŠããããŸãã ããã¯ãåã«ã¹ã¿ã ãµãŒããŒã®Ansible管çãµãŒããŒããssh-copy-idãŠãŒãã£ãªãã£ã䜿çšããŠå®è¡ã§ããŸãã
ssh-copy-id root@ip___
SSHãä»ããŠç®¡çè ããã«ã¹ã¿ã ãµãŒããŒã«ãã°ã€ã³ããããšã«ãããæ£åœæ§ã確èªã§ããŸãã ãã¹ã¯ãŒããèŠæ±ãããªããªã£ãå Žåããã¹ãŠãæ£åžžã§ãã
InfoboxCloudã§ã¯ ãå ¬éããŒãæ¢ã«æå®ãããŠããæ°ãããµãŒããŒãäœæã§ããŸãã ãããè¡ãã«ã¯ãã¯ãªãŒã³ãªãµãŒããŒãäœæããŸãã äžèšã®ããã«ãå ¬éSSHããŒãã³ããŒããŸãã 次ã«ãOSã€ã¡ãŒãžãäœæããŸãã
次ã«ãã³ã³ãããŒã«ããã«ã®[ãµãŒããŒã€ã¡ãŒãž]ã»ã¯ã·ã§ã³ã§ãå¿ èŠã«å¿ããŠãã€ã¡ãŒãžã®[ãµãŒããŒã®äœæ]ãã¯ãªãã¯ããAnsibleã®æ§æã®æºåãæŽããŸãã
Ansibleãå®å šã«æ§æãããŠããããšã確èªããŸãããã
ãµãŒãã¹å¯Ÿè±¡ã®ãµãŒããŒã«pingãå®è¡ã§ããŸãã
ansible experiments -m ping
ãŸãã¯ãHello Worldãããšã³ãŒããããã«éä¿¡ããŸãã
ansible experiments -a "/bin/echo Hello, World!"
æ§æ管ç
ãã¬ã€ããã¯ã§ã®äœæ¥
Playbookã®å®è¡ã¯ãAnsibleã®ã³ã¢ã¿ã¹ã¯ã®1ã€ã§ãã ãã¬ã€ããã¯ã«ã¯ã¿ã¹ã¯ãªã¹ããå«ãŸããŠããŸãã Ansibleå ã®åã¿ã¹ã¯ã¯ãã³ãŒãã¢ãžã¥ãŒã«ã䜿çšããŸãã ãã¬ã€ããã¯èªäœã¯YAML圢åŒã§èšè¿°ãããŠããŸãããã¢ãžã¥ãŒã«ã¯ä»»æã®ããã°ã©ãã³ã°èšèªã§äœæã§ããŸãã ã¢ãžã¥ãŒã«ããã®ã¡ãã»ãŒãžåœ¢åŒãJSONã§ããããšãéèŠã§ãã
ã€ã ã«
ãã¬ã€ããã¯ã¯YAMLã§æžãããŠããŸãã YAMLãã¡ã€ã«ãæžãããã®åºæ¬çãªã«ãŒã«ãèŠãŠã¿ãŸãããã
Ansibleã®å Žåãã»ãšãã©ãã¹ãŠã®YAMLãã¡ã€ã«ã¯ãªã¹ãã§å§ãŸããŸãã åãªã¹ãé ç®ã¯ãããŒãšå€ã®ãã¢ã®ãªã¹ãã§ãããå€ãã®å ŽåãèŸæžãšåŒã°ããŸãã
ãã¹ãŠã®YAMLãã¡ã€ã«ã¯ã---ãã§å§ãŸãå¿ èŠããããŸãã ããã¯YAML圢åŒã®äžéšã§ãããããã¥ã¡ã³ãã®å§ãŸãã瀺ããŸãã
ãªã¹ãã®ãã¹ãŠã®ã¡ã³ããŒã¯ãè¡ã®å é ããã€ã³ãã³ãããå¿ èŠããããã¹ããŒã¹ãŸãã¯ã-ãã§å§ãŸãå¿ èŠããããŸãã ã³ã¡ã³ãã¯ãïŒãã§å§ãŸããŸãã
äŸïŒ
--- #Message - Hosting â Cloud
èŸæžã¯ããããŒïŒãïŒã³ãã³ãšã¹ããŒã¹ïŒãå€ãã®åœ¢åŒã§è¡šç€ºãããŸãã
--- #Message site: habr blog: infobox
å¿ èŠã«å¿ããŠãèŸæžãç瞮圢ã§è¡šç€ºã§ããŸãã
--- #Comment {site: habr, blog: infobox}
次ã®ããã«è«çå€ïŒtrue / falseïŒãæå®ã§ããŸãã
--- need_access: no use_service: yes file_conf: TRUE read_value: True kill_process: false
YAMLãã¡ã€ã«ã®äŸå šäœã¯æ¬¡ã®ããã«ãªããŸãã
--- #About blog site: habr blog: infobox must_read: True themes: - hosting - cloud - it - geeks brands: - infobox - infoboxcloud
å€æ°ã®å ŽåãAnsibleã¯ã{{var}}ãã䜿çšããŸãã ã³ãã³ã®åŸã®å€ãã{ãã§å§ãŸãå ŽåãYAMLã¯äœãèšãããèããŸãã
å€æ°ã䜿çšããã«ã¯ãæ¬åŒ§ãåŒçšç¬Šã§å²ãå¿ èŠããããŸãã
word: "{{ variable }}"
ããã§ããã¬ã€ããã¯ã®äœæãéå§ã§ããŸãã
æåã®ãã¬ã€ããã¯ãæžã
Playbookã¯ãæäŸããããµãŒããŒããŠãŒã¶ãŒå€æ°ãã¿ã¹ã¯ããã³ãã©ãŒïŒãã³ãã©ãŒïŒãªã©ã®ãªã¹ãã§æ§æã§ããŸãã ã»ãšãã©ã®æ§æèšå®ã¯ããã¬ã€ããã¯ã§ãªãŒããŒã©ã€ãã§ããŸãã åãã¬ã€ããã¯ã¯ããªã¹ãå ã®1ã€ä»¥äžã®ã¢ã¯ã·ã§ã³ïŒã²ãŒã ïŒã§æ§æãããŠããŸãã
ã²ãŒã ã®ç®æšã¯ããã¹ãã®ã°ã«ãŒããäºåå®çŸ©ãããããŒã«ã«é¢é£ä»ããAnsibleã¿ã¹ã¯ãžã®ãã£ã¬ã³ãžãšããŠæ瀺ããããšã§ãã
äŸãšããŠãnginxã®ã€ã³ã¹ããŒã«ããã»ã¹ãèŠãŠã¿ãŸãããã
ãã¬ã€ããã¯ãä¿åããããã£ã¬ã¯ããªãäœæããŸãã
mkdir ~/ansible/playbooks
次ã®å 容ã䜿çšããŠãplaybooksãã£ã¬ã¯ããªã«setup_nginx.ymlãã¡ã€ã«ãäœæããŸãã
--- - hosts: experiments tasks: - name: Install nginx package apt: name=nginx update_cache=yes sudo: yes - name: Starting nginx service service: name=nginx state=started sudo: yes
å 容ãèŠãŠã¿ãŸãããã
- hosts ïŒã¿ã¹ã¯ãå®è¡ãããã¹ããŸãã¯ã°ã«ãŒãã®ãªã¹ãã ãã®ãã£ãŒã«ãã¯å¿
é ã§ããã圹å²ãé€ãããã¹ãŠã®ãã¬ã€ããã¯ã«å¿
èŠã§ãã ãã¹ãã°ã«ãŒããæå®ãããŠããå ŽåãAnsibleã¯æåã«ãã¬ã€ããã¯ã§æ¢ãã次ã«ã€ã³ãã³ããªãã¡ã€ã«ã§æ¢ããŸãã ã³ãã³ãïŒ
ansible-playbook --list-host, â playbook (playbooks/setup_nginx.yml).
ã«ãã£ãŠãã©ã®ãã¹ããäœæ¥ãè¡ããã調ã¹ãããšãã§ããŸãansible-playbook --list-host, â playbook (playbooks/setup_nginx.yml).
ansible-playbook --list-host, â playbook (playbooks/setup_nginx.yml).
ã¿ã¹ã¯ ïŒã¿ã¹ã¯ã ãã¹ãŠã®ãã¬ã€ããã¯ã«ã¯ã¿ã¹ã¯ãå«ãŸããŠããŸãã ã¿ã¹ã¯ã¯ãå®è¡ããã¢ã¯ã·ã§ã³ã®ãªã¹ãã§ãã ã¿ã¹ã¯ãã£ãŒã«ãã«ã¯ãã¿ã¹ã¯ã®ååïŒãã¬ã€ããã¯ãŠãŒã¶ãŒã®ã¿ã¹ã¯ã«é¢ããåç §æ å ±ïŒãå®è¡ããã¢ãžã¥ãŒã«ãããã³ã¢ãžã¥ãŒã«ã«å¿ èŠãªåŒæ°ãå«ãŸããŸãã ãnameããã©ã¡ãŒã¿ãŒã¯ãªãã·ã§ã³ã§ãããæšå¥šãããŸãã
æåããŸããïŒ