深圳网站建设燦,做个网站网站需要多少钱,去除wordpress版本号,注册企业营业执照需要什么条件12.2handles
handles触发器(条件)#xff0c;满足条件后再做什么事情应用场景#xff1a;想表示#xff1a;配置文件变化#xff0c;再重启服务 配置handlers之前#xff0c;每次运行剧本都会重启nfs#xff0c;无论配置文件是否变化。
[rootm01 /server/ans/playbook]…12.2handles
handles触发器(条件)满足条件后再做什么事情应用场景想表示配置文件变化再重启服务 配置handlers之前每次运行剧本都会重启nfs无论配置文件是否变化。
[rootm01 /server/ans/playbook]# cat 17-handler-nfs-server.yml
---
- hosts: dbtasks:- name : 01.分发配置copy:src: ./exportsdest: /etc/exportsbackup: yes- name : 05 启动服务nfssystemd:name: nfsenabled: yesstate: restarted
不管exports配置有无修改都会执行05启动nfs服务配置了handerles,只有服务配置文件变化了再重启服务。
[rootm01 /server/ans/playbook]# cat 17-handler-nfs-server.yml
---
- hosts: dbtasks:- name : 01.分发配置copy:src: ./exportsdest: /etc/exportsbackup: yesnotify:- restart nfs #与下面handlers的name一样handlers:- name: restart nfssystemd:name: nfsenabled: yesstate: restarted
12.3 when(判断)
when是ansible中的判断语句(条件语句) 实现对于某个模块在满足或不满足xxxxx条件下再执行 给web服务器或lb服务器配置nginx的yum源
when: ( ansible_distribution Ubuntu) #如果系统的发行版本是Ubuntu则运行模块ansible_distribution的ansible-facts变量when: ( ansible_hostname is match(web|lb) ) #主机名包含web或lb配置nginx源when: ( ansible_hostname is not match(web|lb) ) cat nginx.repo
[nginx]
baseurl http://nginx.org/packages/centos/$releasever/$basearch/
enabled 1
gpgcheck 0
name nginx stable repocat 18-when-fenfa-nginx-yum.yml
---
- hosts: alltasks:- name: 配置lb或者web的nginx yum源copy:src: ./nginx.repodest: /etc/yum.repos.d/nginx.repobackup: yeswhen: ( ansible_hostname is match(web|lb) )12.4 循环
批量启动重启服务 案例01-批量重启服务:crond,nfs,rpcbind
cat 19-item-restart-service.yml
---
- hosts: nfsgather_facts: notasks:- name: restart 多个服务systemd:name: {{ item }}state: restartedwith_items:- crond- rpcbind- nfs案例02-循环添加用户并指定uid
ansible中2个或多个变量的循环语句格式.
用户名uidhbinz12307zhangsan12380
cat 20-item-duo-var-useradd.yml
---
- hosts: alltasks:- name: 批量添加用户user:name: {{ item.name }}uid: {{ item.uid }}state: presentwith_items:- { name: hbinz , uid: 12307 }- { name: zhangsan , uid: 12308 }循环小结
with_items实现循环变量名字item多个变量循环了解即可 注这里的with_items可以替换成loops
12.5 Jinja2模板
经常使用在配置文件中让配置文件中包含变量
copy模块没有解析变量
template模块传输的时候解析配置文件变量(ansible)配置文件格式改为exports.j2cat 21-jinja-fenfa-conf.yml
---
- hosts: nfstasks:- name: 分发nfs配置文件,加主机名template:src: ./exports.j2dest: /etc/exports
ansible-playbook -i hosts 21-jinja-fenfa-conf.ymlnfs01的exports
[rootnfs01 ~]# cat /etc/exports
# nfs01 nfs配置文件
/data 172.16.1.0/24(rw,all_squash)
使用jinja2模板要求 配置文件必须要以.2结尾(ansible)分发文件的时候使用template模块用法与copy一致 案例02-jinja2循环
批量共享目录/data /backup /nfsdata
cat exports.j2
# {{ ansible_hostname}} nfs配置文件
{% for name in [/data,/backup,/nfsdata] %}
{{name}} 172.16.1.0/24(rw,all_squash)
{% endfor %}--------------------------------------------------
[rootnfs01 ~]# cat /etc/exports
# nfs01 nfs配置文件
/data 172.16.1.0/24(rw,all_squash)
/backup 172.16.1.0/24(rw,all_squash)
/nfsdata 172.16.1.0/24(rw,all_squash)核心掌握:
配置文件xxxx.j2template模块jinja2配置文件支持ans变量.
12.6 Roles
roles:规范剧本相关的目录.本质规定的几个专用的目录 按照初级方式写剧本
剧本nfs服务端
---
- hosts: nfstasks:- name: 01.部署nfs服务端软件yum:name: nfs-utilsstate: installed- name: 02.修改配置文件template:src: ./exports.j2dest: /etc/exportsbackup: yesnotify: - 04.启动服务-rpcbind-nfs服务- name: 03.创建对应的目录和,权限file:path: /data/owner: nfsnobodygroup: nfsnobodystate: directoryhandlers:- name: 04.启动服务-rpcbind-nfs服务systemd:name: {{ item }}enabled: yesstate: restartedwith_items:- rpcbind- nfsaroles结构
b环境准备及部署流程
1.先书写或拆分剧本中tasks的内容2.根据剧本分类存放配置文件模板文件(j2)3.根据剧本配置handlers的main.yaml文件4.书写剧本入口.与nfs-server目录同级
mkdir -p roles
cd roles
mkdir -p nfs-server/{files,templates,tasks,handlers}
[rootm01 /server/ans/playbook/roles]# tree -F
.
└── nfs-server/├── files/├── handlers/├── tasks/└── templates/c先书写拆分或拆分剧本中tasks的内容.
[rootm01 /server/ans/playbook/roles]# cat nfs-server/tasks/main.yml
- name: 01.部署nfs服务端软件yum:name: nfs-utilsstate: installed- name: 02.修改配置文件template:src: ./exports.j2dest: /etc/exportsbackup: yesnotify: - 04.启动服务-rpcbind-nfs服务- name: 03.创建对应的目录和,权限file:path: /data/owner: nfsnobodygroup: nfsnobodystate: directoryd根据剧本分类存放配置文件模板文件(j2)
[rootm01 /server/ans/playbook/roles]# cat nfs-server/templates/exports.j2
# {{ ansible_hostname}} nfs配置文件
/data/ 172.16.1.0/24(rw,all_squash)
[rootm01 /server/ans/playbook/roles]# tree -F
.
└── nfs-server/├── files/├── handlers/├── tasks/│ └── main.yml└── templates/└── exports.j25 directories, 2 filese根据剧本配置handlers的main.yml文件
[rootm01 /server/ans/playbook/roles]# cat nfs-server/handlers/main.yml
- name: 04.启动服务-rpcbind-nfs服务systemd:name: {{ item }}enabled: yesstate: restartedwith_items:- rpcbind- nfs
[rootm01 /server/ans/playbook/roles]# tree -F
.
└── nfs-server/├── files/├── handlers/│ └── main.yml├── tasks/│ └── main.yml└── templates/└── exports.j2f书写入口剧本与nfs-server目录同级
[rootm01 /server/ans/playbook/roles]# cat top.yml
---
- hosts: nfsroles:- role: nfs-server #与目录名字一致,ans才能找到下个入口
[rootm01 /server/ans/playbook/roles]# tree -F
.
├── nfs-server/
│ ├── files/
│ ├── handlers/
│ │ └── main.yml
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ └── exports.j2
└── top.ymlgroles执行流程
1.ansible-playbook -i host top.yml2.读取top.yml内容获取roles信息3.根据顺序先执行第1个role—nfs-server4.执行nfs-server找nfs-server对应的目录5.执行里面的tasks下面的main.yml6.执行mian.yml的时候遇到copy/template模块则找对应的目录copy(files)template(templates)找对应的文件7.执行main.yml的时候遇到notify则会找handlers下面的main.yml的内容进行匹配和执行。
12.7 Galaxy-了解
官方roles集合
ansible-galaxy install geerlingguy.nginx12.8 ansible-vault-了解
加密文件
ansible-vault encrypt hosts #加密配置文件设置密码
ansible --ask-vault-pass -i hosts all -m ping #使用ansible命令或ansible-playbook命令需要加上 --ask-vault-pass13.Ansible-进阶-优化
/etc/ansible/ansible.cfginventory /etc/ansible/hosts #指定的默认的主机清单. 未来可以修改为 ./hosts 就可以不用加上-i
forks 50 #并发数量. 可以增加这个数量获取更快批量管理效率.
sudo_user root #配置下被管理端具有sudo权限的用户,并且修改/etc/sudoers 注释掉requiretty
host_key_checking False #默认是True 连接新的主机要进行验证. 建议关闭,加速.
log_path /var/log/ansible.log #默认没有开启.
ssh_args -C -o ControlMasterauto -o
ControlPersist6d #连接的保持时间. 6d 10d
pipelining True #加速,加速连接合并不必要的连接. 要求:不能使用sudo,如果使用则不能开启.14.总结Ansblie
主机清单
模块
剧本
文章转载自: http://www.morning.rwfj.cn.gov.cn.rwfj.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.zpyh.cn.gov.cn.zpyh.cn http://www.morning.hjsrl.cn.gov.cn.hjsrl.cn http://www.morning.pfkrw.cn.gov.cn.pfkrw.cn http://www.morning.ztmkg.cn.gov.cn.ztmkg.cn http://www.morning.yqfdl.cn.gov.cn.yqfdl.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.fprll.cn.gov.cn.fprll.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.yrdn.cn.gov.cn.yrdn.cn http://www.morning.gfprf.cn.gov.cn.gfprf.cn http://www.morning.lydtr.cn.gov.cn.lydtr.cn http://www.morning.spbp.cn.gov.cn.spbp.cn http://www.morning.pwgzh.cn.gov.cn.pwgzh.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.ntzbr.cn.gov.cn.ntzbr.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.pdkht.cn.gov.cn.pdkht.cn http://www.morning.krywy.cn.gov.cn.krywy.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.ksjnl.cn.gov.cn.ksjnl.cn http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn http://www.morning.wrtpk.cn.gov.cn.wrtpk.cn http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.jtkfm.cn.gov.cn.jtkfm.cn http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.lkpzx.cn.gov.cn.lkpzx.cn http://www.morning.bmbnc.cn.gov.cn.bmbnc.cn http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.hctgn.cn.gov.cn.hctgn.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.kmqwp.cn.gov.cn.kmqwp.cn http://www.morning.zrbpx.cn.gov.cn.zrbpx.cn http://www.morning.mzwqt.cn.gov.cn.mzwqt.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.fmznd.cn.gov.cn.fmznd.cn http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.ndltr.cn.gov.cn.ndltr.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.pmhln.cn.gov.cn.pmhln.cn http://www.morning.jmllh.cn.gov.cn.jmllh.cn http://www.morning.ncrk.cn.gov.cn.ncrk.cn http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.gbxxh.cn.gov.cn.gbxxh.cn http://www.morning.vtbtje.cn.gov.cn.vtbtje.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn http://www.morning.kstlm.cn.gov.cn.kstlm.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.yslfn.cn.gov.cn.yslfn.cn http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.jtqxs.cn.gov.cn.jtqxs.cn http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.bkgfp.cn.gov.cn.bkgfp.cn http://www.morning.fesiy.com.gov.cn.fesiy.com http://www.morning.dbxss.cn.gov.cn.dbxss.cn http://www.morning.weiwt.com.gov.cn.weiwt.com http://www.morning.qprtm.cn.gov.cn.qprtm.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.ldcrh.cn.gov.cn.ldcrh.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn