배포 자동화 툴
brew install ansible
ansible-doc -l
ansible-doc file
ansible-playbook example-play.yml
- hosts: webservers
users: root
sudo: yes // root 권한 획득을 위해 사용
user: flynn // sudo 실행 전에 장비에 연결한 사용자 이름 정의
sudo_user: root // sudo 사용을 시도하여 되려는 사용자 정의
connection: ssh // 원격장비 연결하기 위해 사용할 수 있는 전송 방법 정의(ssh, paramiko, local)
gather_facts: yes // setup 모듈 결과 사용여부 태스크 수행시간을 줄일 수 있음
vars:
nginx_version: 1.9.1
motd_warning: 'WARNING: Use by Flynn Only'
testservers: yes
변수 파일을 통해 로드
vars_files:
/conf/test.yml
사용자 프롬프트를 통해서 받음
vars_prompt:
- name:'https_passpharase'
prompt: 'Key Passphrase'
private: yes
tasks:
// action: 모듈명령
- name: install nginx
action: yum name=nginx state=installed
// ansible이 추천 하는 방식
// module이름: 필요한 인수나열
- name: configure nginx
copy: src=files/nginx.conf dest=/etc/nginx/nginx.conf
// 인수 여러줄로 나열
- name: restart nginx
service:
name:nginx
state: restarted
handlers:
- name: restart nginx
action: service name=nginx state=restarted
ansible machine-name -m ping
ansible machine-name -m setup
ansible machine-name -m file -a 'path=/etc/fstab'
ansible machine-name -m file -a 'path=/tmp/test state=directory mode=0700 owner=root'
ansible machine-name -m file -a 'path=/tmp/test state=absent'
ansible machinename -m copy -a 'src=/etc/fstab dest=/tmp/fstab'
ansible machinename -m command -a 'rm -rf /tmp/testing removes=/tmp/testing'
ansible machinename -m shell -a '/opt/testapp/install.sh > /var/log/testapp.log creates=/var/log/testapp.log'
template: src=templates/named.conf.j2 dest=/etc/named.conf owner=root group=named mode=0640
set_fact: innodb_buffer_pool_size_mb="{{ ansible_memtotal_mb / 2 }}
pause: prompt="Wanring! Enter to continue CTRL-C a to quit"
pause: seconds=30
wait_for: port=8080 state=started
assemble: src=/opt/sshkeys dest=/root/.ssh/authorized_keys owner=root group=root mode=0700
add_host:
---
- name: Create operating system group
hosts: all
tasks:
- group_by: key=os_{{ ansible_distribution }}
- name: Run on CentOS hosts only
hosts: os_CentOS
tasks:
- name: Install Nginx
yum: name=nginx state=latest
- name: Run on Ubuntu hosts only
hosts: os_Ubunutu
tasks:
- name: Install Nginx
apt: pkg=nginx state=latest
tasks:
- name: Install mlocate
yum: name=mlocate state=installed
- name: Run updatedb
command: /usr/bin/updatedb
async: 300 // 커맨드가 완료될 때까지 앤시블이 기다려 줄 수 있는 최대 값
poll: 10 // 커맨드가 완료될 때를 점검하기 위해 얼마나 자주 폴링
poll 0 // 앤시블이 완료를 기다리지 않음 async 0 // 작업을 완료할 때까지 기다림
tasks:
- name: Secure config files
file: path=/etc/{{ item }} mode=0600 owner=root group=root
with_items:
- my.cnf
- shadow
- fstab
// lookup 플러그인
tasks:
- name: Upload public keys
copy: src={{ item }} dest=/root/.sshkeys mode=0600 owner=root group=root
with_fileglob:
- keys/*.pub
tasks:
- name: Install VIM via yum
yum: name=vim-enhanced state=installed
when: ansible_os_family == "RedHat"
- name: Install VIM via apt
apt: name=vim state=installed
when: ansible_os_family == "Debian"
- name: Unexpected OS family
debug: msg="OS Family {{ ansible_os_family }} is not supported" fail=yes
when: not ansible_os_family == "RedHat" or ansible_os_family == "Debian"
tasks:
- name: Get config
get_url: dest=configs/{{ ansible_hostname }} force=yes url=http://{{ ansible_hostname }}/diagnostic/config
delegate_to: localhost
// localhost에 위임하는 경우는 local_action
이 있음
tasks:
- name: Get config
local_action: get_url dest=configs/{{ ansible_hostname }} force=yes url=http://{{ ansible_hostname }}/diagnostic/config
{{ hostvars.[hostname].ansible_default_ipv4.address }}
- name: Create a user for all app servers
with_items: groups.appservers
mysql_user: name=flynn password=test host={{ hostvars.[item].ansible_eth0.ipv4.address }} state present
tasks:
- name: For secure machines
set_fact: sshconfig=files/ssh/sshd_config_secure
when: "'secure' in group_names"
- name: For non-secure machines
set_fact: sshconfig=files/ssh/sshd_config_default
when: "'secure' not in group_names"
- name: Copy over the config
copy: src={{ sshconfig }} dest=/tmp/sshd_config
copy: src=files/nrpe.{{ ansible_architecture }}.conf dest=/etc/nagios/nrpe.cfg
name: Get the best match for the machine
copy: dest=/etc/nginx.conf src={{ item }}
first_available_file:
- files/nginx/{{ ansible_os_family }}-{{ ansible_architecture }}.conf
- files/nginx/default-{{ ansible_architecture }}.conf
- files/nginx/default.conf
name: Upload the file
shell: aws s3 put-object --bucket=my-test-bucket --key={{ ansible_hostname }}/fstab --body=/etc/fstab --region=eu-west-1
environment:
AWS_ACCESS_KEY_ID: XXXXXXXX
AWS_SECRET_ACCESS_KEY: XXXXXXXX
직접 호출 방식
name: Download file
get_url: dest=/var/tmp/file.tar.gz url=http://server/file.tar.gz
environment:
http_proxy: "{{ lookup('env', 'http_proxy') }}"
with_* 사용하는 방식
name: Register the webapp farm
local_action: add_host name={{ item }} groupname=webapp
with_sequence: start=1 end=10 format=webapp%02x
- name: Get /tmp info
file: dest=/tmp state=directory
register: tmp
- name: Set mode on /var/tmp
file: dest=/tmp/subtmp mode={{ tmp.mode }} state=directory
debug: msg="{{ item }}"
with_items: ansible_interfaces
ansible-playbook --verbose playbook.yml
# usersetup.yml
# Requires a user variable to specify user to setup
- name: Create user account
user: name={{ user }} state=present
- name: Make user SSH config dir
file: path=/home/{{ user }}/.ssh owner={{ user }} group={{ user }} mode=0600 state=directory
- name: Copy in public key
copy: src=keys/{{ user }}.pub dest=/home/{{ user }}/.ssh/authorized_keys mode=0600 owner={{ user }} group={{ user }}
tasks:
- include: usersetup.yml user={{ item }}
with_items:
- niceilm
- flynn
handlers:
- include: sendmailhandlers.yml
---
- include "drfailover.yml"
- include "upgradeapp.yml"
- include "drfailback.yml"
- name: Notify management
host: local
tasks:
- local_action: mail to="niceilm@naver.com" msg='The application has been upgraded and is now live'
- include "drupgrade.yml"
- name: cache
apt: purge=yes name=lxc-docker
ignore_errors: yes