自動化運維Ansible實踐(二)
第 1 章 Playbook基本使用
使用Playbook的好處
特點
? 易讀的編排語言
? 適合配置管理和應用部署
? 非常適合部署復雜的工作
先來認識一下Playbook
自動部署Nginx
main.yml
-
hosts: webservers
vars:
hello: Ansibletasks:
- name: Add repo
yum_repository:
name: nginx
baseurl: http://nginx.org/packages/centos/3/$basearch/
gpgcheck: no
enabled: 1 - name: Install nginx
yum:
name: nginx
state: latest - name: Copy nginx configuration file
copy:
src: ./site.conf
dest: /etc/nginx/conf.d/site.conf - name: Start nginx
service:
name: nginx
state: started - name: Create wwwroot directory
dest: /var/www/html
state: directory - name: Create test page index.html
shell: echo "hello {{hello}}" > /var/www/html/index.html
- name: Add repo
site.conf
server {
listen 80;
server_name www.ctnrs.com;
location / {
root /var/www/html;
index index.html;
}
}
.YAML語法
? 縮進表示層級關系
? 不支持制表符“tab”縮進,使用空格縮進
? 通常開頭縮進 2 個空格
? “---” 表示YAML格式,一個文件的開始
? “#”註釋
playbook幫助 ansible-playbook --help
在執行前可以先檢查語法ansible-playbook nginx.yml --syntax-check
.Playbook文件結構
-
name: play1
hosts: webservers
remote_user: root
vars:
var_name: value
tasks:- name: echo
shell: "echo {{var_name}}"
- name: echo
-
name: play2
hosts: webservers
remote_user: root
vars:
var_name: value
tasks:- name: echo
shell: "echo {{var_name}}"
- name: echo
- hosts: webservers
remote_user: root
vars:
var_name: value
tasks:- name: echo
shell: "echo {{var_name}}"
- name: echo
.在變更時執行操作(handlers)
notify:在任務結束時觸發
handlers:由特定條件觸發Tasks
hosts: webservers
gather_facts: no
tasks:
-
name: Copy nginx configuration file
copy:
src: ./site.conf
dest: /etc/nginx/conf.d/site.conf
notify:- reload nginx ------》當配置文件改變時通知重啟nginx服務
handlers:
- name: reload nginx
service: name=nginx state=reloaded
.任務控制(tags)
在每一個任務中添加tags標簽,可以根據指定的tags運行相應的任務。
-
hosts: webservers
gather_facts: no
vars:
hello: Ansibletasks:
- name: Add repo
yum_repository:
name: nginx
description: nginx repo
baseurl: http://nginx.org/packages/centos/2/$basearch/
gpgcheck: no
enabled: 1 - name: Install nginx
yum:
name: nginx
state: latest
tags: install - name: Copy nginx configuration file
copy:
src: ./site.conf
dest: /etc/nginx/conf.d/site.conf
tags: congiuration - name: Start nginx
service:
name: nginx
state: started - name: Create wwwroot directory
file:
dest: /var/www/html
state: directory - name: Create test page index.html
shell: echo "hello {{hello}}" > /var/www/html/index.html
指定tags運行:
[[email protected] ansible-playbook]# ansible-playbook nginx.yml --tags "congiuration"
- name: Add repo
PLAY [webservers] ***
TASK [Copy nginx configuration file] ****
ok: [192.128.132.12]
ok: [192.128.132.14]
PLAY RECAP **
192.128.132.14 : ok=1 changed=0 unreachable=0 failed=0
192.128.132.12 : ok=1 changed=0 unreachable=0 failed=0
.Playbook文件調試
語法檢查:ansible-playbook main.yml --syntax-check
打印語句:
- hosts: webservers
tasks:- debug:
msg: {{group_names}} - debug:
msg: {{inventory_hostname}} - debug:
msg: {{ansible_hostname}}
- debug:
.案例:自動部署Tomcat
-
hosts: 192.128.132.12
gather_facts: no
vars:
tomcat_version: 8.1.33
tomcat_install_dir: /usr/localtasks:
-
name: Install jdk1.8
yum: name=java-1.8.0-openjdk state=present -
name: Download tomcat
get_url: url=http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz dest=/tmp -
name: Unarchive tomcat-{{ tomcat_version }}.tar.gz
unarchive:
src: /tmp/apache-tomcat-{{ tomcat_version }}.tar.gz
dest: "{{ tomcat_install_dir }}"
copy: no - name: Start tomcat
shell: cd {{ tomcat_install_dir }} &&
mv apache-tomcat-{{ tomcat_version }} tomcat8 &&
cd tomcat8/bin && nohup ./startup.sh &
-
第 2 章 Playbook定義變量與使用
.命令行
.在Inventory中定義
.在Playbook中定義
在Playbook中定義變量
hosts: webservers
gather_facts: no
vars:
var_name: value
var_name: value
tasks:
- name: hello
shell: "echo {{var_name}}"
.在Role中定義
.註冊變量(register)
註冊變量
- hosts: webservers
gather_facts: no
tasks:- name: Get date
command: date +"%F_%T"
register: date_output - name: Echo date_output
command: touch /tmp/{{date_output.stdout}}
- name: Get date
.系統信息變量(facts)
系統變量
hosts: webservers
tasks:
- name: Get hostname
debug: msg={{ansible_hostname}}第 3 章 Playbook文件復用
.include & import 區別
include(動態):在運行時導入
? --list-tags,--list-tasks不會顯示到輸出
? 不能使用notify觸發來自include內處理程序名稱(handlers)
import*(靜態):在Playbook解析時預先導入
? 不能與循環一起使用
? 將變量用於目標文件或角色名稱時,不能使用inventory(主機/主機組等)中的變量
import_playbook
includetasks & importtasks
4. 第 4 章 Playbook流程控制
條件
-
hosts: webservers
tasks:
- name: Host 192.168.132.16 run this task
debug: msg="{{ansible_default_ipv4.address}}"
when: ansible_default_ipv4.address == ‘192.168.132.16‘
只執行該任務,跳過其他主機。
- name: Host 192.168.132.16 run this task
根據不同發行版安裝apache服務
-
hosts: webservers
tasks:
-
name: Update apache version - yum
yum: name=httpd state=present
when: ansible_pkg_mgr == ‘yum‘
notify: restart httpd - name: Update apache version - apt
apt: name=apache2 state=present update_cache=yes
when: ansible_pkg_mgr == ‘apt‘
notify: restart apache2
handlers:
- name: restart httpd
service: name=httpd state=restared
handlers: - name: restart apache2
service: name=apache2 state=restared
tasks:
-
- name: "shut down CentOS 6 and Debian 7 systems"
command: /sbin/shutdown -t now
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
(ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
tasks:- name: "shut down CentOS 6 systems"
command: /sbin/shutdown -t now
when:- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "6"
- name: "shut down CentOS 6 systems"
循環
-
name: with_list
debug:
msg: "{{ item }}"
with_list:- one
- two
- name: with_list -> loop
debug:
msg: "{{ item }}"
loop:- one
- two
-
name: with_items
debug:
msg: "{{ item }}"
with_items: "{{ items }}" - name: with_items -> loop
debug:
msg: "{{ item }}"
loop: "{{ items|flatten(levels=1) }}"
創建用戶: - hosts: webservers
gather_facts: no
tasks:- name: with_list
user: name={{item}} state=present
with_list:- test1
- test2
5. 第 5 章 Playbook模板(jinja2)
5.1 .條件和循環
test.yml
- name: with_list
-
hosts: webservers
vars:
hello: Ansibletasks:
- template: src=f.j2 dest=/tmp/f.j2
f.j2
{% set list=[‘one‘, ‘two‘, ‘three‘] %}
{% for i in list %}
{% if i == ‘two‘ %}
-> two
{% elif loop.index == 3 %}
-> 3
{% else %}
{{i}}
{% endif %}
{% endfor %}
{{ hello }}
{% set dict={‘zhangsan‘: ‘26‘, ‘lisi‘: ‘25‘} %}
{% for key, value in dict.iteritems() %}
{{key}} -> {{value}}
{% endfor %}
5.2 .案例:管理Nginx配置文件
main.yml
-
hosts: webservers
gather_facts: no
vars:
http_port: 80
server_name: www.ctnrs.comtasks:
- name: Copy nginx configuration file
template: src=site.conf.j2 dest=/etc/nginx/conf.d/www.ctnrs.com.conf
notify: reload nginx
handlers:
- name: reload nginx
service: name=nginx state=reloaded
- name: Copy nginx configuration file
site.conf.j2
{% set list=[10, 12, 13, 25, 31] %}
upstream {{server_name}} {
{% for i in list %}
server 192.168.1.{{i}}:80;
{% endfor %}
}
server {
listen {{ http_port }};
server_name {{ server_name }};
location / {
proxy_pass http://{{server_name}};
}
}
自動化運維Ansible實踐(二)