13_Ansible role, create directory structure, Roles dependency; Playbook reference materials: facts, with_item, jinja template, role role

16.Ansible role
16.1.Ansible Roles introduction
16.2. Create directory structure
16.3.Ansible Roles dependencies
17. Other reference materials
17.1.Playbook reference materials
17.2.Ansible facts
17.3. Judgment statement when
17.4.with_items
17.5.ansible jinja template
17.6.ansible role role
17.7. variables other reference documents

16.Ansible role

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

16.1. Introduction to Ansible Roles

Roles Whether it is Ansible or saltstack, when I write one-click deployment, it is impossible to write all the steps into a 'script' file. We definitely need to separate and decouple different working modules. , then when it comes to decoupling, we need to use the official recommendation of roles, because the directory structure of roles is clearer.

For example: We previously recommended that you write a base.yml to write all the basic optimization projects. In fact, it is very tasteless to put everything in it. Why don’t we separate all these functions, and whoever needs to use it can just call it.

Suggestion: It is best to use only one task for each role, which is convenient for us to call and can achieve good decoupling. (SOA)

16.2. Create directory structure

[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

Directory structure description:

[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 dependencies

rolesAllows you to automatically import other roles when you use roles. Role dependencies are stored in the meta/main.yml file in the roles directory.

For example: push wordpress and decompress it. As a prerequisite, you must install nginx and php, and run the service to run the wordpress page. At this time, we can define roles that depend on nginx and php in the roles of wordpress

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

If the main.yml file in the meta directory is written, Ansible will automatically execute the dependencies file in the main.yml file in the meta directory first. As shown above, it will first execute the installation of nginx and php.

17. Other references

17.1. Playbook References

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. Judgment statement 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 template

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 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. Other reference documents for variables

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/

Guess you like

Origin blog.csdn.net/toto1297488504/article/details/132235972