사용자 도구

사이트 도구


build_deploy_management:ansible

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
다음 판 양쪽 다음 판
build_deploy_management:ansible [2020/09/13 13:42]
kwon37xi
build_deploy_management:ansible [2020/12/16 21:51]
kwon37xi [Local 실행]
줄 4: 줄 4:
   * [[https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html|Ansible All modules]]   * [[https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html|Ansible All modules]]
   * [[https://github.com/ansible/ansible-examples|ansible/ansible-examples: A few starter examples of ansible playbooks, to show features and how they work together. See http://galaxy.ansible.com for example roles from the Ansible community for deploying many popular applications.]]   * [[https://github.com/ansible/ansible-examples|ansible/ansible-examples: A few starter examples of ansible playbooks, to show features and how they work together. See http://galaxy.ansible.com for example roles from the Ansible community for deploying many popular applications.]]
 +  * [[https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html|Best Practices — Ansible Documentation]]
   * 자동화 툴   * 자동화 툴
  
 ===== AWS ===== ===== AWS =====
   * https://github.com/ansible/awx : Web UI   * https://github.com/ansible/awx : Web UI
 +
 +===== become =====
 +  * ''become: yes'' 일 경우 ''sudo'' 로 명령을 실행한다.
 +  * 이때, 비밀번호를 받는 다양한 방법이 존재할 수 있는데, 일단 ''ansible-playbook''에 ''%%--ask-become-pass%%'' 옵션을 주면 최초 시작시 비밀번호를 물어본다.
 +  * 혹은 다음과 같이 ''ansible_become_pass'' 변수로 명령행에 지정할 수도 있다. (''user'' 생략가능) [[https://stackoverflow.com/questions/21870083/specify-sudo-password-for-ansible|Specify sudo password for Ansible]]
 +<code sh>
 +ansible-playbook playbook.yml ... --user=username \
 +   --extra-vars "ansible_become_pass=yourPassword"
 +</code>
 +  * 그런데,이 경우 비밀번호가 외부로 노출되므로 비밀번호를 ''read -s SUDO_PASSWORD'' 등으로 읽거나 환경변수에 지정하고서 다음과 같이 하면 명령 history 노출등에 방어가 된다.
 +<code sh>
 +ansible-playbook playbook.yml ... \
 +    --extra-vars="ansible_become_pass='{{ lookup('env', 'SUDO_PASSWORD') }}'"
 +</code>
 +  * [[linux:sudo|sudo]]
 +
 +===== become_user =====
 +  * 특정 사용자로 명령 실행하기. ''ansible-playbook'' 자체를 ''root'' 로 실행한다면 ''become'' 대신 ''become_user''를 사용해야 할듯.
 +
  
 ===== Local 실행 ===== ===== Local 실행 =====
줄 15: 줄 35:
 <code yml> <code yml>
 - name: playbook name - name: playbook name
-  hosts: 127.0.0.1+  hosts: localhost # 혹은 127.0.0.1 로도 작동했음.
   connection: local   connection: local
   tasks:   tasks:
줄 25: 줄 45:
 </code> </code>
  
-====== 참조 ======+===== File 생성 ===== 
 +  * [[https://www.mydailytutorials.com/ansible-create-files/|How to create files in Ansible - My Daily Tutorials]] 
 +  * [[https://docs.ansible.com/ansible/latest/modules/copy_module.html|copy module]] 
 +  * [[https://docs.ansible.com/ansible/latest/modules/file_module.html|file module]] 
 +<code yml> 
 +  tasks: 
 +  - name: Ansible create file with content example 
 +    copy: 
 +      dest: "/Users/mdtutorials2/Documents/Ansible/remote_server.txt" 
 +      content: "contents" 
 +      mode: 0777 
 +      owner: mdtutorials2 
 +</code> 
 + 
 +===== 환경변수 읽기 ===== 
 +  * [[https://docs.ansible.com/ansible/latest/plugins/lookup/env.html|env – read the value of environment variables — Ansible Documentation]] 
 +<code> 
 +- debug: msg="{{ lookup('env', 'HOME') }} is an environment variable" 
 +</code> 
 + 
 +===== 변수(var) 외부에 두고 include ===== 
 +  * [[https://www.toptechskills.com/ansible-tutorials-courses/ansible-include-import-variables-tutorial-examples/|How to Include Variables in Ansible + Examples | TopTechSkills.com]] 
 +  * ''vars_files'' : 외부 yml 파일의 key/value 쌍을 변수로 읽어들인다. 기본 경로는 현재 플레이북 경로이다. 
 +<code yml> 
 +- name: blah blah.. 
 +  hosts: ... 
 +  vars_files: 
 +    - "vars.yml" # file in the same directory 
 +  tasks: ... 
 +</code> 
 +  * ''vars.yml'' 
 + 
 +<code yml> 
 +# key/value 쌍. 
 +ubuntu_release: "{{ lookup('pipe', 'lsb_release -cs') }}" 
 +current_user: "{{ lookup('env', 'USER') }}" 
 +vagrant_version: 2.2.10 
 +packer_version: 1.6.1 
 +... 
 +</code> 
 + 
 +===== playbook 을 윈해 환경변수 ===== 
 +  * playbook 실행시 적용될 환경변수 지정 
 +  * https://stackoverflow.com/questions/27733511/how-to-set-linux-environment-variables-with-ansible 
 +<code sh> 
 +- hosts: all 
 +  roles: 
 +     - php 
 +     - nginx 
 +  environment: 
 +    MY_ENV_VARIABLE: whatever_value 
 +</code> 
 + 
 +===== 특정 Task 용 환경변수 ===== 
 +  * 다른 태스크에는 적용안됨. 
 +<code sh> 
 +  tasks: 
 +    - name: Echo my_env_var 
 +      shell: "echo $MY_ENV_VARIABLE" 
 +      environment: 
 +        MY_ENV_VARIABLE: whatever_value 
 +</code> 
 + 
 +===== shell & command ===== 
 +  * [[https://www.mydailytutorials.com/introduction-shell-command-module-ansible/|Introduction to Shell and Command Modules in Ansible - My Daily Tutorials]] 
 + 
 +===== apt repository ===== 
 +  * [[https://linuxhint.com/manage_ubuntu_package_repositories_ppas_ansible/|Manage Ubuntu Package Repositories and PPAs Using Ansible – Linux Hint]] 
 + 
 +===== 특정 태스크 지정 ===== 
 +  * ''%%--start-at-task="태스크이름"%%'' : 해당 태스크 부터 실행 
 +  * ''%%--step%%'' : 실행여부 질문 
 + 
 +===== retries ===== 
 +  * ''register'', ''until'', ''delay'' 와 함께 사용. 
 +  * 현재의 ''retries'' 는 ''until''이 없으면 작동하지 않는다. ''until''에서 명백하게 성공 조건을 명시해야한다. 
 +  * [[https://github.com/ansible/ansible/issues/20802|"retries" without "until": retry until success · Issue #20802]] : 성공시 자동으로 ''retries'' 종료 해달라는 이슈. PR 이 있으며, 머지될 가능성이 있음. 
 + 
 +===== ansible-pull ===== 
 +  * [[:git|git]] 리포지토리등에서 파일을 받아서 ansible-playbook 실행. 
 +  * [[https://docs.ansible.com/ansible/latest/cli/ansible-pull.html|ansible-pull]] 
 +  * [[https://medium.com/splunkuserdeveloperadministrator/using-ansible-pull-in-ansible-projects-ac04466643e8|Using Ansible Pull In Ansible Projects | by Vince Sesto | Splunk User Developer Administrator | Medium]] 
 +  * 실행할 플레이북 파일명을 명시하지 않으면 기본적으로 저장소 최상위 디렉토리의 ''local.yml'' 파일을 실행한다. 
 + 
 +<code sh> 
 +ansible-pull https://github.com/.../xxx.git 
 +</code> 
 +===== 참조 ===== 
 +  * [[https://www.youtube.com/playlist?list=PLT98CRl2KxKEUHie1m24-wkyHpEsa4Y70|Getting started with Ansible - YouTube]] : 매우 쉽게 Ansible 설명.
   * [[http://knight76.tistory.com/1977|ansible tomcat]]   * [[http://knight76.tistory.com/1977|ansible tomcat]]
   * [[http://brownbears.tistory.com/358|Ansible이란?]]   * [[http://brownbears.tistory.com/358|Ansible이란?]]
   * [[https://skyoo2003.github.io/post/2017/11/14/ansible-module-develop|Ansible Module 개발하기 - 📚 Devlog in the SKY 📚]]   * [[https://skyoo2003.github.io/post/2017/11/14/ansible-module-develop|Ansible Module 개발하기 - 📚 Devlog in the SKY 📚]]
   * [[http://theeye.pe.kr/archives/2597|Ansible Playbook 정리 | 아이군의 블로그]]   * [[http://theeye.pe.kr/archives/2597|Ansible Playbook 정리 | 아이군의 블로그]]
 +  * [[https://www.ansible.com/blog/ansible-tips-and-tricks-dealing-with-unreliable-connections-and-services|Ansible Tips and Tricks: Dealing with Unreliable Connections and Services]]