注意区分include与import模块(虽然都是task关联使用,但是差距很大)
官方roles网站及使用工具
ansible-galaxy
命令:默认与https://galaxy.ansible.com网站API通信,可以查找、下载各种社区开发的 Ansible 角色#ansible安装后默认创建目录
/usr/share/ansible/roles #主要用于存放一些系统角色
/etc/ansible/roles #默认角色路径
#可以自定义roles文件,但是要在配置文件中指定文件路径
vim ansible.cfg
[defaults] #此模块内添加参数
roles_path=~/myroles #添加路径目录参数
#自定义目录myroles,创建apache项目
[ans@node1 myroles]$ ansible-galaxy init apache
- Role apache was created successfully
#初始化项目的目录层级
[ans@node1 myroles]$ tree -C apache/
apache/ <---具体的角色项目名称,比如nginx、tomcat、php(自由设置)
├── defaults <--用于为当前角色设定默认变量,此目录应当包含一个main.yml文件
│ └── main.yml <--main.yml,类似代码中的主函数,进行统一管理
├── files <--用来存放由copy模块或script模块等模块调用的文件
├── handlers <--用于定义此角色中触发条件时执行的动作,此目录应当包含一个main.yml文件
│ └── main.yml
├── meta <--用于定义此角色的特殊设定及其依赖关系,此目录应当包含一个main.yml文件
│ └── main.yml
├── README.md <--说明文件
├── tasks <--用于定义当前角色的任务列表,此目录应当包含一个main.yml文件
│ └── main.yml
├── templates <--用来存放jinjia2模板,template模块会自动在此目录中寻找jinjia2模板文件
├── tests <--用于存放测试role本身功能的playbook和主机定义文件,在开发测试阶段比较常用,此目录应当包含一个main.yml文件和自身资源设定invetory
│ ├── inventory
│ └── test.yml
└── vars <--用于定义此角色用到的变量,此目录应当包含一个main.yml文件
└── main.yml
#官网给出的项目文件层级样例
site.yml #应该是同一目录层级的3个剧本
webservers.yml
fooservers.yml
roles/ #roels目录,可以看出与剧本是同一层级
common/ #common项目
files/ #项目中的指定文件目录
templates/
tasks/
handlers/
vars/
defaults/
meta/
webservers/ #webservers项目,common项目都在roles目录中,同一目录层级
files/
templates/
tasks/
handlers/
vars/
defaults/
meta/
将之前写好的一个简单剧本、相关模板与变量,进行拆分迁移写入roles中
####################### 剧本 #######################
[ans@node1 ansible]$ vim playbook.yml
- hosts: app
vars:
web_package: httpd
web_service: httpd
tasks:
- name: install apache
yum:
name: "{{ web_package }}"
state: installed
- name: start apache
service:
name: "{{ web_service }}"
state: started
- name: create index.html
copy:
content: "{{ ansible_hostname }}\n"
dest: /var/www/html/index.html
- name: config apache
template:
src: files/httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: restart apache
handlers:
- name: restart apache
service:
name: "{{ web_service }}"
state: restarted
tags: web
- hosts: all
tasks:
- name: only node1 install haproxy
block:
- name: install haproxy
yum:
name: haproxy
state: installed
- name: start haproxy
service:
name: haproxy
state: started
- name: config haproxy
template:
src: files/haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
notify: reload haproxy
when: ansible_hostname == 'node1'
handlers:
- name: reload haproxy
service:
name: haproxy
state: reloaded
tags: haproxy
####################### 模板 #######################
[ans@node1 ansible]$ cat files/httpd.conf.j2
…… #httpd配置文件,省略部分未修改内容
Listen {{ http_host }}:{{ http_port }}
……
[ans@node1 ansible]$ vim files/haproxy.cfg.j2
…… #配置文件,省略部分未修改内容
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:80
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
{% for host in groups['app']%}
server {{hostvars[host]['ansible_facts']['hostname']}} {{hostvars[host]['ansible_facts']['eth0']['ipv4']['address']}}:80 check
{% endfor %}
#仅为了测试,httpd所以进行了些无谓的拆分
#目录结构
[ans@node1 tasks]$ tree
.
├── index.yml
├── install.yml
├── main.yml
└── start.yml
#main.yml
[ans@node1 tasks]$ cat main.yml
---
- include: install.yml
- include: start.yml
- include: index.yml
#安装task
[ans@node1 tasks]$ cat install.yml
- name: install apache
yum:
name: "{{ web_package }}"
state: installed
#启动task
[ans@node1 tasks]$ cat start.yml
- name: start apache
service:
name: "{{ web_service }}"
state: started
#配置task
[ans@node1 tasks]$ cat index.yml
- name: create index.html
copy:
content: "{{ ansible_hostname }}\n"
dest: /var/www/html/index.html
- name: config apache
template:
src: httpd.conf.j2 #注意此次调用模板,直接写文件名即可,应为已设定指定同项目下的templates目录
dest: /etc/httpd/conf/httpd.conf
notify: restart apache
#变量
[ans@node1 apache]$ cat vars/main.yml
---
# vars file for apache
web_package: httpd
web_service: httpd
#触发器
[ans@node1 apache]$ cat handlers/main.yml
---
# handlers file for apache
- name: restart apache
service:
name: "{{ web_service }}"
state: restarted
#模板直接拷贝进入templates文件中
[ans@node1 apache]$ ls templates/
httpd.conf.j2
[ans@node1 ansible]$ ls
ansible.cfg files inventory myroles playbook.yml roles_start.yml test.file vars
#我的启动脚本与管理的myroles文件夹在同一目录中
[ans@node1 ansible]$ cat roles_apache_start.yml
---
- hosts: app
roles:
- role: apache
[ans@node1 ansible]$ ansible-playbook roles_apache_start.yml
PLAY [app] *************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************
ok: [node3]
ok: [node2]
TASK [apache : install apache] *****************************************************************************************************
changed: [node2]
changed: [node3]
TASK [apache : start apache] *******************************************************************************************************
changed: [node2]
changed: [node3]
TASK [apache : create index.html] **************************************************************************************************
ok: [node3]
ok: [node2]
TASK [apache : config apache] ******************************************************************************************************
changed: [node3]
changed: [node2]
RUNNING HANDLER [apache : restart apache] ******************************************************************************************
changed: [node3]
changed: [node2]
PLAY RECAP *************************************************************************************************************************
node2 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#测试正常
#这次就不细拆分了,全部写在main中
[ans@node1 ansible]$ cat myroles/haproxy/tasks/main.yml
---
# tasks file for myroles/haproxy
- name: install haproxy
yum:
name: haproxy
state: installed
- name: start haproxy
service:
name: haproxy
state: started
- name: config haproxy
template:
src: files/haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
notify: reload haproxy
#在task中就不需要原有的when条件判断了,将判断写在启动文件中
#没有独立变量
#拷贝模板进入templates中
[ans@node1 ansible]$ ls myroles/haproxy/templates/
haproxy.cfg.j2
#设置触发器
[ans@node1 ansible]$ cat myroles/haproxy/handlers/main.yml
---
# handlers file for myroles/haproxy
- name: reload haproxy
service:
name: haproxy
state: reloaded
[ans@node1 ansible]$ cat roles_haproxy_start.yml
---
- hosts: all ##所有主机节点
roles:
- role: apache
when: ansible_hostname in groups['app'] ##apache这个角色面向的是app组的成员
- role: haproxy
when: ansible_hostname == 'node1' ##haproxy面向的是节点node1
[ans@node1 ansible]$ ansible-playbook roles_start.yml
PLAY [all] *************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************
ok: [node2]
ok: [node3]
ok: [node1]
TASK [apache : install apache] *****************************************************************************************************
skipping: [node1]
ok: [node2]
ok: [node3]
TASK [apache : start apache] *******************************************************************************************************
skipping: [node1]
ok: [node3]
ok: [node2]
TASK [apache : create index.html] **************************************************************************************************
skipping: [node1]
ok: [node3]
ok: [node2]
TASK [apache : config apache] ******************************************************************************************************
skipping: [node1]
ok: [node2]
ok: [node3]
TASK [haproxy : install haproxy] ***************************************************************************************************
skipping: [node2]
skipping: [node3]
changed: [node1]
TASK [haproxy : start haproxy] *****************************************************************************************************
skipping: [node2]
skipping: [node3]
changed: [node1]
TASK [haproxy : config haproxy] ****************************************************************************************************
skipping: [node2]
skipping: [node3]
changed: [node1]
RUNNING HANDLER [haproxy : reload haproxy] *****************************************************************************************
changed: [node1]
PLAY RECAP *************************************************************************************************************************
node1 : ok=5 changed=4 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0
node2 : ok=5 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
node3 : ok=5 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
#执行正常
[ans@node1 ansible]$ curl node1
node2
[ans@node1 ansible]$ curl node1
node3
#可以调度轮询