playbooks 是一种简单的配置管理系统与多机器部署系统的基础。与现有的其他系统有不同之处,且非常适合于复杂应用的批量部署
Playbooks 的格式是YAML,语法做到最小化

这里讲述的是使用ansible-playbooks的roles安装LNMP应用

结构目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[root@Ansible ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
├── roles
│   ├── source_nginx
│   │   ├── files
│   │   │   ├── echo-nginx-module-0.59.tar.gz
│   │   │   ├── gperftools-2.0.zip
│   │   │   ├── nginx-1.11.7.tar.gz
│   │   │   ├── nginx_tcp_proxy_module_nginx1.11.7.zip
│   │   │   ├── nginx-upstream-jvm-route-nginx1.11.7.zip
│   │   │   ├── ngx_cache_purge-2.3.tar.gz
│   │   │   ├── openssl-1.1.0e.tar.gz
│   │   │   ├── pcre-8.40.zip
│   │   │   ├── yum.repo
│   │   │   └── zlib-1.2.11.tar.gz
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   │   ├── comm.conf.j2
│   │   │   ├── etc_init.d_nginx.txt
│   │   │   ├── fastcgi.conf.j2
│   │   │   ├── nginx.conf.j2
│   │   │   ├── proxy.conf.j2
│   │   │   └── static.conf.j2
│   │   └── vars
│   │   └── main.yml
│   ├── yum_mysql
│   │   ├── files
│   │   │   └── yum.repo
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   │   └── my.cnf.j2
│   │   └── vars
│   │   └── main.yml
│   └── yum_php
│   ├── files
│   │   └── yum.repo
│   ├── handlers
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   │   └── www.conf.j2
│   └── vars
│   └── main.yml
└── site.yml
19 directories, 32 files
[root@Ansible ~]#

Ansible之安装LNMP

参考:
Ansible-Playbooks之安装Nginx
Ansible-Playbooks之安装PHP
Ansible-Playbooks之安装Mysql
环境:
   ansible 2.2.1.0
   Centos 6.6
   PHP 7.0.16 (fpm-fcgi)
   Nginx 1.11.7
   Mysql 5.5.54

1
2
3
4
5
6
7
8
9
#入口文件
[root@Ansible ansible]# cat site.yml
- hosts: test_hosts
user: root
roles:
- yum_mysql
- yum_php
- source_nginx
[root@Ansible ansible]#

注:之前可能是在最后安装的mysql,把之前在PHP上安装的一个插件(php70w-mysql)弄丢了,所以就把mysql放最前面安装了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
[root@Ansible ~]# ansible-playbook /etc/ansible/site.yml
PLAY [test_hosts] **************************************************************
TASK [setup] *******************************************************************
ok: [192.168.31.110]
TASK [yum_mysql : Install libselinux-python package] ***************************
changed: [192.168.31.110]
TASK [yum_mysql : Check whether yum.repo file exist] ***************************
fatal: [192.168.31.110]: FAILED! => {"changed": true, "cmd": "ls /etc/yum.repos.d/yum.repo", "delta": "0:00:00.004362", "end": "2017-03-20 12:01:43.959441", "failed": true, "rc": 2, "start": "2017-03-20 12:01:43.955079", "stderr": "ls: cannot access /etc/yum.repos.d/yum.repo: No such file or directory", "stdout": "", "stdout_lines": [], "warnings": []}
...ignoring
TASK [yum_mysql : Copy yum.repo file] ******************************************
changed: [192.168.31.110]
TASK [yum_mysql : Rebuild the yum cache] ***************************************
changed: [192.168.31.110]
[WARNING]: Consider using yum module rather than running yum
TASK [yum_mysql : Install Mysql Packages] **************************************
changed: [192.168.31.110] => (item={u'state': u'absent', u'name': u'mysql*'})
changed: [192.168.31.110] => (item={u'name': u'mysql.x86_64'})
changed: [192.168.31.110] => (item={u'name': u'mysql-devel.x86_64'})
changed: [192.168.31.110] => (item={u'name': u'mysql-server.x86_64'})
TASK [yum_mysql : Configure Mysql file] ****************************************
changed: [192.168.31.110]
TASK [yum_php : Install libselinux-python package] *****************************
ok: [192.168.31.110]
TASK [yum_php : Check whether yum.repo file exist] *****************************
changed: [192.168.31.110]
TASK [yum_php : Copy yum.repo file] ********************************************
skipping: [192.168.31.110]
TASK [yum_php : Rebuild the yum cache] *****************************************
skipping: [192.168.31.110]
TASK [yum_php : Check whether nginx users exist] *******************************
fatal: [192.168.31.110]: FAILED! => {"changed": true, "cmd": "id nginx", "delta": "0:00:00.005955", "end": "2017-03-20 12:14:49.015479", "failed": true, "rc": 1, "start": "2017-03-20 12:14:49.009524", "stderr": "id: nginx: No such user", "stdout": "", "stdout_lines": [], "warnings": []}
...ignoring
TASK [yum_php : The nginx user does not exist, so we need to create it.[The nginx user's password is 123456]] ***
changed: [192.168.31.110]
TASK [yum_php : Install PHP packages] ******************************************
ok: [192.168.31.110] => (item={u'state': u'absent', u'name': u'php*'})
changed: [192.168.31.110] => (item={u'name': u'php70w'})
changed: [192.168.31.110] => (item={u'name': u'php70w-gd'})
ok: [192.168.31.110] => (item={u'name': u'libjpeg*'})
changed: [192.168.31.110] => (item={u'name': u'php70w-imap'})
changed: [192.168.31.110] => (item={u'name': u'php70w-ldap'})
changed: [192.168.31.110] => (item={u'name': u'php70w-odbc'})
changed: [192.168.31.110] => (item={u'name': u'php70w-pear'})
ok: [192.168.31.110] => (item={u'name': u'php70w-xml'})
changed: [192.168.31.110] => (item={u'name': u'php70w-xmlrpc'})
changed: [192.168.31.110] => (item={u'name': u'php70w-mbstring'})
changed: [192.168.31.110] => (item={u'name': u'php70w-mcrypt'})
changed: [192.168.31.110] => (item={u'name': u'php70w-bcmath'})
ok: [192.168.31.110] => (item={u'name': u'libmcrypt'})
changed: [192.168.31.110] => (item={u'name': u'libmcrypt-devel'})
changed: [192.168.31.110] => (item={u'name': u'php70w-fpm'})
ok: [192.168.31.110] => (item={u'name': u'php70w-cli'})
ok: [192.168.31.110] => (item={u'name': u'php70w-pdo'})
changed: [192.168.31.110] => (item={u'name': u'php70w-tidy'})
changed: [192.168.31.110] => (item={u'name': u'php70w-mysql'})
TASK [yum_php : Configure PHP] *************************************************
changed: [192.168.31.110]
TASK [yum_php : Modify file permissions] ***************************************
changed: [192.168.31.110] => (item={u'path': u'/etc/httpd/conf.d/php.conf', u'state': u'file'})
changed: [192.168.31.110] => (item={u'path': u'/var/lib/php/session'})
changed: [192.168.31.110] => (item={u'path': u'/var/lib/php/wsdlcache'})
TASK [source_nginx : disabled iptables] ****************************************
changed: [192.168.31.110]
TASK [source_nginx : Set the selinux value to Permissive] **********************
changed: [192.168.31.110]
TASK [source_nginx : Create download directory] ********************************
changed: [192.168.31.110]
TASK [source_nginx : Install the some tools] ***********************************
changed: [192.168.31.110] => (item={u'name': u'unzip'})
changed: [192.168.31.110] => (item={u'name': u'zip'})
ok: [192.168.31.110] => (item={u'name': u'tar'})
ok: [192.168.31.110] => (item={u'name': u'libselinux-python'})
TASK [source_nginx : download some source packages] ****************************
changed: [192.168.31.110] => (item={u'url': u'https://ftp.pcre.org/pub/pcre/pcre-8.40.zip', u'validate_certs': u'no'})
changed: [192.168.31.110] => (item={u'url': u'http://nginx.org/download/nginx-1.11.7.tar.gz'})
TASK [source_nginx : unarchive some source packages] ***************************
changed: [192.168.31.110] => (item={u'src': u'/tmp/download/nginx-1.11.7.tar.gz', u'remote_src': u'yes'})
changed: [192.168.31.110] => (item={u'src': u'/tmp/download/pcre-8.40.zip', u'remote_src': u'yes'})
changed: [192.168.31.110] => (item={u'src': u'files/zlib-1.2.11.tar.gz'})
changed: [192.168.31.110] => (item={u'src': u'files/openssl-1.1.0e.tar.gz'})
changed: [192.168.31.110] => (item={u'src': u'files/gperftools-2.0.zip'})
changed: [192.168.31.110] => (item={u'src': u'files/echo-nginx-module-0.59.tar.gz'})
changed: [192.168.31.110] => (item={u'src': u'files/nginx-upstream-jvm-route-nginx1.11.7.zip'})
changed: [192.168.31.110] => (item={u'src': u'files/ngx_cache_purge-2.3.tar.gz'})
TASK [source_nginx : Check whether nginx users exist] **************************
changed: [192.168.31.110]
TASK [source_nginx : The nginx user does not exist, so we need to create it.[The nginx user's password is 123456]] ***
skipping: [192.168.31.110]
TASK [source_nginx : Install the pcre-8.40 source package] *********************
changed: [192.168.31.110]
TASK [source_nginx : Install the zlib-1.2.11 source package] *******************
changed: [192.168.31.110]
TASK [source_nginx : Install the openssl-1.1.0e source package] ****************
changed: [192.168.31.110]
TASK [source_nginx : Install the some devel packages] **************************
ok: [192.168.31.110] => (item={u'rpm': u'openssl-devel'})
changed: [192.168.31.110] => (item={u'rpm': u'pcre-devel'})
ok: [192.168.31.110] => (item={u'rpm': u'zlib-devel'})
ok: [192.168.31.110] => (item={u'rpm': u'gcc'})
ok: [192.168.31.110] => (item={u'rpm': u'gcc-c++'})
ok: [192.168.31.110] => (item={u'rpm': u'make'})
ok: [192.168.31.110] => (item={u'rpm': u'perl'})
changed: [192.168.31.110] => (item={u'rpm': u'perl-devel'})
changed: [192.168.31.110] => (item={u'rpm': u'perl-ExtUtils-Embed'})
TASK [source_nginx : Install the gperftools-2.0 source package] ****************
changed: [192.168.31.110]
TASK [source_nginx : Install patch package] ************************************
changed: [192.168.31.110]
TASK [source_nginx : Play nginx-upstream-jvm-route patche] *********************
changed: [192.168.31.110]
TASK [source_nginx : Install the Nginx package] ********************************
changed: [192.168.31.110]
TASK [source_nginx : Create some directory] ************************************
changed: [192.168.31.110] => (item={u'path': u'/tmp/tcmalloc', u'mode': u'0777'})
changed: [192.168.31.110] => (item={u'path': u'/var/proxy_cache/temp'})
changed: [192.168.31.110] => (item={u'path': u'/var/proxy_cache/cache'})
changed: [192.168.31.110] => (item={u'path': u'/var/log/nginx'})
changed: [192.168.31.110] => (item={u'path': u'/usr/local/nginx-1.11.7/conf/vhosts'})
TASK [source_nginx : set nginx parameter] **************************************
ok: [192.168.31.110]
TASK [source_nginx : Copy same files] ******************************************
changed: [192.168.31.110] => (item={u'dest': u'/etc/init.d/nginx', u'src': u'templates/etc_init.d_nginx.txt', u'mode': u'0755'})
changed: [192.168.31.110] => (item={u'dest': u'/usr/local/nginx-1.11.7/conf/nginx.conf', u'src': u'templates/nginx.conf.j2'})
changed: [192.168.31.110] => (item={u'dest': u'/usr/local/nginx-1.11.7/conf/vhosts/proxy.conf', u'src': u'templates/proxy.conf.j2'})
changed: [192.168.31.110] => (item={u'dest': u'/usr/local/nginx-1.11.7/conf/vhosts/comm.conf', u'src': u'templates/comm.conf.j2'})
changed: [192.168.31.110] => (item={u'dest': u'/usr/local/nginx-1.11.7/conf/vhosts/static.conf', u'src': u'templates/static.conf.j2'})
changed: [192.168.31.110] => (item={u'dest': u'/usr/local/nginx-1.11.7/conf/vhosts/fastcgi.conf', u'src': u'templates/fastcgi.conf.j2'})
RUNNING HANDLER [yum_mysql : restart mysql] ************************************
changed: [192.168.31.110]
RUNNING HANDLER [yum_mysql : Set the mysql database password] ******************
changed: [192.168.31.110]
RUNNING HANDLER [yum_php : restart php-fpm] ************************************
changed: [192.168.31.110]
RUNNING HANDLER [source_nginx : restart nginx] *********************************
changed: [192.168.31.110]
PLAY RECAP *********************************************************************
192.168.31.110 : ok=36 changed=33 unreachable=0 failed=0
[root@Ansible ~]#

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@Ansible ~]# ansible test_hosts -m shell -a '/etc/init.d/nginx status'
192.168.31.110 | SUCCESS | rc=0 >>
nginx (pid 78698) is running...
[root@Ansible ~]# ansible test_hosts -m shell -a '/etc/init.d/php-fpm status'
192.168.31.110 | SUCCESS | rc=0 >>
php-fpm (pid 78539) is running...
[root@Ansible ~]# ansible test_hosts -m shell -a '/etc/init.d/mysqld status'
192.168.31.110 | SUCCESS | rc=0 >>
mysqld (pid 78413) is running...
[root@Ansible ~]#

image

附件:ansible_install_LNMP.tar.gz


本文出自”Jack Wang Blog”:http://www.yfshare.vip/2017/03/20/Ansible-Playbooks之安装LNMP/