ansible-学习总结(roles概念与⽬录结构、roles实践)
⽂章⽬录
Roles
概念
由来:ansible⾃动化运⾏,基础由AD-Hoc命令来完成,在命令变多时,产⽣了playbook进⾏管理任务,简单任务使⽤playbook可以轻松处理,但是有复杂任务时单个playbook不可以胜任了,这时需要把多个playbook进⾏组合,少量⽤include将剧本中任务互相关联即可完成,但是playbook还在增多的情况时就不⽅便管理了,这时引⼊roles对playbook进⾏有效组织就⼗分必要了
Roles:⾓⾊,是ansible⾃1.2版本开始引⼊的新特性
⽬的:⽤于层次性,结构化地组织playbook,roles能够根据层次型结构⾃动装载变量、⽂件、任务、模块及触发器
⽅法:roles通过分别将放置于变量、⽂件、任务、模块及触发器单独的⽬录中,并可以便捷地include它们的⼀种机制
应⽤:⾓⾊⼀般⽤于基于主机构建服务的场景中、但也可以是⽤于构建守护进程等场景中
注意区分include与import模块(虽然都是task关联使⽤,但是差距很⼤)
import_tasks(Static):静态⽅法会在playbooks解析阶段将⽗task变量和⼦task变量全部读取并加载,这时候如果⽗与⼦task都定义了相同变量,⼦task变量为最终值,再继续执⾏⽗task时,会影响变量相关判断条件
include_tasks(Dynamic):动态⽅法则是在执⾏play之前才会加载⾃⼰变量,即⼀个⽗与⼦task执⾏时都是只使⽤⾃⼰的变量,不会互相进⾏⼲扰
官⽅roles⽹站及使⽤⼯具
Ansible Galaxy(galaxy.ansible)是⼀个官⽹提供的免费共享和下载 Ansible ⾓⾊的⽹站,可以帮助我们更好的定义和学习roles
ansible-galaxy命令:默认与galaxy.ansible⽹站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  <--⽤于为当前⾓⾊设定默认变量,此⽬录应当包含⼀个l⽂件
│└── l  <--l,类似代码中的主函数,进⾏统⼀管理
├── files    <--⽤来存放由copy模块或script模块等模块调⽤的⽂件
├── handlers  <--⽤于定义此⾓⾊中触发条件时执⾏的动作,此⽬录应当包含⼀个l⽂件
│└── l
├── meta    <--⽤于定义此⾓⾊的特殊设定及其依赖关系,此⽬录应当包含⼀个l⽂件
│└── l
├── README.md  <--说明⽂件
├── tasks    <--⽤于定义当前⾓⾊的任务列表,此⽬录应当包含⼀个l⽂件
│└── l
├── templates  <--⽤来存放jinjia2模板,template模块会⾃动在此⽬录中寻jinjia2模板⽂件
├── tests    <--⽤于存放测试role本⾝功能的playbook和主机定义⽂件,在开发测试阶段⽐较常⽤,此⽬录应当包含⼀个l⽂件和⾃⾝资源设定invetory │├── inventory
│└── l
└── vars    <--⽤于定义此⾓⾊⽤到的变量,此⽬录应当包含⼀个l⽂件
└── l
#官⽹给出的项⽬⽂件层级样例
roles/      #roels⽬录,可以看出与剧本是同⼀层级
common/      #common项⽬
files/      #项⽬中的指定⽂件⽬录
templates/
tasks/
handlers/
vars/
defaults/
meta/
webservers/    #webservers项⽬,common项⽬都在roles⽬录中,同⼀⽬录层级
files/
templates/
tasks/
handlers/
vars/
defaults/
meta/
roles实践
将之前写好的⼀个简单剧本、相关模板与变量,进⾏拆分迁移写⼊roles中
源剧本
>>>>### 剧本 >>>>###
[ans@node1 ansible]$ l
- hosts: app
-
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: f.j2
dest: /etc/httpd/f
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 f.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 %}
拆分迁移
先制作apache项⽬的roles
1. 迁移tasks
#仅为了测试,httpd所以进⾏了些⽆谓的拆分
#⽬录结构
[ans@node1 tasks]$ tree
.
├── l
├── l
├── l
└── l
#l
[ans@node1 tasks]$ l
---
- include: l
- include: l
-
include: l
#安装task
[ans@node1 tasks]$ l
- name: install apache
yum:
name: "{{ web_package }}"
state: installed
#启动task
[ans@node1 tasks]$ l
- name: start apache
service:
name: "{{ web_service }}"
state: started
#配置task
[ans@node1 tasks]$ l
- name: create index.html
copy:
content: "{{ ansible_hostname }}\n"
dest: /var/www/html/index.html
- name: config apache
template:
src: f.j2      #注意此次调⽤模板,直接写⽂件名即可,应为已设定指定同项⽬下的templates⽬录
dest: /etc/httpd/f
notify: restart apache
免费管理系统html模板2. 设置变量,模板,触发器
#变量
[ans@node1 apache]$ cat l
---
# vars file for apache
web_package: httpd
web_service: httpd
#触发器
[ans@node1 apache]$ cat l
-
--
# handlers file for apache
- name: restart apache
service:
name: "{{ web_service }}"
state: restarted
#模板直接拷贝进⼊templates⽂件中
[ans@node1 apache]$ ls templates/
3. 设置启动roles的剧本,进⾏测试
注意启动剧本,应在建⽴在ploybook的层级中
[ans@node1 ansible]$ ls
ansible.cfg  files  inventory  myroles  l  l  test.file  vars
#我的启动脚本与管理的myroles⽂件夹在同⼀⽬录中
[ans@node1 ansible]$ cat roles_l
---
- hosts: app
roles:
- role: apache
[ans@node1 ansible]$ ansible-playbook roles_l
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
#测试正常

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论