实战-使用 Playbook 批量部署多台 LAMP 环境
playbooks 使用步骤
playbook 是一个不同于使用 ansible 命令行执行方式的模式,功能更强大更灵活。
1、在 playbooks 中定义任务:
- name: task description #任务描述信息
module_name: module_args #需要使用的模块名字: 模块参数
2、ansible-playbook 执行 命令:
[root@xuegod63 ~]# ansible-playbook site.yml
playbook 是由一个或多个"play"组成的列表。play 的主要功能在于将事先归为一组的主机装扮成事先通过Ansible 中的 task 定义好的角色。
github 上提供了大量的实例供大家参考 https://github.com/ansible/ansible-examples
实战 1:使用 Playbook 批量部署多台 LAMP 环境
Playbook 常用文件夹作用:
files:存放需要同步到异地服务器的源码文件及配置文件;
handlers:当服务的配置文件发生变化时需要进行的操作,比如:重启服务,重新加载配置文件,handlers['hændlə z] 处理程序
meta:角色定义,可留空; meta ['metə] 元
tasks:需要进行的执行的任务;
templates:用于执行 lamp 安装的模板文件,一般为脚本; templates ['templɪts] 模板
vars:本次安装定义的变量
首先,我们可以在 Ansible 服务器上安装 LAMP 环境,然后,再将配置文件通过 Ansible 拷贝到远程主机上
第一步:安装 httpd 软件
[root@xuegod63 ~]# yum install httpd -y
第二部:安装 MySQL
root@xuegod63 ~]# yum install mariadb-server mariadb -y
[root@xuegod63 ~]# mkdir -p /mydata/data #创建目录作为数据存放的位置
[root@xuegod63 ~]# chown -R mysql:mysql /mydata/
[root@xuegod63 ~]# vim /etc/my.cnf #改变数据存放目录
改:2 datadir=/var/lib/mysql
为:2 datadir=/mydata/data
[root@xuegod63 ~]# systemctl start mariadb #启动 MariaDB
第三步:安装 PHP 和 php-mysql 模块
[root@xuegod63 ~]# yum install php php-mysql --y
第四步:提供 php 的测试页
[root@xuegod63 ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>
启动 httpd 服务,在浏览器中访问
[root@xuegod63 ~]# systemctl restart httpd
[root@xuegod63 ~]# iptables -F
测试:http://192.168.1.63/index.php

确保已经出现上面的测试页,而且,要看到 MySQL 已经被整合进来了,才能进行下一步操作
定义组名:
[root@xuegod63 ~]# vim /etc/ansible/hosts #还使用之前定义好的,这里不用修改
[web-servers]
192.168.1.63
192.168.1.64
然后,将公钥信息复制到被控制节点,Ansible 和两个节点间通过 ssh 进行连接。下面 3 个命令之前已经做过,
不用执行了。
[root@xuegod63 ~]# ssh-keygen
[root@xuegod63 ~]# ssh-copy-id root@192.168.1.63
[root@xuegod63 ~]# ssh-copy-id root@192.168.1.64
使用 playbook 创建一个 LAMP 构建的任务
1、创建相关文件
[root@xuegod63 ~]# mkdir -pv
/etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handler
s}
我们将上面搭建成功的 LAMP 环境的 httpd 和 MySQL 的配置文件拷贝到对应目录下
[root@xuegod63 ~]# cd /etc/ansible/
[root@xuegod63 ~]# cp /etc/httpd/conf/httpd.conf lamp/roles/httpd/files/
[root@xuegod63 ~]# cp /etc/my.cnf lamp/roles/mysql/files/写 prepare(前期准备)角色的 playbooks
[root@xuegod63 ansible]# vim lamp/roles/prepare/tasks/main.yml #复制以下红色内容到文件中,
配置好 yum 源
- 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 #清除原有防火墙规则,不然后可能上不了网
2、构建 httpd 的任务
[root@xuegod63 ansible]# cd /etc/ansible/lamp/roles
[root@xuegod63 roles]# mv /var/www/html/index.php httpd/files/
[root@xuegod63 roles]# vim 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运行。
扩展:notify 和 handlers notify [ˈnə ʊ tɪfaɪ] 通知
notify: 这个 action 可用于在每个 play 的最后被触发,这样可以避免多次有改变发生时,每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。
在 notify 中列出的操作称为 handler,也即 notify 中调用 handler 中定义的操作。
---- name: test.yml just for test
hosts: testserver
vars:
region: ap-southeast-1
tasks:
- name: template configuration
file template: src=template.j2 dest=/etc/foo.conf
notify:
-
restart memcached
-
restart apache
handlers:
- name: restart memcached
service: name=memcached state=restarted
- name: restart apache
service: name=apache state=restarted
handlers 概述:
Handlers 也是一些 task 的列表,通过名字来引用,它们和一般的 task 并没有什么区别。
Handlers 是由通知者进行 notify, 如果没有被 notify,handlers 不会执行。
不管有多少个通知者进行了 notify,等到 play 中的所有 task 执行完成之后,handlers 也只会被执行一次。
Handlers 最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了。
3、构建 httpd 的 handlers
[root@xuegod63 roles]# vim httpd/handlers/main.yml
- name: restart httpd
service: name=httpd enabled=yes state=restarted
4、部署我们的 MariaDB 数据库
创建 MySQL 服务的任务,需要安装 MySQL 服务,改变属主信息,启动 MySQL
[root@xuegod63 roles]# cd /etc/ansible/lamp/roles
[root@xuegod63 roles]# vim 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、构建 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、定义整个的任务
[root@xuegod63 roles]# cd /etc/ansible/lamp/roles
[root@xuegod63 roles]# vim site.yml #写入以下内容
- name: LAMP build
remote_user: root
hosts: web-servers
roles:
-
prepare
-
mysql
-
php
-
httpd
注:所有 yml 的配置文件中,空格必须严格对齐

开始部署:
[root@xuegod63 roles]# ansible-playbook -i /etc/ansible/hosts
/etc/ansible/lamp/roles/site.yml
然后,在浏览器中访问这两台节点主机,可以直接访问成功。

注:
1、默认情况下,首次登陆一台服务器,系统会提示是否要记住对端的指纹,用 Ansible 也会这样,这样会导致需要手工输入 yes 或 no,Ansible 才可以往下执行。如需避免这种情况,需要在 /etc/ansible/ansible.cfg 文件中设置 host_key_checking = False
例 1:
[root@xuegod63 roles]# rm -rf /root/.ssh/known_hosts
[root@xuegod63 roles]# ansible-playbook -i /etc/ansible/hosts ./site.yml #发现需要输入 yes,
来保存对端的指纹
解决:
[root@xuegod63 roles]# vim /etc/ansible/ansible.cfg
改:62 #host_key_checking = False #就是把前面的#号去了
为:host_key_checking = False
[root@xuegod63 roles]# rm -rf /root/.ssh/known_hosts
[root@xuegod63 roles]# ansible-playbook -i /etc/ansible/hosts ./site.yml #发现不需要输入 yes,
可以自动安装了