ansible
20210727 (화) ad-hoc 명령어 및 playbook 기초
gusalstm
2021. 7. 27. 16:44
반응형
9:33 review
ansible 모듈 : https://docs.ansible.com/ansible-core/devel/collections/ansible/builtin/index.html#plugins-in-ansible-builtin
ansible ad-hoc 명령어
ansible [pattern] -m MODULE [-a ARGUMENT]
※ shell 모듈은 멱등성을 지원하지 않고, 결과값에 지속적으로 CHANGED로 보여지게되므로 상황에 맞게 사용.
[user@ansible-server work]$ ansible all -m shell -a "yum list |grep python2" [user@ansible-server work]$ ansible webservers -m yum -a "name=httpd state=present" → 1회차 실행 192.168.56.11 | CHANGED => { → 명령 성공, changed true (httpd 패키지 설치) "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, [user@ansible-server work]$ ansible webservers -m yum -a "name=httpd state=present" → 2회차 실행 192.168.56.11 | SUCCESS => { → 명령 성공, changed false (이미 설치되었으므로) "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, [user@ansible-server work]$ ansible all -m user -a "name=test" → 생성 [user@ansible-server work]$ ansible all -m user -a "name=test state=absent" →삭제 [user@ansible-server work]$ ansible all -m shell -a "tail /etc/passwd" → 확인 [user@ansible-server work]$ ansible webservers -m service -a "name=httpd state=started" [user@ansible-server work]$ ansible webservers -m service -a "name=httpd state=started" >> ~/fileA [user@ansible-server work]$ ansible webservers -m service -a "name=httpd state=stopped" >> ~/fileA [user@ansible-server work]$ ansible localhost -m shell -a "echo test > ~/work/aaa" 리다이렉션은 shell 모듈로 사용 가능 |
Ansible Playbook
VI 에디터 yaml편의옵션
~./vimrc syntax on autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab autoindent ※ 파일타입 : yaml에 한해서 ts : 스페이스 2칸, sts : 지울때 2칸, sw : tab 확장, autoindent |
httpd 실행 테스트
[user@ansible-server work]$ vim install_httpd.yml --- - name : "Install Apache Webserver" hosts : all tasks : - name : Install latest Apache Webserver (YUM) yum : name : httpd state : latest [user@ansible-server work]$ ansible-playbook --syntax-check install_httpd.yml → 문법체크 playbook: install_httpd.yml [user@ansible-server work]$ ansible-playbook install_httpd.yml PLAY [Install Apache Webserver] **************************************************************************************** TASK [Gathering Facts] ************************************************************************************************* ok: [192.168.56.12] ok: [192.168.56.13] ok: [192.168.56.11] TASK [Install latest Apache Webserver (YUM)] *************************************************************************** ok: [192.168.56.11] changed: [192.168.56.12] changed: [192.168.56.13] PLAY RECAP ************************************************************************************************************* 192.168.56.11 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.56.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.56.13 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [user@ansible-server work]$ vim uninstall_httpd.yml --- - name : "Uninstall Apache Webserver" hosts : all tasks : - name : Uninstall Apache Webserver (YUM) yum : name : httpd state : absent [user@ansible-server work]$ ansible-playbook --syntax-check uninstall_httpd.yml playbook: uninstall_httpd.yml [user@ansible-server work]$ ansible-playbook -i inventory2/hosts5 uninstall_httpd.yml PLAY [Uninstall Apache Webserver] ************************************************************************************** TASK [Gathering Facts] ************************************************************************************************* ok: [192.168.56.12] ok: [192.168.56.11] ok: [192.168.56.13] TASK [Uninstall Apache Webserver (YUM)] ******************************************************************************** changed: [192.168.56.11] changed: [192.168.56.12] changed: [192.168.56.13] PLAY RECAP ************************************************************************************************************* 192.168.56.11 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.56.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.56.13 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
728x90