6. Ansible is...
• 오픈소스 구성관리 및 프로비저닝 도구 (similar to Chef, Puppet, Salt)
• 실행 작업을 작성하기 쉬운 YAML 형식으로 정의
• SSH 접속만 가능하면 대부분 Ansible을 통해 작업을 수행
• Agentless: 대상 서버에 Agent 설치가 필요 없음
• Idempotency: 같은 작업을 여러번 수행하더라도 결과는 같음
11. What can it automate?
Infrastructure
Provisioning
Configuration
Management
Application
Deployment
SSH
12. Ansible Architecture
Ansible Control Node (Desktop, Laptop)
Playbook
(YAML)
- name
connection
hosts: DB
tasks:
module
....
- name
connection
hosts: WEB
tasks:
module
....
ssh
ssh
pip install
ansible
Inventory
(hostfile)
[WEB]
10.0.1.100
10.0.2.100
10.0.3.100
[DB]
10.0.4.100
10.0.5.100
WEB (Managed Node)
10.0.1.100 10.0.2.100 10.0.3.100
DB (Managed Node)
10.0.5.10010.0.4.100
13. How to install Ansible?
Control Node Requirements
• Python 2 (version 2.7)
• Python 3 (version 3.5 and higher)
• Windows is not supported for the control node
Managed Node Requirements
• Native OpenSSH (1.3 or later)
• By default this uses sftp
• If that's not available, you can switch to scp
• Python 2 (2.6 or later) or Python 3 (3.5 or later)
Installing Ansible (RHEL and CentOS)
$ yum install python
$ yum install python-pip
$ pip install virtualenv
$ pip install ansible
14. What is Ansible Inventory?
mail.example.com
[WEB]
10.0.1.100
10.0.2.100
10.0.3.100
[DB]
10.0.4.100
10.0.5.100
INI, GROUP, HOST
WEB:
hosts:
10.0.1.100:
10.0.2.100:
vars:
some_server:
some_server=test.exam
ple.com
YAML, HOST, HOST VAR
[WEB]
host1
ansible_connection=ssh
ansible_host=10.0.1.11
ansible_user=opc
host2
ansible_connection=ssh
ansible_host=10.0.1.12
ansible_user=opc
INI, GROUP, HOST, HOST VAR
[WEB]
10.0.1.100
[WAS1:children]
10.0.1.101
10.0.1.102
[WAS1:vars]
some_server=test.exampl
e.com
INI, GROUP, CHILD GROUP & VAR
• 기본 Inventory File: /etc/ansible/hosts
• 별도의 경로에 별도의 파일로 생성 가능[Inventory]
• host, group, group children에 대한 변
수를 별도의 파일로 관리 가능 (권장)
• Managed Node (구성할 리모트 서버)에 대
한 호스트 정보를 가짐, 그룹과 호스트로 관리
15. Playbook - Basic
---
- name: Web Server Play
hosts: web
remote_user: opc
become_method: sudo
become: yes
tasks:
- name: add web user
user:
name: webuser
shell: /bin/bash
append: yes
state: present
tags:
- add_web_user
- name: DB Server Play
hosts: db
remote_user: opc
tasks:
- name: add db user
user:
name: oracle
shell: /bin/bash
append: yes
state: present
tags:
- add_db_user
...
YAML
https://docs.ansible.com/ansible/2.4/playbooks_keywords.html
Playbook keywords
Playbook Keywords
Tasks
Module
Module Input Parameters
Tags
Play1
Play2
16. Playbook - Keywords
---
- name: Web Server Play
hosts: web
remote_user: opc
become_method: sudo
become: yes
tasks:
YAML
Keywords: Common Playbook Objects
• Play
• Role
• Block
• Task
Keywords
17. Playbook - Tasks
---
- name: Web Server Play
hosts: web
remote_user: opc
become_method: sudo
become: yes
tasks:
- name: add web user
user:
name: 'webuser'
shell: /bin/bash
append: yes
state: present
tags:
- add_web_user
- name: debug
debug:
msg: "debug..."
YAML
Each task contains:
• Task name
• Module
• Module Parameter
• Conditions (when, failed_when..)
• Processing directives (become, register..)
Task1
Task2
18. Playbook - Module
---
- name: Web Server Play
hosts: web
remote_user: opc
become_method: sudo
become: yes
tasks:
- name: add web user
user:
name: 'webuser'
shell: /bin/bash
append: yes
state: present
tags:
- add_web_user
- name: debug
debug:
msg: "debug..."
YAML
https://docs.ansible.com/ansible/latest/modules/modules_by_category.html
User Module
Module Input Parameters
Debug Module
Module Input Parameters
Ansible Module List
19. Playbook - Working with Modules
https://docs.ansible.com/ansible/latest/modules/find_module.html#find-module
예) File find Module
20. Playbook - Working with Modules
Input Parameters Return Values
• Return Values는 Ansible에서 기본 제공하는 Common
Return Value와 Internal 사용을 위한 Value를 별도 제공
https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html
21. Playbook - Working with Modules
- name: Recursively find /tmp files older than 2 days
find:
paths: /tmp
age: 2d
recurse: yes
register: result
- name: print find files result
debug:
msg: "{{ result }}"
find Module 사용 예시
Input Parameter
find Module
Capture return value to a variable
debug Module
Print output variable
< TASK [print find files result] >
ok: [1.2.3.4] => {
"result": {
"changed": false,
"examined": 3119,
"files": [
{
"atime": 1483973253.7295375,
...
"mode": "0600",
"mtime": 1483973253.7295375,
"nlink": 1,
"path": "/tmp/delme",
22. Playbook - Variables
---
- name: Web Server Play
hosts: web
vars: web
user_name: webuser
vars_files:
- /home/user/ansible/users.yml
remote_user: opc
become_method: sudo
become: yes
tasks:
- name: add web user
user:
name: '{{ user_name }}'
shell: /bin/bash
append: yes
state: present
tags:
- add_web_user
YAML
---
users:
- user: user1
tenancy: ocid1.tenancy.oc1..
region: ap-seoul-1
- user: user2
tenancy: ocid1.tenancy.oc1..
region: ap-seoul-1
vars
ansible-playbook release.yml --extra-vars "user_name=webuser"
extra vars
inventory vars
host_vars, group_vars
- /etc/ansible/hosts/host_vars/{host}
- /etc/ansible/hosts/group_vars/{group}
var file
facts
ansible hostname -m setup
- ansible_hostname, ansible_version..
Role Defaults
The lowest priority of any variables available
23. Playbook - Handler
---
- name: Web Server Play
hosts: web
remote_user: opc
become_method: sudo
become: yes
tasks:
- name: httpd package is present
yum:
name: httpd
state: latest
notify: Restart httpd
handlers: httpd package is present
- name: Restart httpd
service: httpd
name: httpd
state: restarted
YAML
notify
handler
Handlers: Running Operations On Change
• Only run if triggered by the notify directive
• Any module can be used for the handler action
• Indicates a change in the system state
24. Playbook - TemplatesYAML
---
- name: Web Server Play
hosts: web
remote_user: opc
become_method: sudo
become: yes
tasks:
- name: Install nginx
yum:
name: nginx
state: present
- name: Copy nginx conf for
wordpress
- template: src=default.conf dest=/
etc/nginx/conf.d/default.conf
notify: restart nginx server {
listen {{ nginx_port }} default_server;
server_name {{ server_hostname }};
root /src/wordpress/ ;
client_max_body_size 64M;
location ~* /(?:uploads|files)/.*.php$ {
deny all;
}
....
• Jinja is a modern and designer-friendly templating language for Python
• Jinja2 template language used in Ansible
• {% … %} for control statements (conditions)
• {{ … }} for expressions (variables)
• {# … #} for comments (describe the task)
Jinja2 Template
default.conf
How it works
25. Playbook - Roles
---
- name: WordPress,MariaDB,NginX, php-fpm
hosts: all
remote_user: opc
become_method: sudo
become: yes
roles:
- common
- mariadb
- nginx
- php-fpm
- wordpress
YAML
site.yml
roles/
common/
tasks/
handlers/
files/
templates/
vars/
defaults/
meta/
Role Directory Structure
The main list of tasks
Handlers (used by this role or anywhere)
Can be deployed via this role
Can be deployed via this role
Other variables for the role
Default variables for the role
Some meta data for this role
Using Role
Ansible Galaxy
• Free site for finding, downloading, rating, and reviewing all kinds of
community developed Ansible roles (https://galaxy.ansible.com)
• Command line tool for Ansible Galaxy: ansible-galaxy
Using ansible-galaxy
• ansible-galaxy init --force common
• ansible-galaxy install oracle.oci_ansible_modules
• ansible-galaxy remove oracle.oci_ansible_modules
• ansible-galaxy list
26. Playbook - Conditionals & Loops
Loop and Conditionals
---
tasks:
- command: echo {{ item }}
loop: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5
When
---
tasks:
- name: "shut down Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_facts['os_family'] ==
"Debian"
# note that all variables can be used
directly in conditionals without double curly
braces
Iterating over a simple list
---
- name: add several users
user:
name: "{{ item }}"
state: present
groups: "wheel"
loop:
- testuser1
- testuser2
Iterating over a list of hashes
---
- name: add several users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
loop:
- { name: 'testuser1', groups: 'wheel' }
- { name: 'testuser2', groups: 'root' }
with_list
---
- name: with_list
debug:
msg: "{{ item }}"
with_list:
- one
- two
with_items
---
- name: with_items
debug:
msg: "{{ item }}"
with_items: "{{ items }}"
with_list -> loop
loop and the flatten filter.