çµæ§åã«ansibleã®å®è¡æã®ãªãã·ã§ã³ãé·ããã¦è¦ããããã工夫ãããã®ãã©ãã«ãè³æãæ®ãã¦ãªãã£ãã®ã§ãã¾ã¨ãã¦ã¿ã¾ããã
ããã¾ã§åãåã£ãä¸ã¤ã®æ段ã§ããããã¹ããã©ã¯ãã£ã¹ã ã¨ã¯æã£ã¦ãªãã§ãããå°ãã§ã誰ãã®å½¹ã«ç«ã¦ã°ã¨æãæ®ãã¦ããã¾ãã
ansibleã£ã¦ï¼
ãµã¼ãã®æ§æã管çããããã®ãã¼ã«ã§ããããã使ããã¨ã§ã¢ããªã±ã¼ã·ã§ã³ãå®è¡ããããã®è¨å®ãªã©ãã³ã¼ãã§ç®¡çããè¨å®ãèªååãããã¨ãã§ãã¾ãã
è¨å®ãã³ã¼ãã§ç®¡çãèªåååºæ¥ããã¨ã®ã¡ãªããã¨ãã¦ã¯ããµã¼ãã®è¨å®é ç®ã«å¯¾ãã¦ã¬ãã¥ã¼ãæããã¨ããããããªã誤ã£ãè¨å®ãªã©ãé²ãã ããæ¥é½ãµã¼ããå¢ããï¼ã¹ã±ã¼ã«ã¢ã¦ãï¼å¯¾å¿ãå¿ è¦ã«ãªã£ãã¨ããªã©ã«ãã対å¿ãããã¨ãã§ããããªã©ãããããã¾ãã
ansibleã®ã³ãã³ãé·ãåé¡
ä»åã®è¨äºã§ã¯ansibleã®ããã®ç´°ããæ¸ãæ¹ãªã©ã«ã¤ãã¦ã¯è¨è¼ãã¾ãããï¼ä»ã®ãµã¤ããå ¬å¼ã§ååãã£ã±ãè¨äºãããã¾ãã®ã§ããã¡ããè¦ã¦ãã ãããï¼
ansibleãæ¸ãã¦ããã¨å®è¡ã³ãã³ããä¸è¨ã®ããã«ãªãã·ã§ã³ããã£ã±ãã¤ãã¦é·ããªããã¨ã¯ãªãã ãããï¼
ansible-playbook --vault-password-file $(VAULT_PASS) -i ec2.py -l tag_Environment_${ENV} --private-key=$(KEY) site.yml -u ${user} --check
ä¸è¨ã¯ç§ã使ã£ã¦ãç°å¢ã®ãããã¸ã§ãã³ã°ç¨ã®ã³ãã³ãã§ãããããé·ããã¦è¦ãããã¾ããã ä¸å¿è§£èª¬ãã¦ããã¨ã
- --vault-password-file $(VAULT_PASS) ã¯ãã¡ã¤ã«ãæå·åãã¦ç®¡çãã¦ããã¨ãã«è¤ååãã¦ãããã¸ã§ãã³ã°ãå®è¡ããããã®éµã¨ãªããã¡ã¤ã«ãæå®ãããªãã·ã§ã³
- -i ec2.py ã¯ãã¤ãããã¯ã¤ã³ãã³ããªã¨ããä»çµã¿ã使ãããã®ãªãã·ã§ã³ã§ããããã¸ã§ãã³ã°å¯¾è±¡ãåçã«å¤åããéã«ä½¿ç¨ãã
- -l tag_Environment_${ENV} ã¯ãããã¸ã§ãã³ã°ãå®è¡ãããã¹ãã®ãã£ã«ã¿ãªã³ã°ãªãã·ã§ã³
- --private-key=$(KEY) ã¯ãããã¸ã§ãã³ã°ã®éã«ä½¿ç¨ããsshç¨ã®éµã®ãªãã·ã§ã³
- -u ${user} ã¯ãããã¸ã§ãã³ã°ã®éã«ä½¿ç¨ããsshç¨ã®ã¦ã¼ã¶ã®ãªãã·ã§ã³
- --check ã¯dry-runã®ããã®ãªãã·ã§ã³ã§ããããã¤ãã¦å®è¡ããã¨å®éã«ã¯ãããã¸ã§ãã³ã°ããã«å®è¡ããã¨ãã®çµæããã¹ããããã¨ãã§ãã¾ã
ãã¡ããä¸ã¤ä¸ã¤ã®æå³ã¯ç解ãã¦ããã®ã§ãæãåºããªããæ¸ãã°å®è¡ã§ããªããã¨ããªãã§ãããããé »ç¹ã«å®è¡ãããã®ã§ããªãã£ããããã®ã§ãä¹ ãã¶ãã«ããã¼ãããã¸ã§ãã³ã°ããããã£ã¦ããã¨ãã«ããããã³ãã³ããªãã ã£ãï¼ãã¨ãªããã¨ãå¤ãã§ãã
ãã¡ããå¿ãã¦ã大ä¸å¤«ãªããã«ã³ãã³ããã©ããã«ã¡ã¢ãã¦ãããããREADMEã«æ¸ãã¦ããã¦ãããã¨æãã®ã§ããããããæ¯åè¦ããã³ãããã¦å®è¡ããã®ããªãã ãã¤ã±ã¦ãªãã§ãããã
対çåè£
ä¸è¨ã®è§£æ±ºçã¨ãã¦ãä¸è¨ãèãã¾ããã 1. Alias 1. ã·ã§ã«ã¹ã¯ãªãã 1. Makefile
Alias
ä¸çªæåã«æãã¤ããã®ãAliasã§ãããé·ã£ããããã³ãã³ãã«å¯¾ãã¦Aliasãå¼µãã¨ããã®ã¯ããããã¾ãããã ã§ããä¸è¨ã®çç±ãããAliasã§ãããã¨ãæ念ãã¾ããã
- 1å°ã®ansibleçºå°ãµã¼ãããè¤æ°ãµã¼ãã¹ã®ansibleãå®è¡ããéã«ãªãã·ã§ã³ãããããéãããaliasã§ã¯å¯¾å¿ããããªã
- ãã¤ãããã¯ã¤ã³ãã³ããªã使ãããã®ec2.pyã«ã³ãã³ããä¾åãã¦ãããããã®ä¾å解決ãã§ããªã
1å°ã®ansibleçºå°ãµã¼ãããè¤æ°ãµã¼ãã¹ã®ansibleãå®è¡ããéã«ãªãã·ã§ã³ãããããéãããaliasã§ã¯å¯¾å¿ããããªã
ç§ã®è§¦ã£ã¦ãç°å¢çãªåé¡ã§ãããããããã¾ããããè¤æ°ã®ãµã¼ãã¹ã触ã£ã¦ã¦ããããã«å¯¾ãã¦ansibleãããç¶æ ã§ããã¤ãã®å®è¡ãä¸å°ã®ansibleå®è¡ç¨ç°å¢ãããããã¸ã§ãã³ã°ãããã¦ããããã aliasã«ç»é²ããã¨ä»ã®ãµã¼ãã¹ã®ãããã¸ã§ãã³ã°ã®ã³ãã³ãã¨ãããã£ã³ã°ãã¦ãã¾ãã¾ãããã¡ãããããaliaså¼µãååã«ãµã¼ãã¹åãªã©ãã¤ãã¦å¯¾å¿ãã¦ãããã®ã§ãããç ©éã«ãªãããã ã£ãã®ã§ããã¾ããã
ãã¤ãããã¯ã¤ã³ãã³ããªã使ãããã®ec2.pyã«ã³ãã³ããä¾åãã¦ãããããã®ä¾å解決ãã§ããªã
ããã«ã¤ãã¦ãç§ã®ç°å¢ã«ãããã®ã§ãããã®ã§ããããã¤ãããã¯ã¤ã³ãã³ããªã使ç¨ãã¦ããããã®è§£æ±ºã«awsã®ec2.pyã使ç¨ãã¦ãã¾ããããã«ã¤ãã¦ã¯å ¬å¼ã§ãæ¸ããã¦ããã®ã§è©³ç´°ã¯ãã¡ãã確èªãã¦ãã ããã
Working With Dynamic Inventory — Ansible Documentation
åé¡ã¨ãã¦ã¯ããã®ãããã¸ã§ãã³ã°ã³ãã³ããå®è¡ããéã«ãããããec2.pyãåå¾ãã¦ãããã¨ãå¿ è¦ãªã®ã§ãããç°å¢ã®ã»ããã¢ããã¯ãã£ããã³ã¼ãã§ç®¡çã§ããããã«ãªã£ãã®ã«ãç°å¢ã»ããã¢ããããããã®æé ãã³ã¼ãã§ç®¡çã§ãã¦ããªãã®ã§ã¯ãæå³ããªããããªãããã¨ããã®ãç§ã®æè¦ã§ãããªã®ã§ãã®ec2.pyã«å¯¾ãã¦ãä½ããã®å½¢ã§åå¾ãã¦ãããã¨ãå¿ è¦ã ã¨ãããã¨ãã³ã¼ãã§è¡¨ç¾ããããªã¨æããaliasã¯å´ä¸ã«ãªãã¾ããã
ã·ã§ã«ã¹ã¯ãªãã
次ã«æãã¤ããã®ãã·ã§ã«ã¹ã¯ãªããã§ããã¾ããããã¤ãã®æé ããã£ã¦ãããèªååãããã¨ãªãã¨ãã«ã¹ã¿ãã¤ãºæ§ã®é«ãã·ã§ã«ã¹ã¯ãªãããåè£ã«ä¸ããã®ã¯å½ç¶ãã¨æãã¾ãã ã·ã§ã«ã¹ã¯ãªããã§ããã°ãµã¼ãã¹ãã¨ã®ansibleã®ã¬ãã¸ããªã«ã¹ã¯ãªãããå«ãã¦å®è¡ããããã«ããã°ãaliasã®ããã«ãããã£ã³ã°ãã¦ãã¾ãåé¡ãé²ãã¾ãã
çµè«ããè¨ãã¨ãç§ã¯ã·ã§ã«ã¹ã¯ãªãããé¸ã³ã¾ããã§ããããã¡ããã·ã§ã«ã¹ã¯ãªããã§ã§ããªãããã§ã¯ããã¾ããããååã«ãããªé¸æè¢ã§ã¯ããã¨æãã¾ãã ãã ãèªåãã·ã§ã«ã¹ã¯ãªãããæ¸ãã»ã©ã®ãã®ã§ã¯ãªãããã¤ã·ã§ã«ã¹ã¯ãªããæ¸ãã®ãããã©ãããã¨æã£ã¦ãã¾ã£ãããã«å´ä¸ãã¾ããã
Makefile
Makefileã§ããæ®æ®µãã¾ã触ã£ã¦ãªã人ããããã¨ãããMakefileï¼ãã¨ãªãããããã¾ãããã§ãããèªåã¯Makefileãçµæ§æ°ã«å ¥ã£ã¦ãã¾ããMakefileã®ããç¹ã¨ãã¦ã¯ãä¾å解決ãã§ããç°¡åãªã³ãã³ãã®ã©ãããæ¸ãããããããã«å°½ãããã¨æãã¾ããä»åã®ç®çã§ããã¨ãansibleã®é·ããªãã·ã§ã³ãã©ãããããµã¼ãã¹ãã¨ã«å¾®å¦ã«éããªãã·ã§ã³ããµã¼ãã¹ãã¨ã«Makefileãæ¸ããã¨ã§è§£æ±ºããec2.pyã®ä¾å解決ãç°¡åã«è¡¨ç¾ãããã¨ãã§ãã¾ãã
å ·ä½çã«ã©ãããã
æçµçã«ä¸è¨ã®ãããªMakefileããµã¼ãã¹ã®ansibleã¬ãã¸ããªã«å«ããããã«ãã¾ããã
init: ec2.py ec2.ini .PHONY: init ec2.py: wget https://raw.github.com/ansible/ansible/devel/contrib/inventory/ec2.py chmod +x ec2.py ec2.ini: @echo 'please set up your credential ec2.ini' exit -1 VAULT_PASS=./default/path ENV=default-env KEY=default-key USER=default-user dry-run: init ansible-playbook --vault-password-file $(VAULT_PASS) -i ec2.py -l tag_Environment_${ENV} --private-key=$(KEY) site.yml -u ${USER} --check .PHONY: dry-run run: init ansible-playbook --vault-password-file $(VAULT_PASS) -i ec2.py -l tag_Environment_${ENV} --private-key=$(KEY) site.yml -u ${USER} .PHONY: run MODE=encrypt/decrypt TARGET=your_target_file vault: ansible-vault $(MODE) --vault-password-file $(VAULT_PASS) $(TARGET) .PHONY: vault encrypt: @$(MAKE) vault MODE=encrypt .PHONY: encrypt decrypt: @$(MAKE) vault MODE=decrypt .PHONY: decrypt
ãã®Makefileããããã£ã¬ã¯ããªä¸ã§ make dry-run
ã®ããã«å®è¡ããã¨ååå®è¡æ㯠init
ã®ã¿ã¼ã²ãããå®è¡ãããã®ã§ec2.pyã¯èªåã§DLãããec2.iniã«ã¯ã¬ãã³ã·ã£ã«æ
å ±ãè¨å
¥ãããã¨ããæ¨ã®ã¡ãã»ã¼ã¸ãåºãã¦çµäºãã¾ããã¾ããec2.iniãã¿ã¼ã²ããã«ãªã£ã¦ããã®ã§ãã®ãã¡ã¤ã«ãçæãããã¾ã§ãã®ã¨ã©ã¼ã¯è§£æ¶ãããªãã®ã§ãä½æããã¦ããªãã®ã«ansibleãå®è¡ããããã¨ã¯ããã¾ããã
ã¾ãããã¾ãã§ãã¡ã¤ã«ã®æå·åã®ã³ãã³ããåã¯è¦ããããªãã®ã§ãmake ã®ã¿ã¼ã²ããã«ãã¦ã¿ã¾ãããããããæãã§ã³ãã³ãã®ã©ããã¨ãã¦ã使ããå¿ããã¨ãã¦ã make ã®è£å®ãªã¹ããããä½ãã§ããããå¤æã§ããã®ã§ã便å©ã§ããã
æ°ãã人ãããã¸ã§ã¯ãã«å ¥ã£ã¦ããã¨ãã¦ããã使ãã³ãã³ããmakeã«ã¾ã¨ã¾ã£ã¦ããã¨ã³ãã³ãã®èª¬æãç´°ããããªãã¦ãç解ãã¦ããããã®ã§ã¨ã¦ãå©ãã£ã¦ã¾ãã
(æ¬å½ã¯ãå ¬å¼ã§ansibleã®å®è¡ãªãã·ã§ã³ããã¡ã¤ã«ã§è¨è¿°ã§ããã°ä¸çªãããã ãããã©ãã