The use of Ansible automated operation and maintenance tool roles (3)

Ansible之Roles

Introduction to Roles

The new features introduced by ansible since version 1.2 are used to organize playbooks hierarchically and structured. Roles can automatically load variable files, tasks and handlers according to the hierarchical structure. To use roles, you only need to use the include directive in the playbook. Simply put, roles are a mechanism by which variables, files, tasks, templates, and processors are placed in separate directories, and they can be included conveniently. Roles are generally used in scenarios where services are built based on hosts, but they can also be used in scenarios such as building daemons. It is mainly used when the code reuse of the scene is high.

Roles is a collection of multiple roles. Multiple roles can be placed in independent subdirectories under the roles directory.

ls -l roles/
  mysql/
  httpd/
  nginx/

directory role

Take roles/mysql/: project as an example

Directory name effect
files/ Store files called by modules such as copy or script
templates/ The template module finds the directory of the required template file
tasks/ Define the basic elements of task and role. At least one file named main.yml should be included; other files need to be included in this file through include
handlers/ At least one main.yml file should be included; other files need to be included in this file through include
whose/ Define variables, including at least one main.yml file; other files need to be included in the file again through include
meta/ Define the special settings of the current role and its dependencies. At least one file named main.yml should be included. Other files need to be included in this file through include
default/ Setting the default variable is to use the main.yml file in this directory, which has a lower priority than the vars directory

Create a playbook through roles

  • Create the corresponding directory
cd /etc/ansible/roles/
mkdir httpd/{tasks,files,vars,handlers} -p
  • Create a script for installing the service
vim /etc/ansible/roles/httpd/tasks/install.yml
---
 - name: install httpd service
     yum: name=httpd
  • Create profile playbook
vim /etc/ansible/roles/httpd/tasks/config.yml
---
- name: httpd config
  copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
  notify: restart-httpd
  • Create httpd homepage file script
vim /etc/ansible/roles/httpd/tasks/index.yml
---
- name: httpd index
  copy: src=index.html dest=/var/www/html/
  • Create a startup service script
vim /etc/ansible/roles/httpd/tasks/service.yml
---
- name: start httpd
  service: name=httpd state=started enabled=yes
  • Create the main.yml file of the task
vim /etc/ansible/roles/httpd/tasks/main.yml
---
- include: install.yml
- include: config.yml
- include: index.yml
- include: serivce.yml

The main.yml file should pay attention to the order of calling, the first executed first

  • Create the handlers file
vim /etc/ansible/roles/httpd/handlers/main.yml
---
- name: restart-httpd
  service: name=httpd state=restartd
  • Create httpd configuration file
vim /etc/ansible/roles/httpd/files/httpd.conf
---
ServerRoot "/etc/httpd"
Listen 87
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
    AllowOverride none
    Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
  • Create httpd homepage file
vim /etc/ansible/roles/httpd/files/index.html
---
<h1>Hello</h1>
  • Create httpd script call role
vim /etc/ansible/role_http.yml
---
- hosts: web
  remote_user: root
  roles:
    - httpd

The files called by the script should be placed at the same level as the roles directory

  • Execute the script
ansible-playbook role_http.yml

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_33235529/article/details/114665997