How to use the ansible in the playbook deployment lamp architecture

Real - Volume Deployment Playbook using multiple LAMP environment

  • Use playbooks playbook step is a different way of using the ansible command line mode, more powerful and more flexible.

  • Ideas: we built lanp architecture, takes about:

  • yum install Service

  • service start

  • copy the site copy past

In playbooks defined tasks:

  • name: task description # description of job module_name: module name module_args # need to use: ansible-playbook execute command format:

[root@ansible ~]# ansible-playbook site.yml

  • Examples playbook is a list of one or more "play" thereof. play's main function is to host pre-classified as a group dressed as pre-defined by ansible role in the task. On github provides a number of examples for your reference:

https://github.com/ansible/ansible-examples 4.2

LAMP environment Playbook deploying multiple frequently used folders role:

  • files: Storage need to be synchronized to the remote file server source code and configuration files;
  • handlers: When the operation of the service configuration file changes needed, such as: restart the service, reload the configuration file, handlers - handler meta: role definition, can be left blank;
  • tasks: The task needs to be performed;
    - templates: template file for performing lamp installation, usually the script;
  • vars: The installation of variable definitions 4.3

Use Playbook Batch deploy multiple steps LAMP environment

We can install LAMP server environment on ansible, then, and then copy the configuration file to the remote host by ansible

  • Step 1: Install the software httpd

[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 data stored

[root@ansible ~]# chown -R mysql:mysql /mysqldata/ # Authorization

[root@ansible ~]# vim /etc/my.cnf # Change the data storage directory change:

2 datadir=/var/lib/mysql

Read:2 datadir=/mydata/data/

[root@ansible data]# systemctl start mariadb

  • Step 3: Install Module PHP and php-mysql

[root@ansible ~]# yum -y install php php-mysql

Step four: provide php test page

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

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

<?php  
    phpinfo();
?>

[root@ansible ~]# systemctl reload httpd # Service httpd start

httpd test: http: //192.168.43.121

Make sure you have appeared above a test page, and to see that MySQL has been integrated we come to the next step

Fifth; defined group name

[Root @ ansible ~] # vim / etc / ansible / hosts # previously defined further use, without modification where

[webservers]
192.168.43.121
192.168.43.167
192.168.43.168

Then, the public key information to the copy control node connected through ssh and ansible between two nodes. We've already done the following three commands, not executed.

[root@ansible ~]# ssh-keygen
[root@ansible ~]# ssh-copy-id [email protected]
[root@ansible ~]# ssh-copy-id [email protected]

6: Using playbook to create the task of building a LAMP

1, create documents

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

We will build on top of the successful LAMP environment httpd and MySQL configuration files are copied 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 prepare (preparation) role playbooks

[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/*  #删除原有的yum配置文件
- name: provide yumrepo file
  shell: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo   #下载新的yum配置文件
- name: clean the yum repo
  shell: yum clean all    #清除原有的yum缓存信息
- name: clean the iptables
  shell: iptables -F    #清除原有防火墙规则,不然后可能上不了网
[root@ansible ansible]#
  

2, the 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    #安装httpd服务
- name: provide test page
 copy: src=index.php dest=/var/www/html    #提供测试页
- name: delete apache config
 shell: rm -rf  /etc/httpd/conf/httpd.conf  #删除原有的apache配置文件,如果不删除,下面的copy任务是不会执行的,因为当源文件httpd.conf和目标文件一样时,copy命令是不执行的。如果copy命令不执行,那么notify将不调用handler。
- name: provide configuration file
 copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf    #提供httpd的配置文件
 notify: restart httpd   #当前面的copy复制成功后,通过notify通知名字为restart httpd的handlers运行

3, the construction of handlers 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, the deployment of our MariaDB database
to create a MySQL service tasks need to install the MySQL service, change the owner information, 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    #安装mysql服务
- name: mkdir date directory
  shell: mkdir -p /mydata/data    #创建挂载点目录
- name: provide configration file
  copy: src=my.cnf dest=/etc/my.cnf    #提供mysql的配置文件
- name: chage the owner
  shell: chown -R mysql:mysql /mydata/    #更改属主和属组
- name: start mariadb
  service: name=mariadb enabled=yes state=started    #启动mysql服务

5, the task of building PHP

[root@xuegod63 roles]# vim php/tasks/main.yml
- name: install php
yum: name=php state=present    #安装php
- name: install php-mysql
yum: name=php-mysql state=present    #安装php与mysql交互的插件

6, defines the entire mission

[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
  roles:
    - prepare
    - mysql
    - php
    - httpd
[root@ansible roles]#

Note: All yml configuration file, the space must be strictly

Began to deploy:

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

Then, these two nodes host access in the browser, direct access to success.

Summary: do this lab, you need a clean environment, selinux, the firewall must be shut down

Published 60 original articles · won praise 3 · Views 2057

Guess you like

Origin blog.csdn.net/weixin_42313749/article/details/104584619