Ansible detailed explanation (15) - Ansible Role combat

Today, I will continue to introduce Linux operation and maintenance related knowledge. The main content of this article is the role configuration in Ansible.

1. Practical goals and ideas

Today I bring you another Role combat of Ansible. From the perspective of specific Role application, this article implements the standardized Ansible Role combat, and completes the installation of Apache services through Role.
To achieve this, we first create the apache subdirectory in the /etc/ansible/roles/ directory as a Role. Then in this directory, create subdirectories such as vars, files, handlers, tasks, templates, meta, etc. Then copy the relevant configuration files to the files folder, and write the relevant variables and Playbook files.

2. Role preparation

First, let's create the relevant folder and execute the command:

mkdir -p /etc/ansible/roles/apache/{
    
    vars,files,tasks,meta,template,default,handlers}

After the creation is completed, it is as follows:
insert image description here
Then, we install the Apache service on the Ansible device, and modify its configuration file to change the three places of Listen, User and Group to { { listen_port }}, { { username }} and { { groupname }}. Then rename it to httpd.conf.j2 and put it in the templates directory.

3. Playbook writing

First, we first write the variable file of Role, create vars/main.yml, and write the following content:

listen_port: 80
username: pzz
groupname: pzz

After completion, we write the Taks file of Role, create tasks/prepare.yml, and write the following content:

- name: Shutdown Firewall
  shell: systemctl stop firewalld; iptables -F; setenforce 0
- name: Create User nad Group
  shell: useradd pzz -M -s /sbin/nologin

Create tasks/install.yml and write the following:

- name: Install Apache
  yum: name=httpd state=installed
- name: Config Apache
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify: Restart Apache

Create the main file taks/main.yml, import the above two task files, and write the following content:

- include: prepare.yml
- include: install.yml

Next, we create the handler file, create handlers/main.yml, and write the following:

- name: Restart Apache
  service: name=httpd state=restarted

Finally, let's execute the Role, execute the command as follows:

ansible-playbook /etc/ansible/roles/apache/main.yml

The execution results are as follows: It can be
insert image description here
seen that our Ansible Role was successfully executed!
Originality is not easy, please indicate the source for reprinting: https://blog.csdn.net/weixin_40228200

Guess you like

Origin blog.csdn.net/weixin_40228200/article/details/123579007