1.Roles介紹
ansible自1.2版本引入的新特性仲墨,用于層次性、結(jié)構(gòu)化地組織playbook俩由。roles能夠根據(jù)層次型結(jié)構(gòu)自動裝載變量文件幻梯、tasks以及handlers等努释。要使用roles只需要在playbook中使用include指令引入即可伐蒂。簡單來講,roles就是通過分別將變量恩沛、文件雷客、任務(wù)桥狡、模板及處理器放置于單獨的目錄中,并可以便捷的include它們的一種機制呈宇。角色一般用于基于主機構(gòu)建服務(wù)的場景中,但也可以是用于構(gòu)建守護進程等場景中存炮。playbook局限在于如果文件較多的情況蜈漓,不清楚哪些主機執(zhí)行了哪些狀態(tài)的yml文件融虽,roles能清楚哪些主機應(yīng)用哪些角色,主要使用場景代碼復(fù)用度較高的情況下般又。
2.Roles目錄結(jié)構(gòu)
目錄介紹
roles: <--所有的角色必須放在roles目錄下茴迁,這個目錄可以自定義位置堕义,默認(rèn)的位置在/etc/ansible/roles
project: <---具體的角色項目名稱倦卖,比如nginx椿争、tomcat、php
files: <--用來存放由copy模塊或script模塊調(diào)用的文件嘉竟。
templates: <--用來存放jinjia2模板舍扰,template模塊會自動在此目錄中尋找jinjia2模板文件边苹。
tasks: <--此目錄應(yīng)當(dāng)包含一個main.yml文件裁僧,用于定義此角色的任務(wù)列表慕购,此文件可以使用include包含其它的位于此目錄的task文件沪悲。
main.yml
handlers: <--此目錄應(yīng)當(dāng)包含一個main.yml文件殿如,用于定義此角色中觸發(fā)條件時執(zhí)行的動作涉馁。
main.yml
vars: <--此目錄應(yīng)當(dāng)包含一個main.yml文件爱致,用于定義此角色用到的變量糠悯。
main.yml
defaults: <--此目錄應(yīng)當(dāng)包含一個main.yml文件,用于為當(dāng)前角色設(shè)定默認(rèn)變量叶沛。
main.yml
meta: <--此目錄應(yīng)當(dāng)包含一個main.yml文件忘朝,用于定義此角色的特殊設(shè)定及其依賴關(guān)系局嘁。
main.yml
3.ansible roles舉例
示例1
#playbook安裝tomcat
[root@m01 /project1]# ll
total 202624
-rw-r--r-- 1 root root 12326996 Apr 5 16:45 apache-tomcat-9.0.26.tar.gz
-rwxr-x--- 1 root root 23689 Apr 5 18:08 catalina.sh
-rw-r--r-- 1 root root 17 Apr 4 22:23 hosts
-rw-r--r-- 1 root root 195094741 Apr 5 16:45 jdk-8u221-linux-x64.tar.gz
-rw-r--r-- 1 root root 1617 Apr 5 18:13 server.xml.j2
-rw-r--r-- 1 root root 126 Apr 5 18:08 setenv.sh
-rw-r--r-- 1 root root 290 Apr 5 18:08 tomcat.service
-rw-r--r-- 1 root root 2353 Apr 6 10:54 tomcat.yml
[root@m01 /project1]# vim tomcat.yml
---
- hosts: web
vars:
- tomcat_version: 9.0.26
- jdk_packages: 8u221
- jdk_version: jdk1.8.0_221
tasks:
- name: add group
group:
name: www
gid: 666
state: present
- name: add user
user:
name: www
uid: 666
group: www
create_home: no
- name: mkdir directory
file:
path: /usr/java
state: directory
- name: unarchive jdk-{{ jdk_packages }}-linux-x64.tar.gz
unarchive:
src: ./jdk-{{ jdk_packages }}-linux-x64.tar.gz
dest: /usr/java
- name: add java environment
lineinfile:
path: /etc/profile
regexp: "{{ item.re }}"
line: "{{ item.li }}"
loop:
- { re: '^export JAVA_HOME=', li: 'export JAVA_HOME=/usr/java/{{ jdk_version }}' }
- { re: '^export PATH=', li: 'export PATH=$JAVA_HOME/bin:$PATH' }
- { re: '^export CLASSPATH=', li: 'export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JA
VA_HOME/lib' }
- name: reloade profile
shell: source /etc/profile
- name: unarchive apache-tomcat-{{ tomcat_version }}.tar.gz
unarchive:
src: ./apache-tomcat-{{ tomcat_version }}.tar.gz
dest: /usr/local
- name: rename tomacat
shell: cd /usr/local/;mv apache-tomcat-{{tomcat_version}} /usr/local/tomcat;chown -R www.www /usr/local/
tomcat
- name: copy tomact server rename tomacat
template:
src: ./server.xml.j2
dest: /usr/local/tomcat/conf/server.xml
backup: yes
- name: copy file
copy:
src: "./{{ item.src }}"
dest: "{{ item.dest }}"
loop:
- { src: 'setenv.sh', dest: '/usr/local/tomcat/bin/' }
- { src: 'tomcat.service', dest: '/lib/systemd/system/' }
- { src: 'catalina.sh', dest: '/usr/local/tomcat/bin/' }
notify: restart tomcat
- name: ln -s enable
file:
src: /lib/systemd/system/tomcat.service
dest: /etc/systemd/system/multi-user.target.wants/tomcat.service
state: link
- name: start tomcat
systemd:
daemon_reload: yes
name: tomcat
state: started
enabled: yes
handlers:
- name: restart tomcat
service:
name: tomcat
state: restarted
將playbook改寫為rolse
#目錄結(jié)構(gòu)
[root@m01 /etc/ansible/roles]# tree tomcat
tomcat
├── files
│ ├── catalina.sh
│ ├── setenv.sh
│ └── tomcat.service
├── handlers
│ └── main.xml
├── tasks
│ └── main.yml
├── templates
│ └── server.xml.j2
└── vars
└── main.yml
#模塊詳細類容
1.files
[root@m01 /etc/ansible/roles/tomcat/files]# ll
total 32
-rwxr-x--- 1 root root 23689 Apr 2 21:20 catalina.sh
-rw-r--r-- 1 root root 126 Apr 2 21:23 setenv.sh
-rw-r--r-- 1 root root 290 Apr 2 21:20 tomcat.service
2.tasks
[root@m01 /etc/ansible/roles/tomcat/tasks]# cat main.yml
- name: add group
group:
name: www
gid: 666
state: present
- name: add user
user:
name: www
uid: 666
group: www
create_home: no
- name: mkdir dir
file:
path: /usr/java
state: directory
- name: unarchive jdk-8u221-linux-x64.tar.gz
unarchive:
src: /root/jdk-8u221-linux-x64.tar.gz
dest: /usr/java
- name: add java environment
lineinfile:
path: /etc/profile
regexp: "{{item.re}}"
line: "{{item.li}}"
loop:
- { re: '^export JAVA_HOME=', li: 'export JAVA_HOME=/usr/java/jdk1.8.0_221' }
- { re: '^export PATH=', li: 'export PATH=$JAVA_HOME/bin:$PATH' }
- { re: '^export CLASSPATH=', li: 'export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib' }
- name: reloade profile
shell: source /etc/profile
- name: unarchive tomcat-{{tomcat_version}}.tar.gz
unarchive:
src: /root/apache-tomcat-{{tomcat_version}}.tar.gz
dest: /usr/local
- name: rename tomacat
shell: cd /usr/local/;mv apache-tomcat-{{tomcat_version}} /usr/local/tomcat;chown -R www.www /usr/local/tomcat
- name: copy tomcat server.xml start file
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
backup: yes
loop:
- { src: 'server.xml.j2', dest: '/usr/local/tomcat/conf/server.xml' }
- name: copy file
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
loop:
- { src: 'setenv.sh', dest: '/usr/local/tomcat/bin/' }
- { src: 'tomcat.service', dest: '/lib/systemd/system/' }
- { src: 'catalina.sh', dest: '/usr/local/tomcat/bin/' }
- name: ln -s enable
file:
src: /lib/systemd/system/tomcat.service
dest: /etc/systemd/system/multi-user.target.wants/tomcat.service
state: link
- name: start tomcat
systemd:
daemon_reload: yes
name: tomcat
state: started
enabled: yes
3.template
[root@m01 /etc/ansible/roles/tomcat/templates]# ls
server.xml.j2
4.vars
[root@m01 /etc/ansible/roles/tomcat/vars]# cat main.yml
tomcat_version: 9.0.26
jdk_packages: 8u221
jdk_version: jdk1.8.0_221
5.handlers
[root@m01 /etc/ansible/roles/tomcat/handlers]# cat main.yml
- name: restart tomcat
service:
name: tomcat
state: restarted
示例2
Ansible安裝keepalive實現(xiàn)主/備模式高可用
#目錄結(jié)構(gòu)
[root@m01 /etc/ansible/roles/keepalived]# ll
total 0
drwxr-xr-x 2 root root 24 Oct 21 20:44 files
drwxr-xr-x 2 root root 22 Apr 6 09:45 handlers
drwxr-xr-x 2 root root 22 Apr 6 10:53 tasks
drwxr-xr-x 2 root root 55 Apr 6 10:29 templates
#rolse詳情
1.tasks
[root@m01 /etc/ansible/roles/keepalived/tasks]# vim main.yml
- name: install keepalived server
yum:
name: keepalived
state: present
- name: configure keepalived server
template:
src: keepalived.conf.j2
dest: /etc/keepalived/keepalived.conf
backup: yes
notify: restart keepalived
- name: create dir
file:
path: /server/scripts/
state: directory
- name: copy check_web.sh
template:
src: check_web.sh.j2
dest: /server/scripts/check_web.sh
mode: '0755'
- name: start keepalived server
systemd:
name: keepalived
state: started
enabled: yes
2.files
[root@m01 /etc/ansible/roles/keepalived/files]# ll
total 4
-rw-r--r-- 1 root root 65 Oct 21 20:44 local.repo
3.templates
[root@m01 /etc/ansible/roles/keepalived/templates]# ls
check_web.sh.j2 keepalived.conf.j2
4.handlers
[root@m01 /etc/ansible/roles/keepalived/handlers]# cat main.yml
- name: restart keepalived
systemd:
name: keepalived
state: restarted
4.ansible roles總結(jié)
1抗楔、編寫任務(wù)(task)的時候连躏,里面不需要寫需要執(zhí)行的主機,單純的寫某個任務(wù)是干什么的即可拍棕,裝軟件的就是裝軟件的,啟動的就是啟動的绰播。單獨做某一件事即可,最后通過main.yml將這些單獨的任務(wù)安裝執(zhí)行順序include進來即可蠢箩,這樣方便維護且一目了然。
2示弓、定義變量時候直接安裝k:v格式將變量寫在vars/main.yml文件即可奏属,然后task或者template直接調(diào)用即可潮峦,會自動去vars/main.yml文件里面去找。
3嘱腥、定義handlers時候齿兔,直接在handlers/main.yml文件中寫需要做什么事情即可础米,多可的話可以全部寫在該文件里面,也可以像task那樣分開來寫医寿,通過include引入一樣的可以靖秩。在task調(diào)用notify時直接寫與handlers名字對應(yīng)即可(二者必須高度一直)竖瘾。
4、模板文件一樣放在templates目錄下即可事扭,task調(diào)用的時后直接寫文件名字即可求橄,會自動去到templates里面找。
注意:如果是一個角色調(diào)用另外一個角色的單個task時后罐农,那么task中如果有些模板或者文件,就得寫絕對路徑了宰睡。