ansible 用法
ansible的常用模块
- command
 
ansible的默认模块,不指定-m参数的时候,使用的就是command模块;
常见的命令都可以使用,但命令的执行不是通过shell来执行的,所以< > | and & z这些操作都不可以,不支持管道,没法批量执行命令
- shell模块:
 
使用shell模块的时候默认是通过/bin/sh来执行的,所以在终端输入的各种命令都可以使用
- scripts模块
 
使用scripts模块可以在本地写一个脚本,在远程服务器上执行
案例1:使用shell模块的案例
1  | ansible -i /etc/ansible/hosts remote -m shell -a "source ~/.bash_profile && df -h|head  | 
案例2:使用script 模块
1  | cat ~/test.sh  | 
1  | ansible -i /etc/ansible/hosts remote -m script -a "~/test.sh"  | 
copy模块的使用
copy模块:实现主控端向目标主机拷贝文件,类似scp功能
案例1: 把ansible主机的/etc/hosts 拷贝到主机组机器中的/root/下
1  | ansible -i /etc/ansible/hosts remote -m copy -a "src=~/test.sh dest=/root owner=root group=root mode=0777"  | 
查看是否执行成功:
1  | ansible -m command -a "ls /root/" 'remote'  | 
file模块
案例5 给文件设置权限
1  | ansible -i /etc/ansible/hosts remote -m file -a "path=/root/test.sh mode=0755"  | 
查看权限:
1  | ansible -m command -a "ls -l /root" 'remote'  | 
stat模块获取远程文件信息
案例6 获取文件信息
1  | ansible -i /etc/ansible/hosts remote -m stat -a "path=/root/test.sh"  | 
get_url 模块
案例7 实现远程主机下载指定的url地址,支持sha256sum文件校验
1  | ansible -i /etc/ansible/hosts remote -m get_url -a "url=https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm dest=/tmp/ mode=0440 force=yes"  | 
注:url=https://xxx 的等号=前后不能有空格
扩展:查看force=yes的作用
yum模块
   yum模块linux平台软件包管理。
   yum模块可以提供的status状态: latest ,present,installed #这三个代表安装;removed, absent #这两个是卸载
案例8 使用yum模块安装httpd
1  | ansible -i /etc/ansible/hosts remote -m yum -a "name=httpd state=latest"  | 
cron模块远程管理主机crontab配置
案例9: 增加每30分钟执行 echo “hello summer”
1  | ansible -i /etc/ansible/hosts remote -m cron -a “name=‘list dir’ minute=’*/30’ job=‘echo hello summer”’”  | 
service 远程管理主机系统服务模块
   service模块常用参数:
   (1)、name参数:此参数用于指定需要操作的服务名称,比如 nginx,httpd。
   (2)、state参数:此参数用于指定服务的状态
比如,我们想要启动远程主机中的httpd,则可以将 state 的值设置为 started;
如果想要停止远程主机中的服务,则可以将 state 的值设置为 stopped。
此参数的可用值有 started、stopped、restarted(重启)、reloaded。
enabled参数:此参数用于指定是否将服务设置为开机 启动项,设置为 yes 表示将对应服务设置为开机启动,设置为 no 表示不会开机启动。
注:想使用service模块启动服务,被启动的服务,必须可以使用service 命令启动或关闭
案例10 使用service模块重启httpd
1  | ansible -i /etc/ansible/hosts remote -m service -a "name=httpd state=restarted"  | 
user模块 管理远程主机的用户
案例11: 使用user模块创建一个用户summer
1  | ansible -i /etc/ansible/hosts remote -m user -a "name=summer state=present"  | 
ansible 实战案例
playbooks的介绍
1) 在playbooks 中定义任务:
- name: task description #任务描述信息
module_name: module_args #需要使用的模块名字: 模块参数
2) ansible-playbook 执行 命令:
ansible-playbook site.yml 
playbook是由一个或多个”play”组成的列表。play的主要功能在于将事先归为一组的主机装扮成事先通过ansible中的task定义好的角色。
github上提供了大量的实例供大家参考: https://github.com/ansible/ansible-examples
实战一: 使用playbook 批量部署多台LAMP环境
先介绍下: Playbook常用文件夹作用:
- files:存放需要同步到异地服务器的源码文件及配置文件;
 - handlers:当服务的配置文件发生变化时需要进行的操作,比如:重启服务,重新加载配置文件,handlers [‘hændləz] 处理程序
 - meta:角色定义,可留空;
 - tasks:需要进行的执行的任务;
 - templates:用于执行lamp安装的模板文件,一般为脚本;
 - vars:本次安装定义的变量
 
搭建思路
思路:我们搭建lanp架构,大概需要:
yum 安装服务
service 启动
copy 把网站拷贝过去
在playbooks 中定义任务:
name: task description #任务描述信息
module_name: module_args #需要使用的模块名字:
github上提供了大量的实例供大家参考:
https://github.com/ansible/ansible-examples 4.2
使用Playbook批量部署多台LAMP环境步骤
我们可以在ansible服务器上安装LAMP环境,然后,再将配置文件通过ansible拷贝到远程主机上
第一步:安装httpd软件
1  | yum -y install httpd -y  | 
第二步:安装MySQL
1  | [root@ansible ~]# yum install mariadb-server mariadb -y #安装mysql服务  | 
第三步:安装PHP和php-mysql模块
1  | yum -y install php php-mysql  | 
第四步:提供php的测试页
1  | vim /var/www/html/index.php  | 
httpd测试:http://{ip}
确保已经出现上面的测试页,而且,要看到MySQL已经被整合进来了,才能进行下一步操作
第五;定义组名
1  | vim /etc/ansible/hosts #还使用之前定义好的,这里不用修改  | 
然后,将公钥信息复制到被控制节点,ansible和两个节点间通过ssh进行连接。下面3个命令之前已经做过,不用执行了。
1  | ssh-keygen  | 
第六:使用playbook创建一个LAMP构建的任务
1、创建相关文件
1  | [root@ansible ~]# mkdir -pv /etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handlers}  | 
我们将上面搭建成功的LAMP环境的httpd和MySQL的配置文件拷贝到对应目录下
1  | [root@summer ~]# cd /etc/ansible/  | 
2、构建httpd的任务
1  | [root@summer ansible]# cd /etc/ansible/lamp/roles/  | 
3、构建httpd的handlers
1  | [root@summer roles]# vim httpd/handlers/main.yml  | 
4、部署我们的MariaDB数据库
创建MySQL服务的任务,需要安装MySQL服务,改变属主信息,启动MySQL
1  | [root@summer roles]# cd /etc/ansible/lamp/roles/  | 
5、构建PHP的任务
1  | [root@summer roles]# vim php/tasks/main.yml  | 
6、定义整个的任务
1  | [root@summer roles]# cd /etc/ansible/lamp/roles/  | 
  注:所有yml的配置文件中,空格必须严格对
开始部署:
1  | [root@summer roles]# ansible-playbook -i /etc/ansible/hosts /etc/ansible/lamp/roles/site.yml  | 
然后,在浏览器中访问这两台节点主机,可以直接访问成功.
总结:做此实验室,需要准备干净环境,selinux、防火墙都要关闭
实战二: 使用ansible部署k8s及集群
安装git命令
1  | yum install git  | 
使用git下载相应的ansible-k8s-insatall 包:
1  | git clone https://github.com/lizhenliang/ansible-install-k8s  | 
进入到ansbile-install-k8s目录
修改hosts文件,根据规划修改对应IP和名称。
1  | cd ansible-install-k8s  | 
部署命令:
单Master版
1  | ansible-playbook -i hosts single-master-deploy.yml -uroot -k  | 
多master版
1  | ansible-playbook -i hosts multi-master-deploy.yml -uroot -k  |