Ansible project combat LNMP

Description:  The nginx and php source package into the character file corresponding to the files in the directory, by vars / main.yml control version and the installation path. as follows:

[@ ansible the root Roles] # CAT Nginx / VARS / main.yml 
DOWNLOAD_DIR: " / usr / local / the src / "   # copy the package to the target host storage path 
INSTALL_DIR: " / usr / local / "        # installation path 
NGINX_VERSION: " 1.12.2 "           # version of the package 
the uSER: " nginx "                     user # run 
the gROUP: " nginx "                    # operation group

Written role

Here are unified role on the /etc/ansible/rolesnext

Install the dependencies need to use compile-time

[root@ansible ~]# cd /etc/ansible/roles/
[@ ansible the root Roles] # CAT init_pkg.yml 
# source compiler installed php, nginx used when required dependencies
 --- 
- the hosts: All 
  REMOTE_USER: the root 

  Tasks:
     - name: the Install the Package
       yum : name = {{Item State =}} Installed 
      with_items:
         - GCC -C ++ 
        - the glibc
         - glibc- devel
         - glib2
         - glib2- devel
         - PCRE
         - PCRE- devel
         - zlib
         - zlib- devel
         - OpenSSL
        - openssl-devel
        - libpng
        - libpng-devel
        - freetype
        - freetype-devel
        - libxml2
        - libxml2-devel
        - bzip2
        - bzip2-devel
        - ncurses
        - curl
        - gdbm-devel
        - libXpm-devel
        - libX11-devel
        - gd-devel
        - gmp-devel
        - readline-devel
        - libxslt-devel
        - expat-devel
        - xmlrpc-c
        - libcurl-devel

nginx roles

1) create the appropriate folder

[root@ansible roles]# mkdir -p nginx/{files,handlers,tasks,templates,vars}

2) preparation of final results

[root @ ansible Roles] # tree INX 
INX 
├── files 
│ ├── INX - 1.12 . 2 . tar .gz 
│ └── INX - 1.16 . 0 . tar .gz 
├── handlers 
│ └── main.yml 
├── Tasks 
│ ├── config.yml 
│ ├── copypkg.yml 
│ ├── group.yml 
│ ├── install .yml 
│ ├── main.yml 
│ ├── service.yml 
│ └── user.yml 
├── templates 
│ ├── nginx.conf.j2 
│ ├── nginx_init.j2 
│ └── nginx.service.j2 
└── VARs 
    └── slim .yml 

5 directories, 14 files

php roles

1) create the appropriate folder

[root@ansible roles]# mkdir -p php/{files,handlers,tasks,templates,vars}

2) preparation of final results

[root @ ansible Roles] # tree php 
php 
├── files 
│ └── php - 5.6 . 40 . tar .gz 
├── handlers 
│ └── main.yml 
├── Tasks 
│ ├── config.yml 
│ ├── copypkg.yml 
│ ├── group.yml 
│ ├── install .yml 
│ ├── main.yml 
│ ├── service.yml 
│ └── user.yml 
├── templates 
│ ├── php - fpm.conf.j2 
│ ├── php - fpm.init.j2 
│ ├── php - FPM .service.j2 
│ └── php.ini.j2 
└── VARs 
    └── main.yml 

5 directories, 14 files

mysql roles

1) create the appropriate folder

[root@ansible roles]# mkdir -p mysql/{files,handlers,tasks,templates,vars}

2) preparation of final results

[root@ansible roles]# tree mysql
mysql
├── files
├── handlers
│   └── main.yml
├── tasks
│   ├── config.yml
│   ├── install.yml
│   ├── main.yml
│   └── service.yml
├── templates
│   ├── my.cnf6.j2
│   └── my.cnf7.j2
└── vars

5 directories, 7 files

The role of the documents prepared by the Executive playbook

[root @ ansible the Roles] # CAT nginx_roles.yml 
# source code to compile and install nginx
 --- 
- hosts: All 
  REMOTE_USER: root 
  the Roles:
     - Role: nginx 


[root @ ansible the Roles] # CAT php_roles.yml 
# source code to compile and install nginx
 - - 
- hosts: All 
  REMOTE_USER: root 
  the Roles:
     - Role: PHP 


[root @ ansible the Roles] # CAT mysql_roles.yml 
# yum install MySQL
 --- 
- hosts: All 
  REMOTE_USER: root 
  the Roles:
     - Role: MySQL 


[root @ ansible the Roles ] # CAT  lnmp.yml 
# configure lnmp, create a virtual host
---
- hosts: all
  remote_user: root
  roles:
    - role: nginx
    - role: php 
    - role: mysql
  
  vars:
    PORT: 8081
    WEBDIR: "/opt/www"
    CONFIGDIR: "/usr/local/nginx/conf/conf.d"

  tasks:
    - name: create vhost dir
      file: name={{ WEBDIR }} state=directory owner=www group=www mode=755

    - name: create vhost conf
      template: src=vhost.conf.j2 dest={{ CONFIGDIR }}/vhost.conf
      Notify: the Restart the Nginx 

    -name: Create the index.php 
      the shell: " echo '<PHP the phpinfo ();??>'> WEBDIR {{}} / the index.php " 
    
  handlers:
     - name: the Nginx the Restart 
      -Service: name = State = Nginx RESTARTED 


# hostslist document preparation, so easy to perform, you can specify on a machine running while performing PlayBook 
[root @ ansible the Roles] # CAT hostlist 
 192.168 . 1.31 
192.168 . 1.32 
192.168 . 1.33 
192.168 . 1.36 


# View all files 
[root @ ansible roles] # ll 
total volume of 28 
-rw-R & lt -. r-- . 1 the root the root  53 6月    4  22 : 37 hostlist
 -rw-r - r--. 1 root root 824 6月    5  10 : 53 init_pkg.yml
 -rw-r - r--. 1 root root 646 6月    5  12 : 05 lnmp.yml 
drwxr -xr-x. 7 root root   77 6月    5  10 : 44 mysql
 -rw-r - r--. 1 root root   81 6月    5  10 : 06 mysql_roles.yml
drwxr-xr-x. 7 root root  77 6月    4  15 : 37 INX
 -rw-r - r--. 1 root root   89 6月    4  17 : 10 nginx_roles.yml 
drwxr -xr-x. 7 root root   77 6月    4  17 : 18 php
 -rw-r - r--. 1 root root   87 6月    4  17 : 37 php_roles.yml
 -rw-r - r--. 1 root root 811 6月    5  11 : 53 vhost.conf.j2

View all files

[root @ ansible Roles] # tree 
. 
├── hostlist 
├── init_pkg.yml 
├── lnmp.yml 
├── mysql 
│ ├── files 
│ ├── handlers 
│ │ └── main.yml 
│ ├── Tasks 
│ │ ├── config. YML 
│ │ ├── install .yml 
│ │ ├── main.yml 
│ │ └── service.yml 
│ ├── templates 
│ │ ├── my.cnf6.j2 
│ │ └── my.cnf7.j2 
│ └── VARs 
├── mysql_roles.yml 
├── INX 
│ ├── files 
│ │ ├── INX - 1.12 . 2 . tar .gz 
│ │ └── INX - 1.16. 0 . tar .gz 
│ ├── handlers 
│ │ └── main.yml 
│ ├── Tasks 
│ │ ├── config.yml 
│ │ ├── copypkg.yml 
│ │ ├── group.yml 
│ │ ├── install .yml 
│ │ ├── main.yml 
│ │ ├── service.yml 
│ │ └── user.yml 
│ ├── templates 
│ │ ├── nginx.conf.j2 
│ │ ├── nginx_init.j2 
│ │ └── nginx.service.j2 
│ └── VARs 
│ └── main.yml 
├── nginx_roles.yml 
├── php 
│ ├── files 
│ │ └── php - 5.6 . 40 . tar.gz 
│ ├── handlers 
│ │ └── main.yml 
│ ├── Tasks 
│ │ ├── config.yml 
│ │ ├── copypkg.yml 
│ │ ├── group.yml 
│ │ ├── install .yml 
│ │ ├── main.yml 
│ │ ├── service.yml 
│ │ └── user.yml 
│ ├── templates 
│ │ ├── php - fpm.conf.j2 
│ │ ├── php - fpm.init.j2 
│ │ ├── php - fpm.service.j2 
│ │ └── php.ini.j2 
│ └── VARs 
│ └── main.yml 
├── php_roles.yml 
└── vhost. conf.j2 

18 directories, 42 files

Execution Description

1) a single machine installed nginx

[root@ansible roles]# ansible-playbook -i hostlist nginx_roles.yml --limit 192.168.1.31

2) a single machine installed php

[root@ansible roles]# ansible-playbook -i hostlist php_roles.yml --limit 192.168.1.31

3) a separate mounting machines mysql

[root@ansible roles]# ansible-playbook -i hostlist mysql_roles.yml --limit 192.168.1.31

4) a single machine deployment lnmp

[root@ansible roles]# ansible-playbook -i hostlist lnmp.yml --limit 192.168.1.31

5) All machines deployed php

[root@ansible roles]# ansible-playbook php_roles.yml

6) All machines deployed nginx

[root@ansible roles]# ansible-playbook nginx_roles.yml

7) All machines deployed mysql

[root@ansible roles]# ansible-playbook mysql_roles.yml

8) All machines deployed lnmp

[root@ansible roles]# ansible-playbook lnmp.yml

 

Guess you like

Origin www.linuxidc.com/Linux/2019-06/159055.htm