Get the App
SLTechnology News&Howtos  ›  Servers  › 

Ansible role Management-Roles (example demonstration!!)

Shulou Source: shulou.com Published: 2022-06-02 02:58:46 10月03日 Update

An overview of Roles Roles can automatically load variable files, task, handlers, and so on, based on a hierarchical structure. To put it simply, Roles is simply 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 it can also be used in scenarios such as building daemons. Roles directory name specific meaning files is used to store files called by copy module or script module templates is used to store j regular templates, template module will automatically look for regular template files tasks in this directory this directory should contain a main.yml file, which should be used to define the task list for this role, this file can use include to contain other task files located in this directory handlers this directory should contain a main.yml file This directory should contain a main.yml file to define the variables used in this role, defaults, and this directory should contain a main.yml file for setting the default variable meta for the current role. This directory should contain a main.yml file. To define the special settings for this role and its dependencies to use Roles in playbook to create a directory with the roles command # by default, mkdir / etc/ansible/roles/-p creates a global variable directory, mkdir / etc/ansible/group_vars/-ptouch / etc/ansible/group_vars/all# file name, when referencing, be careful to create a directory with the name of each role in the roles directory For example, httpdmkdir / etc/ansible/roles/common-p creates 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. However, it is necessary to create mkdir / etc/ansible/roles/httpd/ {files,templates,tasks,handlers,vars,defaults,meta}-pmkdir / etc/ansible/roles/mysql/ {files,templates,tasks,handlers,vars,defaults,meta}-p to create main.yml files in the handlers, tasks, meta, defaults, vars directories of each role. Never customize touch / etc/ansible/roles/httpd/ {defaults,vars,tasks,meta,handlers} / main.ymltouch / etc/ansible/roles/mysql/ {defaults,vars,tasks,meta,handlers} / main.yml in the playbook file Call each role vim / etc/ansible/site.yml- hosts: webserver remote_user: root roles:-httpd-mysql to install and build LAMP architecture instance demonstration to create service component working directory 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}-p create an empty yml file touch / 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 write httpd module # write a simple tasks/main.ymlvim / etc/ansible/roles/httpd/tasks/main.yml- name: ensure apache is at the latest version yum: pkg= {{pkg}} state=latest# define variables, which can be defined in global variables Can also be defined in the roles role variable Generally defined in role variables vim / etc/ansible/roles/httpd/vars/main.ymlpkg: httpd write mysql module vim / etc/ansible/roles/mysql/tasks/main.yml- name: ensure mysql is at the latest version yum: pkg= {{pkg}} state=latest# definition variable vim / etc/ansible/roles/mysql/vars/main.ymlpkg: mariadb* write php module vim / etc/ansible/roles/php/tasks/main.yml- name: ensure php is at the latest version Yum: pkg= {{pkg}} state=latest# definition variable vim / etc/ansible/roles/php/vars/main.ymlpkg: php* Writing roles instance vim / etc/ansible/site.yml- hosts: webserver remote_user: root roles:-httpd-mysql-php# execution script ansible-playbook site.yml is executed on the remote host after execution Query to verify whether the specified service installs rpm-Q httpdhttpd-2.4.6-90.el7.centos.x86_64rpm-Q mariadbmariadb-5.5.64-1.el7.x86_64rpm-Q phpphp-5.4.16-46.1.el7_7.x86_64. Thank you for reading!

Tags: Directory file role variable module command service instance host task global name scene template regular presentation convenience special script action Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker Shulou Information Apple macOS