知识
1、ansible命令执行方式
ansible提供了多少个模块给你用
[root@master-61 ~]#ansible-doc -l |wc -l
3387
Ansible实现批量管理主机的模式主要有俩:
利用ansible命令实现批量管理(ad-hoc)模式
利用ansible剧本实现批量管理(playbook)模式
Ad-hoc和playbook的关系就好比shell命令与shell scripts的关系
ad-hoc模式
ad-hoc模式
也就是ansible的命令行模式,该模式通常用来临时处理一些任务。例如临时批量查看所有被管控机器的内存、负载、磁盘临时批量分发某个特定文件
Playbook模式
Ansible的playbook模式就是针对特定的具体较大的任务,事先写好执行剧本,然后在其他机器上批量执行相同的任务,属于定制化的批量执行任务,例如
一键安装Rsync
一键搭建LNMP集群等
ansible-doc命令
列出ansible所有支持的模块,这就是ansible这个万能工具箱所有的零件了。
[root@master-61 ~]#ansible-doc -l |grep ^ping
ping
[root@master-61 ~]#ansible-doc -l |grep ^shell
shell
查看某个模块的具体用法
[root@master-61 ~]#ansible-doc -s shell
[root@master-61 ~]#ansible-doc -s ping
2、ansible主机清单配置文件语法(重要)
1.主配置文件
/etc/ansible/hosts 主机清单文件
2.文件语法
注意,部分资料里的主机配置文件语法,旧版如下
Ansible 2.0 has deprecated the “ssh” from ansible_ssh_user, ansible_ssh_host, and ansible_ssh_port to become
这是旧版本的用法
ansible_ssh_host
ansible_ssh_port
ansible_ssh_user
ansible_ssh_password
新版参数去掉了中间的_ssh
ansible_user
ansible_host
ansible_port
参数 | 参数类型 | 参数说明 |
ansible_host | 主机地址 | 远程主机ip |
ansible_port | 主机端口 | 设置SSH连接端口,默认22 |
ansible_user | 主机用户 | 默认SSH远程连接的用户身份 |
ansible_password | 用户密码 | 指定SSH远程主机密码 |
3、ansible核心内容(模块学习)
运维远程执行命令,有2个方式
shell脚本,远程执行
ansible模块,远程执行
区别在哪
shell脚本不够智能,不会记录上一次的执行状态,以及修改的状态,因此导致,傻瓜式的,重复性执行。效率是极其低下的,不做状态记录
1.目前状态
ansible的状态,就是如下的颜色区分
绿色:命令以用户期望的执行了,但是状态没有发生改变;
黄色:命令以用户期望的执行了,并且状态发生了改变;
紫色:警告信息,说明ansible提示你有更合适的用法;出现了warning警告
红色:命令错误,执行失败;
蓝色: 详细的执行过程;
2.Ping
命令语法
ansible 主机组 -m 模块名 [模块参数]
如
[root@master-61 ~]#ansible-doc -s ping
[root@master-61 ~]#ansible web -m ping
3.Command
命令语法
ansible 主机组 -m command -a "需要批量执行的命令"
如
[root@master-61 ~]#ansible web -a "hostname"
[root@master-61 ~]#ansible web -a "free -m"
[root@master-61 ~]#ansible web -m command -a "touch /opt/人生无常大肠包小肠.log"
[root@master-61 ~]#ansible web -m command -a "cat /opt/人生无常大肠包小肠.log"
[root@master-61 ~]#ansible web -a "uptime"
root@master-61 ~]#ansible web -m command -a "touch /opt/人生无常大肠包小肠.log warn=false "
[root@master-61 ~]#ansible web -m command -a "useradd yuchao01"
备份/var/log日志目录,需要先进入根目录
cd / && tar -zcvf /opt/log.tgz /var/log
注意你备份文件存放的文件夹是否存在
[root@master-61 ~]#ansible web -m command -a "tar -zcf /opt/log.tgz /var/log chdir=/"
[root@master-61 ~]#ansible web -a "ls -l /opt"
参数
chdir | 在执行命令执行,通过cd命令进入指定目录 |
creates | 定义一个文件是否存在,若不存在,则运行相应命令;存在则跳过 |
free_form(必须) | 参数信息中可以输入任何系统命令,实现远程管理 |
removes | 定义一个文件是否存在,如果存在,则运行相应命令;如果不存在则跳过 |
缺点
不得出现特殊符号< 、>、|、;、&无法识别,需要则使用shell模块实现,也就是无法使用复杂的linux命令
4.shell
远程过滤ssh进程信息
ansible all -m shell -a "ps -ef|grep ssh"
远程获取时间信息,且写入到文件中
[root@master-61 ~]#ansible web -m shell -a "date '+%F %T' > /tmp/date.log"
[root@master-61 ~]#ansible web -m shell -a 'cat /tmp/date.log'
综合
创建文件夹,生成sh脚本文件(查看主机名),赋予脚本可执行权限,执行脚本,忽略warning信息
ansible nfs -m shell -a "mkdir /0224/;echo 'hostname' > /0224/hostname.sh;chmod +x /0224/hostname.sh;/0224/hostname.sh; warn=false"
5.copy
语法
ansible 主机组 -m copy -a "参数"
如
远程拷贝文件,且修改权限,为600
[root@master-61 ~]#ansible web -m copy -a "src=/tmp/61-dnf.log dest=/opt/web-dnf.log group=www owner=www mode=600 "
远程检查文件信息
[root@master-61 ~]#ansible web -m shell -a "ls -l /opt/web-dnf.log"
远程拷贝/opt 整个目录到目标机器
[root@master-61 ~]#ansible web -m copy -a "src=/opt dest=/tmp/"
远程拷贝/opt/ 下的所有内容到目标机器
[root@master-61 ~]#ansible web -m copy -a "src=/opt/ dest=/tmp/"
综合
发送文件且先做好备份
1.检查目标机器的文件
[root@master-61 ~]#ansible web -m shell -a "ls -l /opt/web-dnf.log"
2.远程拷贝文件,且做好备份
[root@master-61 ~]#ansible web -m copy -a "src=/tmp/61-dnf.log dest=/opt/web-dnf.log backup=yes"
3.发现ansible帮你做好了备份
[root@master-61 ~]#ansible web -m copy -a "src=/tmp/61-dnf.log dest=/opt/web-dnf.log backup=yes" ^C
4.[root@master-61 ~]#ansible web -m shell -a "ls -l /opt/web*"
6.file
远程在web服务器组中,创建一个文本
[root@master-61 ~]#ansible web -m file -a "path=/opt/hello_ansible.log state=touch"
[root@master-61 ~]#ansible web -m shell -a "ls -ld /opt/hello*"
创建文件夹
[root@master-61 ~]#ansible web -m file -a "path=/opt/hello_ansible state=directory"
创建并设置权限
ansible web -m file -a "path=/opt/hello-linux.log state=touch owner=www group=www
修改文件属性
[root@master-61 ~]#ansible web -m file -a "path=/opt/hello-linux.log state=touch owner=www group=www mode=777"
修改已存在文件权限
修改文件 Path、mode
[root@master-61 ~]#ansible 172.16.1.7 -m file -a "path=/opt/chaoge666.log owner=www group=www mode=666"
建软连接
ansible web -m file -a "src=/etc/hosts dest=/opt/hosts state=link "
强制
[root@master-61 ~]#ansible web -m file -a "src=/etc/hostsss dest=/opt/hosts state=link force=yes "
7.Script
参数
creates | 定义一个文件是否存在,若不存在,则运行相应命令;存在则跳过 |
free_form(必须) | 参数信息中可以输入任何系统命令,实现远程管理 |
removes | 定义一个文件是否存在,如果存在,则运行相应命令;如果不存在则跳过 |
一键安装rsync
1.管理机创建测试脚本
master-61创建该脚本
[root@master-61 ~]#cat echo_server_info.sh
echo "$(hostname -I)" >> /tmp/server_info.log
echo "$(uptime)" >> /tmp/server_info.log
echo "$(free -m)" >> /tmp/server_info.log
添加执行权限
[root@master-61 ~]#chmod +x echo_server_info.sh
远程执行
发给nfs机器去执行
检查结果
2.远程安装nginx脚本
[root@master-61 ~]#cat install_nginx.sh
#yum install nginx -y
yum remove nginx -y
echo "ansible很重要,是你挣钱的工具"
[root@master-61 ~]#
[root@master-61 ~]#ansible nfs -m script -a "/root/install_nginx.sh"
3.查看命令执行详细过程
-vvvvv参数显示详细过程,v越多,越详细
显示命令执行的详细过程,开启了debug日志模式
[root@master-61 ~]#ansible nfs -vvvvv -m shell -a "df -h"
其他
Cron定时模块
Group管理系统用户组
User用户模块
Yum安装模块
Mount挂载
Archive压缩
Unarchive解压
准备工作
1、ansible安装部署
yum install epel-release ansible libselinux-python -y
前提你配置好了阿里云的epel源可以直接安装
yum install ansible -y
安装好查看版本
[root@master-61 ~]#ansible --version
ansible 2.9.27
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
2、ansible主机登录认证连接
Ansible批量管理主机有两种方式
传统的密码认证
在你的客户端机器、修改了ssh默认端口、以及密码需要修改主机清单文件才可以正确连接。注意你得配置允许密码登录才能进行如下测试
公钥认证
1.将master61机器的公钥,分发给想免密登录的机器
2.后续在对该机器操作,就直接进行ssh的公钥认证了,可以免密码,直接远程执行
方案1
已经基于ssh完成了指纹确认,认证方式
ansible直接用就可以
你可以先一键分发公钥,实现批量免密登录,再ansible免密远程执行命令
方案2
ssh root@172.16.1.7
手动确认yes,写入到本地的known_hosts
你可以手动ssh连接,确认指纹后,再ansible去远程操作,选择认证方式就行
方案3,
你可以直接忽略指纹确认,在主机清单文件中定义好ssh连接配置参数
这个是最简单的,修改ansible配置文件,打开忽略指纹确认的参数即可
修改如下参数即可
72 # uncomment this to disable SSH key host checking
73 host_key_checking = False
后续就进入了认证方式阶段,选择密码,还是公钥
实验一
主机清单配置文件
vim /etc/ansible/hosts
[web]
172.16.1.74
172.16.1.84
172.16.1.94
[nfs]
172.16.1.34
[bakcup]
172.16.1.44
同一组连续的ip
可以修改主机清单文件如下,前提是该些主机的配置一致
[web]172.16.1.[7:9]
公共变量
当主机清单里,很多主机组,有相同的变量属性,可以写成公共变量
这部分配置是针对web主机组,抽象的变量
[root@master-61 ~]#grep -vE '^#|^$' /etc/ansible/hosts
[web:vars]
ansible_ssh_port=22999
ansible_ssh_user=root
ansible_ssh_pass=123123
[web]
172.16.1.[7:9]
[nfs]
172.16.1.31 ansible_ssh_port=22999
[backup]
172.16.1.41 ansible_ssh_port=22999 ansible_ssh_user=root ansible_ssh_pass=123123
1.给rsync机器,进行密码认证
1.给rsync机器,添加密码,端口等信息
[backup]
172.16.1.44 ansible_port=22 ansible_password='123123'
2.如果目标机器的ssh信息都被改了,这里也得改
[backup]
172.16.1.44 ansible_port=22999 ansible_password='123456'
2.添加rsync机器的ssh信息
Ansible软件使用的前提是SSH+KEY免密验证的环境,如果没有配置也可以使用Ansible,如下
[root@master-61 ~#tai7 -2 /etc/ansible/hosts[backup]172.16.1.44 ansible port=22999 ansible user=root ansible password=123123
测试执行
[root@master-61~#ansible backup -m ping
172.16.1.44|SUCCESS =>{
"ansible facts":{
"discovered_interpreter_python":"/usr/bin/python'
"changed": false,
"ping":"pong"
3.拿web机器测试(单独操作某主机)
1.先配置主机组的参数
[web]
172.16.1.74 ansible_port=22999 ansible_password='123123'
172.16.1.84 ansible_port=22999 ansible_password='123123'
172.16.1.94 ansible_port=22999 ansible_password='123123'
这样麻烦可以参照上述通过变量方式简洁
2.执行ping模块,看下是否和远程主机通信
[root@master-61 ~]#ansible web -m ping
172.16.1.74 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
4.测试web组和backup组是否可用
ansible ad-hoc命令
1.web机器组
[root@master-61 ~]#ansible web -m ping
2.rsync机器
[root@master-61 ~]#ansible backup -m shell -a "touch /opt/已给我毅力giao warn=false"
172.16.1.41 | CHANGED | rc=0 >>
[root@master-61 ~]#ansible backup -m shell -a "ls /opt/"
172.16.1.41 | CHANGED | rc=0 >>
已给我毅力giao
5.所有主机都生效的变量(最终版)
指定主机组名all,即可针对所有主机生效,前提是,你要确保这个信息是所有主机通用的。
[root@master-61 ~]#grep -vE '^#|^$' /etc/ansible/hosts
[all:vars]
ansible_port=22999
#ansible_user=root
#ansible_password=123123
[web]
172.16.1.7
172.16.1.8
172.16.1.9
[nfs]
172.16.1.31
[backup]
172.16.1.41
远程执行命令(测试)
[root@master-61 ~]#rm -rf ~/.ansible/cp/*
[root@master-61 ~]#
[root@master-61 ~]#ansible all -m shell -a hostname
[root@master-61 ~]#ansible all -m shell -a hostname
172.16.1.31 | CHANGED | rc=0 >>
nfs-31
172.16.1.8 | CHANGED | rc=0 >>
web-8
172.16.1.41 | CHANGED | rc=0 >>
rsync-41
172.16.1.7 | CHANGED | rc=0 >>
web-7
172.16.1.9 | CHANGED | rc=0 >>
web-9
一定要学会看报错
Ansilel-doc -l | wc -l
如今的运维需要学这几样东西
ansible
docker k8s
阿里云运维
shell