Actual combat: batch deploy multiple LAMP environments using playbook

ansible actual combat case

Introduction to playbooks
1) Define tasks in playbooks:
-name: task description # task description information
module_name: module_args # required module name: module parameter
2) Ansible playbook execution command:
ansible-playbook site.yml

  1. A playbook is a list of one or more "plays". The main function of play is to dress up hosts that are grouped in advance as roles defined in advance through task s in ansible.

A large number of examples are provided on GitHub for your reference:

Practice 1: batch deploy multiple LAMP environments using playbook

First, let's introduce the functions of common Playbook folders:

  • Files: store source files and configuration files that need to be synchronized to remote servers;
  • Handlers: operations that need to be performed when the service configuration file changes, such as restarting the service, reloading the configuration file, and handlers ['h æ ndl] ə z] Handler
  • meta: role definition; can be left blank;
  • Tasks: tasks to be performed;
  • templates: a template file used to execute lamp installation, usually a script;
  • vars: variables defined in this installation

Building ideas

Idea: to build a lanp architecture, we need to:

Install services using yum or up2date

service startup

Copy copy the website

Define tasks in playbooks:
name: task description # task description information
module_name: module_args # required module name:

A large number of examples are provided on github for your reference: 4.2

To batch deploy multiple LAMP environments using Playbook
We can install the LAMP environment on the ansible server, and then copy the configuration file to the remote host through ansible

Step 1: install httpd software

[root@ansible ~]# yum -y install httpd -y

Part II: installing MySQL
[root@ansible ~]# yum install mariadb-server mariadb -y #Install mysql service

[root@ansible ~]# mkdir -p /mysqldata/data/ #Create a directory as the location of the data

[root@ansible ~]# chown -R mysql:mysql /mysqldata/ #to grant authorization

[root@ansible ~]# vim /etc/my.cnf #Change the data storage directory:
2 datadir=/var/lib/mysql

Change to: 2 datadir=/mydata/data/

[root@ansible data]# systemctl start mariadb
Step 3: install PHP and PHP MySQL modules
[root@ansible ~]# yum -y install php php-mysql
Step 4: provide php test page
[root@ansible ~]# vim /var/www/html/index.php

[root@ansible ~]# cat /var/www/html/index.php


[ root@ansible ~]#Systemctl reload httpd # starts the httpd service

httpd test:

Make sure that the above test page has appeared and that MySQL has been integrated before proceeding to the next step

Fifth; Define group name

[ root@ansible ~]#VIM / etc / ansible / hosts # also uses the previously defined ones, which need not be modified here


Then, the public key information is copied to the controlled node, and ansible is connected with the two nodes through ssh. The following three commands have been done before and do not need to be executed.

[root@ansible ~]# ssh-keygen
[root@ansible ~]# ssh-copy-id root@
[root@ansible ~]# ssh-copy-id root@
Sixth: create a LAMP build task using playbook

1. Create related files

[root@ansible ~]# mkdir -pv /etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handlers}

We copy the httpd and MySQL configuration files of the LAMP environment successfully built above to the corresponding directory

[root@ansible ~]# cd /etc/ansible/ [root@ansible ansible]# cp /etc/httpd/conf/httpd.conf lamp/roles/httpd/files/

[root@ansible ansible]# cp /etc/my.cnf lamp/roles/mysql/files/
[root@ansible ansible]# Write playbooks for the prepare role

[root@ansible ansible]# vim lamp/roles/prepare/tasks/main.yml

[root@ansible ansible]# cat lamp/roles/prepare/tasks/main.yml
- name: delete yum config
  shell: rm -rf /etc/yum.repos.d/*  #Delete the existing yum profile
- name: provide yumrepo file
  shell: wget -O /etc/yum.repos.d/CentOS-Base.repo # download the new yum profile
- name: clean the yum repo
  shell: yum clean all    #Clear the original yum cache information
- name: clean the iptables
  shell: iptables -F    #Clear the original firewall rules, and then you may not be able to access the network
[root@ansible ansible]#

2. Task of building httpd

[root@ansible ansible]# cd /etc/ansible/lamp/roles/

[root@ansible roles]# mv /var/www/html/index.php httpd/files/

[root@ansible roles]# vim httpd/tasks/main.yml

[root@ansible roles]# cat httpd/tasks/main.yml

[root@ansible roles]# cat httpd/tasks/main.yml
- name: web server install
 yum: name=httpd state=present    #Install httpd service
- name: provide test page
 copy: src=index.php dest=/var/www/html    #Provide test page
- name: delete apache config

 shell: rm -rf  /etc/httpd/conf/httpd.conf  #Delete the original apache configuration file. If it is not deleted, the following copy task will not be executed, because the copy command will not be executed when the source file httpd.conf is the same as the target file. If the copy command is not executed, notify will not call handler.
- name: provide configuration file
 copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf    #Provides the configuration file for httpd
 notify: restart httpd   #After the previous copy is copied successfully, notify the handlers named restart httpd to run

3. Building handlers for httpd

[root@ansible roles]# vim httpd/handlers/main.yml

[root@ansible roles]# cat httpd/handlers/main.yml
- name: restart httpd
service: name=httpd enabled=yes state=restarted
[root@ansible roles]#

4. Deploy our MariaDB database

To create a MySQL service, you need to install the MySQL service, change the owner information, and start mysql

[root@ansible roles]# cd /etc/ansible/lamp/roles/
[root@ansible roles]# vim mysql/tasks/main.yml
[root@ansible roles]# cat mysql/tasks/main.yml
-name: install the mysql
  yum: name=mariadb-server state=present    #Install mysql service
- name: mkdir date directory
  shell: mkdir -p /mydata/data    #Create mount point directory
- name: provide configration file
  copy: src=my.cnf dest=/etc/my.cnf    #Provide mysql configuration file
- name: chage the owner
  shell: chown -R mysql:mysql /mydata/    #Change owner and group
- name: start mariadb
  service: name=mariadb enabled=yes state=started    #service mysql start 

5. The task of building PHP

[root@ansible roles]# vim php/tasks/main.yml
- name: install php
yum: name=php state=present    #Install php
- name: install php-mysql
yum: name=php-mysql state=present    #Install the plug-in for php and mysql interaction

6. Define the entire task

[root@ansible roles]# cd /etc/ansible/lamp/roles/
[root@ansible roles]# vim site.yml
[root@ansible roles]# cat site.yml
- name: LAMP build
  remote_user: root
  hosts: web-servers
    - prepare
    - mysql
    - php
    - httpd

Note: in all yml configuration files, spaces must be strictly correct

Start deployment:

[root@ansible roles]# ansible-playbook -i /etc/ansible/hosts /etc/ansible/lamp/roles/site.yml

Then, access the two node hosts in the browser, and you can directly access them successfully

Conclusion: to do this laboratory, we need to prepare a clean environment, and selinux and firewall should be closed

Tags: Linux Operation & Maintenance server

Posted on Thu, 18 Nov 2021 18:12:46 -0500 by elenev