13_Ansible role、创建目录结构、Roles依赖关系;Playbook参考资料:facts、with_item、jinja模板、role角色

16.Ansible role

16.1.Ansible Roles介绍

16.2.创建目录结构

16.3.Ansible Roles依赖关系

17.其它参考资料

17.1.Playbook参考资料

17.2.Ansible facts

17.3.判断语句 when

17.4.with_items

17.5.ansible jinja模板

17.6.ansible role角色

17.7.变量其它参考文档

16.Ansible role

转自:https://blog.csdn.net/weixin_48981270/article/details/117756914

16.1.Ansible Roles介绍

roles不管是Ansible还是saltstack,我在写一键部署的时候,都不可能把所有的步骤全部写入到一个'剧本'文件当中,我们肯定需要把不同的工作模块,拆分开来,解耦,那么说到解耦,我们就需要用到roles官方推荐,因为roles的目录结构层次更加清晰。

例如:我们之前推荐大家写一个base.yml里面写所有基础优化的项目,其实把所有东西摞进去也是很鸡肋的,不如我们把这些功能全部拆分开,谁需要使用,就调用即可。

建议:每个roles最好只使用一个tasks这样方便我们去调用,能够很好的做到解耦。(SOA)

16.2.创建目录结构

powershell 复制代码
[root@node4 workspace]# mkdir nginx/{tasks,files,templates,vars,handlers,meta} -p
[root@node4 workspace]# ls
nginx
[root@node4 workspace]# tree nginx/
nginx/
|-- files
|-- handlers
|-- meta
|-- tasks
|-- templates
`-- vars

6 directories, 0 files
[root@node4 workspace]# ls
nginx
[root@node4 workspace]# rm -rf nginx
[root@node4 workspace]# ansible-galaxy init nginx    (使用命令的时候创建)
- Role nginx was created successfully
[root@node4 workspace]# ll
total 0
drwxr-xr-x 1 root root 4096 10月 27 18:45 nginx
[root@node4 workspace]# tree
.
`-- nginx
    |-- defaults
    |   `-- main.yml
    |-- files
    |-- handlers
    |   `-- main.yml
    |-- meta
    |   `-- main.yml
    |-- README.md
    |-- tasks
    |   `-- main.yml
    |-- templates
    |-- tests
    |   |-- inventory
    |   `-- test.yml
    `-- vars
        `-- main.yml

9 directories, 8 files

目录结构说明:

powershell 复制代码
[root@m01 roles]# tree nginx
nginx					#项目目录名称
├── defaults			#默认的变量(优先级很低)
│   └── main.yml
├── files				#存放文件,使用copy模块时自动获取
├── handlers			#存放触发器的配置
│   └── main.yml
├── meta				#依赖的服务,执行该项目时先执行其他的项目
│   └── main.yml
├── README.md
├── tasks				#默认执行的playbook
│   └── main.yml
├── templates			#存放jinja2模板,使用template模块时自动获取
├── tests
│   ├── inventory
│   └── test.yml
└── vars				#存放变量
    └── main.yml

16.3.Ansible Roles依赖关系

roles允许你再使用roles时自动引入其他的roles。role依赖关系存储在roles目录中meta/main.yml文件中。

例如:推送wordpress并解压,前提条件,必须要安装nginx和php,把服务跑起来,才能运行wordpress的页面,此时我们就可以在wordpress的roles中定义依赖nginx和php的roles

powershell 复制代码
[root@m01 roles]# vim /etc/ansible/roles/wordpress/meta/main.yml
dependencies:
  - { role: nginx }
  - { role: php }

如果编写了meta目录下的main.yml文件,那么Ansible会自动先执行meta目录中main.yml文件中的dependencies文件,如上所示,就会先执行nginx和php的安装。

17.其它参考资料

17.1.Playbook参考资料

https://blog.51cto.com/u_13630803/2154192

https://www.cnblogs.com/yanjieli/p/10969299.html

17.2.Ansible facts

https://www.cnblogs.com/lxmhhy/p/6813953.html

https://blog.csdn.net/woshizhangliang999/article/details/105759379

https://www.cnblogs.com/zhanglianghhh/p/12776236.html

https://www.cnblogs.com/nb-blog/p/10565658.html

https://www.codercto.com/a/60267.html

17.3.判断语句 when

https://blog.csdn.net/a13568hki/article/details/103874588

https://www.jianshu.com/p/b172100de444

http://www.linuxboy.net/ansiblejc/147138.html

https://www.cnblogs.com/gaoyuechen/p/7776373.html

17.4.with_items

http://www.linuxe.cn/post-279.html

https://blog.csdn.net/weixin_34116110/article/details/92813078

17.5.ansible jinja模板

https://blog.csdn.net/weixin_48981270/article/details/117756914

https://www.cnblogs.com/yxh168/p/14822773.html

https://blog.csdn.net/weixin_43384009/article/details/105301811

https://www.cnblogs.com/yang-dan/p/12091656.html

https://www.jianshu.com/p/cceca6036a51

https://blog.csdn.net/hanguofei/article/details/102555314

17.6.ansible role角色

https://www.pianshen.com/article/73421469885/

https://blog.csdn.net/woshizhangliang999/article/details/106005990

https://www.jianshu.com/p/157ed21bf47d

https://blog.51cto.com/u_13769014/2122388

https://blog.51cto.com/steed/2436930

https://www.pianshen.com/article/4058846828/

http://t.zoukankan.com/nineep-p-9065874.html

17.7.变量其它参考文档

https://www.cnblogs.com/mauricewei/p/10054300.html

https://www.cnblogs.com/f-ck-need-u/p/7571974.html

https://www.cnblogs.com/deny/p/12394956.html

https://www.zsythink.net/archives/2655/

相关推荐
一切皆文件linux1 天前
网站集群批量管理-密钥认证与Ansible模块
ansible
让美好继续发生2 天前
ansible学习
学习·ansible
有谁看见我的剑了?3 天前
ansible学习之 Facts
ansible
peanutfish3 天前
Chapter 4 RH294 RHEL Automation with Ansible
linux·ansible·yaml
henan程序媛3 天前
jenkins项目发布基础
运维·gitlab·ansible·jenkins
pyliumy5 天前
ansible 配置
大数据·ansible
运维小白。。5 天前
自动化运维工具 Ansible
运维·自动化·ansible
weixin_438197385 天前
ansible之playbook\shell\script模块远程自动安装nginx
linux·服务器·ansible
peanutfish5 天前
Chapter 5 RH294 RHEL Automation with Ansible
linux·ansible·yaml
人类群星闪耀时6 天前
自动化运维的利器:Ansible、Puppet和Chef详解
运维·自动化·ansible