; 实战:ansible自动化部署nginx+keepalived+mysql负载均衡集群 | Linux运维部落

实战:ansible自动化部署nginx+keepalived+mysql负载均衡集群

一、目的

使用ansible自动化部署nginx+keepalived+mysql负载均衡集群。

二、拓扑规划

ansibletest1.png

三、详细步骤

  • 1、环境的搭建

        (1)安装ansible,同时配置私钥免密码进行通信


1
[root<span>@localhost ~]# ssh-keygen &nbsp;-t rsa #-t表示使用的加密类型,其中rsa1表示version1版本,rsa、dsa、ecdsa的加密对于的是version2版本</span><br />Generating public/private rsa key pair.<br /><span>#这里询问你要把生成的密钥文件保存在哪里,默认是在家目录下的.ssh文件夹中,回车保存默认目录</span><br />Enter file in which to save the key (/root/.ssh/id_rsa): <br />Created directory &#039;/root/.ssh&#039;.<br /><span>#这里是对密钥文件加密,不输入则表示不加密</span><br />Enter passphrase (empty for no passphrase): <br />Your identification has been saved in /root/.ssh/id_rsa.<br />Your public key has been saved in /root/.ssh/id_rsa.pub.<br />The key fingerprint is:<br />04:9f:cb:9c:9d:1e:47:d7:e1:d4:c1:87:71:c3:a4:22 root<span>@localhost.localdomain</span><br />The key&#039;s randomart image is:<br />+--[ RSA 2048]----+<br />|<span> &nbsp; &nbsp; &nbsp;. &nbsp; &nbsp; &nbsp; =O+</span>|<br />|<span> &nbsp; &nbsp; &nbsp; o . &nbsp; &nbsp;===</span>|<br />|<span> &nbsp; &nbsp; &nbsp; &nbsp;+E .....o</span>|<br />|<span> &nbsp; &nbsp; &nbsp; + +.o.. &nbsp; </span>|<br />|<span> &nbsp; &nbsp; &nbsp; &nbsp;S + . &nbsp; &nbsp;</span>|<br />|<span> &nbsp; &nbsp; &nbsp; &nbsp; . o &nbsp; &nbsp; </span>|<br />|<span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;. &nbsp; &nbsp; &nbsp;</span>|<br />|<span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>|<br />|<span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>|<br />+-----------------+<br />

        (2)查看已经成功生成了一对密钥


1
[root<span>@localhost</span> ~]# ls /root/.ssh<br />id_rsa &nbsp;id_rsa.pub#其中id_rsa为私钥,id_rsa.pub为公钥<br />

        (3)在生成完密钥对之后将公钥上传给服务器对应用户的家目录


1
[root@localhost ~]# ssh-copy-id -i .ssh/id_rsa.pub root@<span>10.1</span><span>.252</span><span>.215</span><br />[root@localhost ~]# ssh-copy-id -i .ssh/id_rsa.pub root@<span>10.1</span><span>.252</span><span>.235</span><br />[root@localhost ~]# ssh-copy-id -i .ssh/id_rsa.pub <span>10.1</span><span>.253</span><span>.107</span><br />[root@localhost ~]# ssh-copy-id -i .ssh/id_rsa.pub root@<span>10.1</span><span>.249</span><span>.75</span><br />[root@localhost ~]# ssh-copy-id -i .ssh/id_rsa.pub root@<span>10.1</span><span>.249</span><span>.75</span><br />

·

        (4)编辑ansible的hosts文件,定义后所有的主机


1
[<span>19</span>:<span>05</span> root@centos6.<span>8</span>/etc/ansible]<span># cat hosts </span><br /><span># This is the default ansible &#039;hosts&#039; file.</span><br /><span>#</span><br /><span># It should live in /etc/ansible/hosts</span><br /><span>#</span><br /><span># &nbsp; - Comments begin with the &#039;#&#039; character</span><br /><span># &nbsp; - Blank lines are ignored</span><br /><span># &nbsp; - Groups of hosts are delimited by [header] elements</span><br /><span># &nbsp; - You can enter hostnames or ip addresses</span><br /><span># &nbsp; - A hostname/ip can be a member of multiple groups</span><br /><br /><span># Ex 1: Ungrouped hosts, specify before any group headers.</span><br />[web]<br /><span>10.1</span>.<span>252.215</span><br /><span>10.1</span>.<span>252.235</span><br />[nginx]<br /><span>10.1</span>.<span>253.107</span> <span>state</span>=MASTER priority=<span>100</span><br /><span>10.1</span>.<span>249.75</span> &nbsp;<span>state</span>=BACKUP priority=<span>90</span><br />[mysql]<br /><span>10.1</span>.<span>252.36</span><br />

·

        OK,环境已经搭配好,所有主机同步下时间:


1
<span>[19:34 root@centos6.8/etc/ansible]</span># ansible all -a &#039;ntpdate <span>10.1.0.1</span>&#039;<br /><span>10.1.252.215</span> | success | rc=<span>0</span> &gt;&gt;<br /> <span>3</span> Nov <span>19</span>:<span>34</span>:<span>30</span> ntpdate<span>[38293]</span>: adjust time server <span>10.1.0.1</span> offset -<span>0</span>.<span>003936</span> sec<br /><br /><span>10.1.252.36</span> | success | rc=<span>0</span> &gt;&gt;<br /> <span>3</span> Nov <span>19</span>:<span>34</span>:<span>30</span> ntpdate<span>[3291]</span>: adjust time server <span>10.1.0.1</span> offset <span>0</span>.<span>200434</span> sec<br /><br /><span>10.1.252.235</span> | success | rc=<span>0</span> &gt;&gt;<br /> <span>3</span> Nov <span>19</span>:<span>34</span>:<span>36</span> ntpdate<span>[38723]</span>: adjust time server <span>10.1.0.1</span> offset -<span>0</span>.<span>001469</span> sec<br /><br /><span>10.1.253.107</span> | success | rc=<span>0</span> &gt;&gt;<br /> <span>3</span> Nov <span>19</span>:<span>34</span>:<span>37</span> ntpdate<span>[7161]</span>: adjust time server <span>10.1.0.1</span> offset -<span>0</span>.<span>001905</span> sec<br /><br /><span>10.1.249.75</span> | success | rc=<span>0</span> &gt;&gt;<br /> <span>3</span> Nov <span>19</span>:<span>34</span>:<span>37</span> ntpdate<span>[4951]</span>: adjust time server <span>10.1.0.1</span> offset <span>0</span>.<span>018952</span> sec<br />
  • 2、下面来进行ansible的roles和playbook的定义:
    在/etc/ansible/roles目录下创建相关的角色目录:


1
[<span>19</span>:<span>56</span> root@centos6<span>.8</span>/etc/ansible/roles]# mkdir -pv {mysql,web,nginx}/{files,tasks,templates,variables,handlers,meta,defult}<br />mkdir: created directory `mysql<span>&#039;</span><br />mkdir: created directory `mysql/files<span>&#039;</span><br />mkdir: created directory `mysql/tasks<span>&#039;</span><br />mkdir: created directory `mysql/templates<span>&#039;</span><br />mkdir: created directory `mysql/variables<span>&#039;</span><br />mkdir: created directory `mysql/handlers<span>&#039;</span><br />mkdir: created directory `mysql/meta<span>&#039;</span><br />mkdir: created directory `mysql/<span>default</span><span>&#039;</span><br />mkdir: created directory `web<span>&#039;</span><br />mkdir: created directory `web/files<span>&#039;</span><br />mkdir: created directory `web/tasks<span>&#039;</span><br />mkdir: created directory `web/templates<span>&#039;</span><br />mkdir: created directory `web/variables<span>&#039;</span><br />mkdir: created directory `web/handlers<span>&#039;</span><br />mkdir: created directory `web/meta<span>&#039;</span><br />mkdir: created directory `web/<span>default</span><span>&#039;</span><br />mkdir: created directory `nginx<span>&#039;</span><br />mkdir: created directory `nginx/files<span>&#039;</span><br />mkdir: created directory `nginx/tasks<span>&#039;</span><br />mkdir: created directory `nginx/templates<span>&#039;</span><br />mkdir: created directory `nginx/variables<span>&#039;</span><br />mkdir: created directory `nginx/handlers<span>&#039;</span><br />mkdir: created directory `nginx/meta<span>&#039;</span><br />mkdir: created directory `nginx/<span>default</span><span>&#039;</span><br />[<span>19</span>:<span>58</span> root@centos6<span>.8</span>/etc/ansible/roles]# tree<br />.<br />├── mysql<br />│ &nbsp; ├── <span>default</span><br />│ &nbsp; ├── files<br />│ &nbsp; ├── handlers<br />│ &nbsp; ├── meta<br />│ &nbsp; ├── tasks<br />│ &nbsp; ├── templates<br />│ &nbsp; └── variables<br />├── nginx<br />│ &nbsp; ├── <span>default</span><br />│ &nbsp; ├── files<br />│ &nbsp; ├── handlers<br />│ &nbsp; ├── meta<br />│ &nbsp; ├── tasks<br />│ &nbsp; ├── templates<br />│ &nbsp; └── variables<br />└── web<br /> &nbsp; &nbsp;├── <span>default</span><br /> &nbsp; &nbsp;├── files<br /> &nbsp; &nbsp;├── handlers<br /> &nbsp; &nbsp;├── meta<br /> &nbsp; &nbsp;├── tasks<br /> &nbsp; &nbsp;├── templates<br /> &nbsp; &nbsp;└── variables<br /><br /><span>24</span> directories, <span>0</span> files<br />

说明:
files/:存储由copy或script等模块调用的文件;
tasks/:此目录中至少应该有一个名为main.yml的文件,用于定义各task;其它的文件需要由main.yml进行“包含”调用;
handlers/:此目录中至少应该有一个名为main.yml的文件,用于定义各handler;其它的文件需要由main.yml进行“包含”调用;
vars/:此目录中至少应该有一个名为main.yml的文件,用于定义各variable;其它的文件需要由main.yml进行“包含”调用;
templates/:存储由template模块调用的模板文本;
meta/:此目录中至少应该有一个名为main.yml的文件,定义当前角色的特殊设定及其依赖关系;其它的文件需要由main.yml进行“包含”调用;
default/:此目录中至少应该有一个名为main.yml的文件,用于设定默认变量;

下面就是添加playbook了,首先设置web:

  • 3、设置web的playbook

    (1)Tasks:任务


1
[<span>17</span>:<span>27</span> root@centos6.<span>8</span>/etc/ansible]<span># cat roles/web/tasks/main.yml </span><br />- <span>name:</span> install web pakgs<br /> &nbsp;<span>yum:</span> name={{ item }}<br /> &nbsp;<span>with_items:</span><br /> &nbsp;- httpd<br /> &nbsp;- php<br /> &nbsp;- php-mysql<br />- <span>name:</span> config the web<br /> &nbsp;<span>copy:</span> src=httpd.conf dest=<span>/etc/httpd</span><span>/conf/httpd</span>.conf<br /> &nbsp;<span>notify:</span> reload the service<br />- <span>name:</span> install wordpress<br /> &nbsp;<span>copy:</span> src=wordpress dest=<span>/var/www</span><span>/html/wordpress</span><span>/<br />- name: restart the service<br /> &nbsp;service: name=httpd state=started</span><br />

        (2)添加触发器:handlers


1
[<span>20</span>:<span>26</span> root@centos6.<span>8</span>/etc/ansible]<span># cat roles/web/handlers/main.yml </span><br />- name: relaod the service<br /> &nbsp;service: name=httpd <span>state</span>=restarted<br />

        (3)添加需要的file:


1
[<span>18</span>:<span>45</span> root@centos6<span>.8</span>/etc/ansible]# ll roles/web/files/<br />total <span>40</span><br />-rw-r--r--. <span>1</span> root &nbsp; root &nbsp; &nbsp; &nbsp;<span>34419</span> Nov &nbsp;<span>2</span> <span>20</span>:<span>23</span> httpd.conf &nbsp;#主要是配置httpd的默认配置,要事先准备好<br />drwxr-xr-x. <span>5</span> nobody nfsnobody &nbsp;<span>4096</span> Nov &nbsp;<span>3</span> <span>14</span>:<span>00</span> wordpress &nbsp; #wordpres的安装程序,注意这里的配置文件已经更改了后面的连接数据库<br />[<span>18</span>:<span>58</span> root@centos6<span>.8</span>/etc/ansible]#vim &nbsp;roles/web/files/wordpress/wp-config.php &nbsp;#修改数据库信息,信息已经在后面的mysql剧本已经定义好了<br /><span>/** WordPress数据库的名称 */</span><br />define(<span>&#039;DB_NAME&#039;</span>, <span>&#039;wp&#039;</span>);<br /><br /><span>/** MySQL数据库用户名 */</span><br />define(<span>&#039;DB_USER&#039;</span>, <span>&#039;wpuser&#039;</span>); <br /><br /><span>/** MySQL数据库密码 */</span><br />define(<span>&#039;DB_PASSWORD&#039;</span>, <span>&#039;wppass&#039;</span>);<br /><br /><span>/** MySQL主机 */</span><br />define(<span>&#039;DB_HOST&#039;</span>, <span>&#039;10.1.252.109&#039;</span>);<br /><br /><span>/** 创建数据表时默认的文字编码 */</span><br />define(<span>&#039;DB_CHARSET&#039;</span>, <span>&#039;utf8&#039;</span>);<br />

        (4)添加主剧本:


1
[<span>20</span>:<span>28</span> root@centos6<span>.8</span>/etc/ansible]# ll web.yml <br />-rw-r--r--. <span>1</span> root root <span>51</span> Nov &nbsp;<span>2</span> <span>20</span>:<span>22</span> web.yml<br />[<span>20</span>:<span>28</span> root@centos6<span>.8</span>/etc/ansible]# pwd<br />/etc/ansible<br />[<span>20</span>:<span>28</span> root@centos6<span>.8</span>/etc/ansible]# cat web.yml <br />- hosts: web<br /> &nbsp;remote_user: root<br /> &nbsp;roles:<br /> &nbsp;- web &nbsp; &nbsp; <br />

    

        检查语法没有问题:


1
[<span>20</span>:<span>25</span> root@centos6<span>.8</span>/etc/ansible]# ansible-playbook --syntax-check web.yml <br /><br />playbook: web.yml<br /><br />
  • 4、下面来部署前端的nginx调度起和keepalived配置:

    (1)添加task任务:


1
[<span>19</span>:<span>34</span> root@centos6.<span>8</span>/etc/ansible]<span># cat roles/nginx/tasks/main.yml </span><br />- <span>name:</span> install keepalived<br /> &nbsp;<span>yum:</span> name=keepalived<br />- <span>name:</span> copy nginx<br /> &nbsp;<span>copy:</span> src=nginx-<span>1.10</span>.<span>0</span>-<span>1</span>.el7.ngx.x86_64.rpm dest=<span>/tmp/nginx</span>.rpm<br />- <span>name:</span> install nginx<br /> &nbsp;<span>yum:</span> name=<span>/tmp/nginx</span>.rpm<br />- <span>name:</span> delete the nginx pkg<br /> &nbsp;<span>shell:</span> <span>&quot;rm -f /tmp/nginx.rpm&quot;</span><br />- <span>name:</span> config nginx&amp;keepalived<br /> &nbsp;<span>template:</span> src=keepalived.j2 dest=<span>/etc/keepalived</span><span>/keepalived.conf<br /> &nbsp;notify: reload the service<br />- name: config nginx&amp;keepalived<br /> &nbsp;template: src=nginx.j2 dest=/etc</span><span>/nginx/nginx</span>.conf<br /> &nbsp;<span>notify:</span> reload the service<br />- <span>name:</span> start the service<br /> &nbsp;<span>service:</span> name={{ item }} state=started <br /> &nbsp;<span>with_items:</span><br /> &nbsp;- keepalived<br /> &nbsp;- nginx<br />

        (2)添加handlers:


1
[<span>21</span>:<span>28</span> root@centos6.<span>8</span>/etc/ansible]<span>#cat roles/nginx/handlers/mainx.yml </span><br />- <span>name:</span> reload the service<br /> &nbsp;<span>service:</span> name={{ item }} state=restarted<br /> &nbsp;<span>with_items:</span><br /> &nbsp;- nginx<br /> &nbsp;- keepalived<br />

        (3)在hosts列表中复制变量:


1
[<span>19</span>:<span>38</span> root@centos6.<span>8</span>/etc/ansible]<span># cat hosts </span><br /><span># This is the default ansible &#039;hosts&#039; file.</span><br /><span>#</span><br /><span># It should live in /etc/ansible/hosts</span><br /><span>#</span><br /><span># &nbsp; - Comments begin with the &#039;#&#039; character</span><br /><span># &nbsp; - Blank lines are ignored</span><br /><span># &nbsp; - Groups of hosts are delimited by [header] elements</span><br /><span># &nbsp; - You can enter hostnames or ip addresses</span><br /><span># &nbsp; - A hostname/ip can be a member of multiple groups</span><br /><br /><span># Ex 1: Ungrouped hosts, specify before any group headers.</span><br /><br />[web]<br /><span>10.1</span>.<span>252.215</span><br /><span>10.1</span>.<span>252.235</span><br />[nginx]<br /><span>10.1</span>.<span>253.107</span> <span>state</span>=MASTER priority=<span>100</span><br /><span>10.1</span>.<span>249.75</span> &nbsp;<span>state</span>=BACKUP priority=<span>90</span><br />[mysql]<br /><span>10.1</span>.<span>252.36</span><br />

        (4)提供files文件:nginx的安装程序


1
[<span>19</span>:<span>39</span> root@centos6<span>.8</span>/etc/ansible]# ll roles/nginx/files/<br />total <span>644</span><br />-rw-r--r--. <span>1</span> root root <span>655648</span> Apr <span>29</span> &nbsp;<span>2016</span> nginx-<span>1.10</span><span>.0</span>-<span>1.</span>el7.ngx.x86_64.rpm<br />

        (5)在template中使用了变量:

1
nginx配置:


1
[21:33 root@centos6.8/etc/ansible]# <span>cat</span> roles/nginx/templates/nginx.j2 <br /><br />user &nbsp;nginx;<br />worker_processes &nbsp;{{ ansible_processor_vcpus &nbsp;}}; #使用变量,进程数为cpu数量:<br /><br />error_log &nbsp;/<span>var</span>/<span>log</span>/nginx/<span>error</span>.<span>log</span> warn;<br />pid &nbsp; &nbsp; &nbsp; &nbsp;/<span>var</span>/<span>run</span>/nginx.pid;<br /><br /><br />events {<br /> &nbsp; &nbsp;worker_connections &nbsp;1024;<br />}<br /><br /><br />http {<br /> &nbsp; &nbsp;<span>include</span> &nbsp; &nbsp; &nbsp; /etc/nginx/mime.types;<br /> &nbsp; &nbsp;default_type &nbsp;application/octet-stream;<br /><br /> &nbsp; &nbsp;log_format &nbsp;main &nbsp;&#039;$remote_addr - $remote_user [$time_local] <span>&quot;$request&quot;</span> &#039;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#039;$status $body_bytes_sent <span>&quot;$http_referer&quot;</span> &#039;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#039;<span>&quot;$http_user_agent&quot;</span> <span>&quot;$http_x_forwarded_for&quot;</span>&#039;;<br /><br /> &nbsp; &nbsp;access_log &nbsp;/<span>var</span>/<span>log</span>/nginx/access.<span>log</span> &nbsp;main;<br /><br /> &nbsp; &nbsp;sendfile &nbsp; &nbsp; &nbsp; &nbsp;<span>on</span>;<br /> &nbsp; &nbsp;#tcp_nopush &nbsp; &nbsp; <span>on</span>;<br /><br /> &nbsp; &nbsp;keepalive_timeout &nbsp;65;<br /><br /> &nbsp; &nbsp;#gzip &nbsp;<span>on</span>;<br /><br /> &nbsp; &nbsp;<span>include</span> /etc/nginx/<span>conf</span>.<span>d</span><span>/*.conf;<br /><br /> &nbsp; &nbsp;upstream web {<br /> &nbsp; &nbsp;server 10.1.24.113;<br /> &nbsp; &nbsp;server 10.1.24.114;<br /> }<br /> &nbsp; &nbsp;location / {<br /> &nbsp; &nbsp;proxy_pass http://web; &nbsp;<br />}<br /><br />}</span><br />

1
keepalived配置:


1
[<span>21</span>:<span>33</span> root@centos6.<span>8</span>/etc/ansible]<span># cat roles/nginx/templates/keepalived.j2 </span><br />! <span>Configuration</span> <span>File</span> <span>for</span> keepalived<br /><br />global_defs {<br /> &nbsp; notification_email {<br /> &nbsp; &nbsp; sysadmin@firewall.loc<br /> &nbsp; }<br /> &nbsp; notification_email_from <span>Alexandre</span>.<span>Cassen</span>@firewall.loc<br /> &nbsp; smtp_server <span>127.0</span>.<span>0.1</span><br /> &nbsp; smtp_connect_timeout <span>30</span><br /> &nbsp; router_id <span>LVS_DEVEL</span><br />}<br /><br />vrrp_instance <span>VI_1</span> {<br /> &nbsp; &nbsp;state {{ state }} &nbsp;<span>#使用变量,变量在hosts中已经定义了</span><br /> &nbsp; &nbsp;interface eno16777746<br /> &nbsp; &nbsp;virtual_router_id <span>55</span><br /> &nbsp; &nbsp;priority {{ priority }} <span>#使用变量,变量在hosts中已经定义了</span><br /> &nbsp; &nbsp;advert_int <span>1</span><br /> &nbsp; &nbsp;authentication {<br /> &nbsp; &nbsp; &nbsp; &nbsp;auth_type <span>PASS</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;auth_pass <span>232332</span><br /> &nbsp; &nbsp;}<br /> &nbsp; &nbsp;virtual_ipaddress {<br /> &nbsp; &nbsp; &nbsp; &nbsp;<span>10.1</span>.<span>24.222</span><br /> &nbsp; &nbsp;}<br />}<br />

        (6)添加主剧本:


1
[<span>21</span>:<span>30</span> root@centos6<span>.8</span>/etc/ansible]# cat nginx.yml <br />- hosts: nginx<br /> &nbsp;remote_user: root<br /> &nbsp;roles:<br /> &nbsp;- nginx<br />

        (7)语法检测没有问题


1
[<span>21</span>:<span>34</span> root@centos6<span>.8</span>/etc/ansible]#ansible-playbook --syntax-check nginxx.yml <br /><br />playbook: nginx.yml<br />
  • 5、设置mysql:
    (1)设置mysql的task:


1
[18:46 root@centos6.8/etc/ansible]# cat roles/mysql/tasks/main.yml <br />- name: <span>install</span> mysql<br /> &nbsp;yum: <span>name</span>=mysql-<span>server</span><br />- <span>name</span>: copy <span>sql</span> <span>file</span><br /> &nbsp;copy: src=mysql.<span>sql</span> dest=/tmp/mysql.<span>sql</span><br />- <span>name</span>: <span>start</span> mysql service<br /> &nbsp;service: <span>name</span>=mysqld state=started<br />- <span>name</span>: config mysql<br /> &nbsp;shell: <span>&quot;mysql &lt; /tmp/mysql.sql&quot;</span><br /><br />

        (2)设置files文件


1
[18:47 root@centos6.8/etc/ansible]# ll roles/mysql/files/<br />total 4<br />-rw-r<span>--r--. 1 root root 78 Nov &nbsp;3 15:41 mysql.sql</span><br />[19:39 root@centos6.8/etc/ansible]# cat roles/mysql/files/mysql.sql #提供数据库的sql脚本,创建wordpres的用户和数据库<br /><span>CREATE</span> <span>DATABASE</span> wp;<br /><span>GRANT</span> ALL <span>ON</span> wp.* <span>TO</span> <span>&#039;wpuser&#039;</span>@<span>&#039;%&#039;</span> <span>IDENTIFIED</span> <span>BY</span> <span>&#039;wppass&#039;</span>;<br />

        (3)添加主剧本:


1
[<span>18</span>:<span>48</span> root@centos6<span>.8</span>/etc/ansible]# cat mysql.yml <br />- hosts: mysql<br /> &nbsp;remote_user: root<br /> &nbsp;roles:<br /> &nbsp;- mysql<br />

        (4)语法检查没有问题:


1
[<span>18</span>:<span>49</span> root@centos6<span>.8</span>/etc/ansible]# ansible-playbook --syntax-check mysql.yml <br /><br />playbook: mysql.yml<br />

        mysql的ansible配置已经完成

  • 6、至此,web、nginx+keepalived、mysql的配置都已经完成,下面来依次执行剧本
    (1)来看下整个目录的结构:


1
<span>[19:05 root@centos6.8/etc/ansible]</span># <span>tree</span> <span>-L</span> 4<br />.<br />├── <span>ansible</span><span>.cfg</span><br />├── <span>ansible</span><span>.cfg</span><span>.bak</span><br />├── <span>hosts</span><br />├── <span>mysql</span><span>.yml</span><br />├── <span>nginx</span><span>.yml</span><br />├── <span>roles</span><br />│ &nbsp; ├── <span>mysql</span><br />│ &nbsp; │ &nbsp; ├── <span>default</span><br />│ &nbsp; │ &nbsp; ├── <span>files</span><br />│ &nbsp; │ &nbsp; │ &nbsp; └── <span>mysql</span><span>.sql</span><br />│ &nbsp; │ &nbsp; ├── <span>handlers</span><br />│ &nbsp; │ &nbsp; ├── <span>meta</span><br />│ &nbsp; │ &nbsp; ├── <span>tasks</span><br />│ &nbsp; │ &nbsp; │ &nbsp; └── <span>main</span><span>.yml</span><br />│ &nbsp; │ &nbsp; ├── <span>templates</span><br />│ &nbsp; │ &nbsp; └── <span>variables</span><br />│ &nbsp; ├── <span>nginx</span><br />│ &nbsp; │ &nbsp; ├── <span>default</span><br />│ &nbsp; │ &nbsp; ├── <span>files</span><br />│ &nbsp; │ &nbsp; │ &nbsp; └── <span>nginx-1</span><span>.10</span><span>.0-1</span><span>.el7</span><span>.ngx</span><span>.x86_64</span><span>.rpm</span><br />│ &nbsp; │ &nbsp; ├── <span>handlers</span><br />│ &nbsp; │ &nbsp; │ &nbsp; └── <span>main</span><span>.yml</span><br />│ &nbsp; │ &nbsp; ├── <span>meta</span><br />│ &nbsp; │ &nbsp; ├── <span>tasks</span><br />│ &nbsp; │ &nbsp; │ &nbsp; └── <span>main</span><span>.yml</span><br />│ &nbsp; │ &nbsp; ├── <span>templates</span><br />│ &nbsp; │ &nbsp; │ &nbsp; ├── <span>keepalived</span><span>.j2</span><br />│ &nbsp; │ &nbsp; │ &nbsp; └── <span>nginx</span><span>.j2</span><br />│ &nbsp; │ &nbsp; └── <span>variables</span><br />│ &nbsp; └── <span>web</span><br />│ &nbsp; &nbsp; &nbsp; ├── <span>default</span><br />│ &nbsp; &nbsp; &nbsp; ├── <span>files</span><br />│ &nbsp; &nbsp; &nbsp; │ &nbsp; ├── <span>httpd</span><span>.conf</span><br />│ &nbsp; &nbsp; &nbsp; │ &nbsp; └── <span>wordpress</span><br />│ &nbsp; &nbsp; &nbsp; ├── <span>handlers</span><br />│ &nbsp; &nbsp; &nbsp; │ &nbsp; └── <span>main</span><span>.yml</span><br />│ &nbsp; &nbsp; &nbsp; ├── <span>meta</span><br />│ &nbsp; &nbsp; &nbsp; ├── <span>tasks</span><br />│ &nbsp; &nbsp; &nbsp; │ &nbsp; └── <span>main</span><span>.yml</span><br />│ &nbsp; &nbsp; &nbsp; ├── <span>templates</span><br />│ &nbsp; &nbsp; &nbsp; └── <span>variables</span><br />├── <span>test</span><span>.yaml</span><br />└── <span>web</span><span>.yml</span><br /><br />26 <span>directories</span>, 17 <span>files</span><br />

        (2)执行web的playbook:


1
[17:15 root<span>@centos6.8/etc/ansible]# ansible-playbook &nbsp;web.yml </span><br /><br />PLAY [web] <span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span> <br /><br />GATHERING FACTS <span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span> <br />ok: [10.1.252.235]<br />ok: [10.1.252.215]<br /><br />TASK: [web |<span> install web pakgs] *********************************************** <br />ok: [10.1.252.215] =&gt; (item=httpd,php,php-mysql)<br />ok: [10.1.252.235] =&gt; (item=httpd,php,php-mysql)<br /><br />TASK: [web </span>|<span> config the web] ************************************************** <br />ok: [10.1.252.215]<br />ok: [10.1.252.235]<br /><br />TASK: [web </span>|<span> install wordpress] *********************************************** <br />changed: [10.1.252.235]<br />changed: [10.1.252.215]<br /><br />TASK: [web </span>|<span> restart the service] ********************************************* <br />ok: [10.1.252.215]<br />ok: [10.1.252.235]<br /><br />PLAY RECAP ******************************************************************** <br />10.1.252.215 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : ok=5 &nbsp; &nbsp;changed=1 &nbsp; &nbsp;unreachable=0 &nbsp; &nbsp;failed=0 &nbsp; <br />10.1.252.235 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : ok=5 &nbsp; &nbsp;changed=1 &nbsp; &nbsp;unreachable=0 &nbsp; &nbsp;failed=0 &nbsp; &nbsp; &nbsp; &nbsp; </span><br />

        (3)执行mysql的playbook:


1
[18:52 root<span>@centos6.8/etc/ansible]# ansible-playbook &nbsp;mysql.yml </span><br /><br />PLAY [mysql] <span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span> <br /><br />GATHERING FACTS <span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span> <br />ok: [10.1.252.36]<br /><br />TASK: [mysql |<span> install mysql] ************************************************* <br />ok: [10.1.252.36]<br /><br />TASK: [mysql </span>|<span> copy sql file] ************************************************* <br />ok: [10.1.252.36]<br /><br />TASK: [mysql </span>|<span> start mysql service] ******************************************* <br />ok: [10.1.252.36]<br /><br />TASK: [mysql </span>|<span> config mysql] ************************************************** <br />skipping: [10.1.252.36]<br />ok: [10.1.252.36]<br /><br />PLAY RECAP ******************************************************************** <br />10.1.252.36 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: ok=4 &nbsp; &nbsp;changed=0 &nbsp; &nbsp;unreachable=0 &nbsp; &nbsp;failed=0 </span><br />

        (4)执行nginx的playbook:


1
[18:53 root<span>@centos6.8/etc/ansible]# ansible-playbook nginx.yml </span><br /><br />PLAY [nginx] <span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span> <br /><br />GATHERING FACTS <span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span><span>*</span> <br />ok: [10.1.249.75]<br />ok: [10.1.253.107]<br /><br />TASK: [nginx |<span> install keepalived] ******************************************** <br />ok: [10.1.253.107]<br />ok: [10.1.249.75]<br /><br />TASK: [nginx </span>|<span> copy nginx] **************************************************** <br />changed: [10.1.249.75]<br />changed: [10.1.253.107]<br /><br />TASK: [nginx </span>|<span> install nginx] ************************************************* <br />ok: [10.1.249.75]<br />ok: [10.1.253.107]<br /><br />TASK: [nginx </span>|<span> delete the nginx pkg] ****************************************** <br />changed: [10.1.249.75]<br />changed: [10.1.253.107]<br /><br />TASK: [nginx </span>|<span> config nginx&amp;keepalived] *************************************** <br />ok: [10.1.249.75]<br />ok: [10.1.253.107]<br /><br />TASK: [nginx </span>|<span> config nginx&amp;keepalived] *************************************** <br />ok: [10.1.249.75]<br />ok: [10.1.253.107]<br /><br />TASK: [nginx </span>|<span> start the service] ********************************************* <br />ok: [10.1.253.107] =&gt; (item=keepalived)<br />ok: [10.1.249.75] =&gt; (item=keepalived)<br />ok: [10.1.253.107] =&gt; (item=nginx)<br />ok: [10.1.249.75] =&gt; (item=nginx)<br /><br />PLAY RECAP ******************************************************************** <br />10.1.249.75 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: ok=8 &nbsp; &nbsp;changed=2 &nbsp; &nbsp;unreachable=0 &nbsp; &nbsp;failed=0 &nbsp; <br />10.1.253.107 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : ok=8 &nbsp; &nbsp;changed=2 &nbsp; &nbsp;unreachable=0 &nbsp; &nbsp;failed=0 &nbsp; </span><br />

        

        执行完成后,下面就是验证效果的时候:

ansibletest.png

        

        激动的时刻,成功了!

中间本人遇到的问题:在每个task中的name任务中不能有过多的任务,必须要分为多个步骤进行,如此例中的copy nginx然后再yum nginx,不能写在同一个name中,否则会报错!此外,copy的模块拷贝目录时注意最后的“/” 问题。

        OK,更多文章请关注我的博客

原创文章,作者:麦德良,如若转载,请注明出处:/57379

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1660809109@qq.com

工作时间:周一至周五,9:30-18:30,节假日同时也值班

友情链接:万达招商  测试  万达娱乐注册  万达主管QQ  万达娱乐招商  万达娱乐开户  万达娱乐平台  万达娱乐  万达娱乐主管  guoqibee.com