Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build ansible roles

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to build ansible roles, the article is very detailed, has a certain reference value, interested friends must read it!

Ansible roles

In practical work, a complete project is actually a combination of many functions. If all the functions are written in a playbook, there will be a series of problems, such as high code coupling, long playbook, high maintenance cost, low flexibility and so on. The use of roles can skillfully solve this series of problems. Roles is a new function added after the ansible1.2 version, which is suitable for the orchestration architecture of large-scale project playbook.

Ansible roles directory structure

Roles can automatically load variable files, task and handlers according to the hierarchical structure. To put it simply, roles is by placing variables, files, tasks, modules, and processors in separate directories, and you can easily include them. Roles is generally used in scenarios where services are built on hosts, but can also be used in scenarios such as building daemons. The meaning of each directory in roles/ common/ files/ templates/ tasks/ handlers/ vars/ defaults/ meta/ web/ files/ templates/ tasks/ handlers/ vars/ defaults/ meta/roles explains files: used to store files called by copy module or script module. Templates: used to store jinjia2 templates, the template module will automatically look for jinjia2 template files in this directory. Tasks: this directory should contain a main.yml file that defines the task list for this role, and this file can use include to contain other task files located in this directory. Handlers: this directory should contain a main.yml file that defines the actions to be performed when a condition is triggered in this role. Vars: this directory should contain a main.yml file that defines the variables used by this role. Defaults: this directory should contain a main.yml file that sets the default variables for the current character. Meta: this directory should contain a main.yml file that defines the special settings for this role and their dependencies. To use roles in a playbook: 1) create a directory with the roles command. Mkdir / etc/ansible/roles/-p # yum defaults to 2) create a global variable directory. The mkdir / etc/ansible/group_vars/-ptouch / etc/ansible/group_vars/all # filename is defined by yourself. 3) create a directory in the roles directory that commands with each role name, such as httpd. Mkdir / etc/ansible/roles/common-p4) create files, handlers, tasks, templates, meta, defaults, and vars directories in the directory of each role command. Directories that are not needed can be created as empty directories, but not without. Mkdir / etc/ansible/roles/httpd/ {files,templates,tasks,handlers,vars,defaults,meta}-pmkdir / etc/ansible/roles/mysql/ {files,templates,tasks,handlers,vars,defaults,meta}-p5) create main.yml files under the handlers, tasks, meta, defaults, and vars directories of each role, which must not be customized. Touch / etc/ansible/roles/httpd/ {defaults,vars,tasks,meta,handlers} / main.ymltouch / etc/ansible/roles/mysql/ {defaults,vars,tasks,meta,handlers} / main.yml6) invokes the roles in the playbook file. Vi / etc/ansible/site.yml---- hosts: webserver remote_user: root roles:-httpd-mysql role management, roles installation build LAMP architecture [root@localhost ~] # vim / etc/ansible/hosts [ftpserver] 192.168.136.25 create directory Subdirectory mkdir / etc/ansible/roles/httpd/ {files,templates,tasks,handlers,vars,defaults,meta}-pmkdir / etc/ansible/roles/mysql/ {files,templates,tasks,handlers,vars,defaults,meta}-pmkdir / etc/ansible/roles/php/ {files,templates,tasks,handlers,vars,defaults,meta}-ptouch / etc/ansible/roles/httpd/ {defaults,vars,tasks,meta,handlers} / main.ymltouch / etc/ansible/roles/mysql/ {defaults,vars,tasks,meta Handlers} / main.ymltouch / etc/ansible/roles/php/ {defaults,vars,tasks,meta Handlers} / main.yml#apachej role [root@localhost roles] # cd / etc/ansible/roles/httpd/tasks/ [root@localhost tasks] # vim main.yml-name: install apache yum: pkg= {{servername}} state=latest# definition oot@localhost tasks [r oot@localhost tasks] # cd.. / vars/ [root@localhost vars] # vim main.yml servername: httpd#mysql role [root@localhost vars] # cd.. /. / mysql/tasks/ [root@localhost tasks] # vim main.yml-name : install mysqld yum: pkg= {{servername}} state=latest [root@localhost tasks] # vim.. / vars/main.yml servername: mariadb*#php role [root@localhost tasks] # cd.. /.. / php/tasks/ [root@localhost tasks] # vim main.yml-name: install php yum: pkg= {{servername}} state=latest [root@localhost tasks] # cd.. / vars/ [root@localhost vars] # vim main.yml servername: php# create a master script to control these services Do [root@localhost vars] # cd / etc/ansible/ [root@localhost ansible] # vim site.yml-hosts: ftpserver remote_user: root roles:-httpd-mysql-php [root@localhost ansible] # ansible-playbook site.yml PLAY [ftpserver] * * * TASK [Gathering Facts] * ok: [192.168.136 .251] TASK [httpd: install apache] * changed: [192.168.136.251] TASK [mysql: install mysqld] * * changed: [192.168.136.251] TASK [php: install php] * * * * changed: [192.168.136.251] PLAY RECAP * * 192.168.136.251: the above ok=4 changed=3 unreachable=0 failed=0 is all the content of the article "how to build ansible roles" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report