Ansible(一)
Ansible
前言
什么是Ansible
?
Ansible是一个自动化的配置管理工具,自动化主要体现在Ansible
集成了丰富模块,丰富的功能的组件,可以通过一个命令行完成一系列的操作。进而能减少我们重复性的工作和维护成本,以提高工作的效率。
假设我们要在10台Linux
服务器上安装一个nginx
服务,手动如何做的?
第一步:ssh登录num(1..10)服务器
第二步:输入对应服务器密码
第三步:安装 yum install nginx
第四步:启动 systemctl start nginx
第五步:退出登录
依次重复10次
Ansible
可以完成哪些功能
- 批量执行远程命令,可以对多台主机同时进行命令的执行;
- 批量配置软件服务,可以进行自动化的方式配置和管理服务;
- 实现软件开发功能,
jumpserver
底层使用ansible
来实现的自动化管理; - 编排高级的IT任务,
Ansible
的Playbook
是一门编程语言,可以用来描绘一套IT架构;
Ansible
特点
- 容易学习,无代理模式,不想
saltstack
既要学习客户端与服务端,还要学习客户端与服务端中间的通讯协议; - 操作灵活,体现在
Ansible
有较多的模块,提供了丰富的功能、playbook
则提供类似; - 简单易用,体现在
Ansible
一个命令可以完成很多事情; - 安全可靠,因为
Ansible
使用了SSH
协议进行通讯,即稳定也安全; - 移植性高,可以将写好的
playbook
拷贝至任意机器进行执行;
Ansible
的架构中的控制节点、被控制节点、inventory、ad-hoc、playbook、连接协议这些是什么?
Ansible
1、主机架构
外网IP | 内网IP | 角色 | 备注 |
---|---|---|---|
10.0.0.61 | 172.16.1.61 | Ansible控制端 | |
10.0.0.7 | 172.16.1.7 | Ansible被控端 | |
10.0.0.8 | 172.16.1.8 | Ansible被控端 |
Ansible安装
1.先安装epel
源(提供最新的ansible
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
2.安装Ansible
yum install ansible -y
# ansible <host-pattern> [options]
--version #显示ansible版本信息
-v #显示详细信息
-i #主机清单文件路径,默认是在/etc/ansible/hosts
-m #使用的模块名称,默认使用command模块
-a #使用的模块参数,模块的具体动作
-k #提示输入ssh密码,而不使用基于ssh的密钥认证
-C #模拟执行测试,但不会真的执行
-T #执行命令的超时
查看ansible的版本
[root@m01 ~]# ansible --version
ansible 2.7.7
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)]
3.Ansible
的配置文件可以存放任何位置,但配置文件有读取顺序,先对ansible
配置做一个基本了解;
- 最先查找
$ANSIBLE_CONFIG
变量; - 其次查找当下目录下
ansible.cfg
; - 然后查找用户家目录下的
.ansible.cfg
; - 最后查找
/etc/ansible/ansible.cfg
(默认)
[root@m01 ~]# cat /etc/ansible/ansible.cfg
#inventory = /etc/ansible/hosts #主机列表配置文件
#library = /usr/share/my_modules/ #库文件存放目录
#remote_tmp = ~/.ansible/tmp #临时py文件存放在远程主机目录
#local_tmp = ~/.ansible/tmp #本机的临时执行目录
#forks = 5 #默认并发数
#sudo_user = root #默认sudo用户
#ask_sudo_pass = True #每次执行是否询问sudo的ssh密码
#ask_pass = True #每次执行是否询问ssh密码
#remote_port = 22 #远程主机端口
host_key_checking = False #跳过检查主机指纹
log_path = /var/log/ansible.log #ansible日志
[privilege_escalation] #如果是普通用户则需要配置提权
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False
4. Ansible Ineventory
Inventory
文件中填写需要被管理主机与主机组信息(逻辑上定义)。默认Inventory
文件在/etc/ansible/hosts
。当然也可以自定义,然后使用-i
指定inventory
文件的位置,下面通过几个场景演示,如何配置inventory
文件。
1.场景一、基于密码连接
[root@oldboy.com ~]# cat /etc/ansible/hosts
#方式一、主机+端口+密码
[webservers]
172.16.1.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
172.16.1.8 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
[root@m01 ~]# ansible webservers -m ping -i ./hosts
172.16.1.8 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.7 | SUCCESS => {
"changed": false,
"ping": "pong"
}
#方式二、主机+端口+密码
[webservers]
web[1:2].oldboy.com ansible_ssh_pass='123456'
#方式三、主机+端口+密码
[webservers]
web[1:2].oldboy.com
[webservers:vars]
ansible_ssh_pass='123456'
[webservers]
web01 ansible_ssh_host=172.16.1.7 ansible_ssh_port=22
web02 ansible_ssh_host=172.16.1.8
场景二、基于密钥连接,需要先创建公钥和私钥,并下发公钥至被控端
具体操作参照:
[root@m01 ~]# ssh-keygen
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8
#方式一、主机+端口+密钥
[root@m01 ~]# cat hosts
[webservers]
172.16.1.7
172.16.1.8
[root@m01 ~]# ansible webservers -m ping -i ./hosts
172.16.1.8 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.7 | SUCCESS => {
"changed": false,
"ping": "pong"
}
#方式二、别名+主机+端口+密钥
[root@m01 ~]# cat hosts
[webservers]
web01 ansible_ssh_host=172.16.1.7 ansible_ssh_port=22
web02 ansible_ssh_host=172.16.1.8
[root@m01 ~]# ansible webservers -m ping -i ./hosts
web02 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web01 | SUCCESS => {
"changed": false,
"ping": "pong"
}
场景三、主机组使用方式
#1.定义两个组
[lbservers]
172.16.1.5
172.16.1.6
[webservers]
172.16.1.7
172.16.1.8
#2.servers组包括两个子组[lbservers,webserver]
[servers:children]
[lbservers]
[webserver]
#列出当前某个组有多少台主机
[root@m01 ~]# ansible lbservers -m ping -i ./hosts --list-hosts
hosts (1):
web01
[root@m01 ~]# ansible webservers -m ping -i ./hosts --list-hosts
hosts (1):
web02
[root@m01 ~]# ansible servers -m ping -i ./hosts --list-hosts
hosts (2):
web01
web02
[root@m01 ~]# ansible all -m ping -i ./hosts --list-hosts
hosts (3):
web03
web02
web01
注意:
如果控制端和被控制端第一次通讯,需要先添加指纹信息,那如果机器特别多少的情况下怎么办?
仅需开启ansible
中的 host_key_checking = False
Ad-Hoc,常用模块
1.什么是ad-hoc
ad-hoc
简而言之就是”临时命令“,执行完即结束,并不会保存
2.ad-hoc
模式的使用场景
比如多台机器上看某个进程是否启动,或拷贝指定文件到本地,等等
3.ad-hoc
模式的命令使用,ansible 'zxm' -m command -a 'df -h'
,含义如下图
4.使用ad-hoc执行一次远程命令,注意观察返回结果的颜色
绿色:代表被管理主机没有被修改
黄色:代表被管理端主机发现更改
红色:代表出现了故障,注意查看提示
5.常用模块
command # 执行shell命令(不支持管道等特殊字符)
shell # 执行shell命令
scripts # 执行shell脚本
yum_repository # 配置yum仓库
联网下载 get_url
安装 yum
配置 copy
启动 service、systemd
创建用户与组 user、group
授权 file
定时任务 crond
挂载 mount
firewalld firewall
selinux selinux
6.使用过程中需要先了解ansible-doc帮助手册
[root@m01 ~]# ansible-doc -l # 查看所有模块说明
[root@m01 ~]# ansible-doc copy # 表示指定模块方法
[root@m01 ~]# ansible-doc -s copy # 表示指定模块参数
1.执行命令模块
1.command
默认执行bash
命令模块,模块不支持重定向或管道
#默认模块,执行命令
[root@m01 ~]# ansible oldboy -a "hostname"
#shell模块,如果需要一些管道操作,则使用shell
[root@m01 ~]# ansible oldboy -m shell -a "ifconfig|grep eth0" -f 50
2.script脚本模块
[root@m01 ~]# cat yum.sh
#!/usr/bin/bash
yum install -y iftop
#在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
[root@m01 ~]# ansible oldboy -m script -a "/server/scripts/yum.sh"
2.软件管理模块
3.yum安装软件模块
[root@m01 ~]# ansible oldboy -m yum -a "name=httpd state=installed"
name httpd #指定要安装的软件包名称
file:// #指定从本地哪个目录安装rpm
http:// #指定从哪个网站安装rpm包
state #指定使用yum的方法
installed,present #安装软件包
removed, absent #移除软件包
latest #安装最新软件包
list=ansible #列出当前仓库可用的软件包
disablerepo="epel,ol7_latest" #安装软件时,不从哪些仓库获取
download_only=true #仅下载软件包,不安装
3.文件管理模块
1.copy文件拷贝模块
#1.拷贝文件文件至被控节点
[root@m01 ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/test.txt"
#2.对远端已有文件进行备份,按照时间信息备份
[root@m01 ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/test.txt backup=yes"
#3.向被控端主机写入数据,并且会覆盖远端文件内原有数据信息
[root@m01 ~]# ansible oldboy -m copy -a "content='bgx' dest=/tmp/oldboy"
src #推送数据的源文件信息
dest #推送数据的目标路径
backup #对推送传输过去的文件,进行备份
content #直接批量在被管理端文件中添加内容
group #将本地文件推送到远端,指定文件属组信息
owner #将本地文件推送到远端,指定文件属主信息
mode #将本地文件推送到远端,指定文件权限信息
2.file文件创建模块
1.直接修改被控端的权限
[root@m01 ~]# ansible web01 -m file -a "path=/opt mode=0400" -i ./hosts
2.在被控端创建目录
[root@m01 ~]# ansible oldboy -m file -a "path=/tmp/oldboy state=directory"
3.在被控端创建文件
[root@m01 ~]# ansible oldboy -m file -a "path=/tmp/tt state=touch mode=555 owner=root group=root"
4.递归授权目录权限
[root@m01 ~]# ansible oldboy -m file -a "path=/data owner=bgx group=bgx recurse=yes"
path #指定远程主机目录或文件
recurse #递归授权
state #状态
directory #在远端创建目录
touch #在远端创建文件
link #创建链接文件
absent #表示删除文件或目录
mode #设置文件或目录权限
owner #设置文件或目录属主
group #设置文件或目录属组
3.get_url文件下载模块
1.通过get_url下载文件或者软件
[root@m01 ~]# ansible webservers -m get_url -a "url=http,https dest=/opt mode=0777" -i ./hosts
2.下载一个文件前先进行md5校验,通过则下载,不通过则失败
ansible webservers -m get_url -a "url=http,https dest=/opt mode=0777 checksum=md5:76eb3af80ffd" -i ./hosts
url #文件在网络上的具体位置
dest #下载到被控端的哪个目录下
checksum #校验(md5 sha256)
4.服务管理模块
ansible
管理服务的启动与停止,使用service
、systemd
#1.启动`crond`服务,并加入开机自启
[root@m01 ~]# ansible webservers -m service -a "name=crond state=started enabled=yes"
#2.停止crond服务,并删除开机自启
[root@m01 ~]# ansible webservers -m service -a "name=crond state=stopped enabled=no"
#3.重启crond服务
[root@m01 ~]# ansible webservers -m service -a "name=crond state=restarted"
#4.重载crond服务
[root@m01 ~]# ansible webservers -m service -a "name=crond state=reloaded"
name # 定义要启动服务的名称
state # 指定服务状态
started #启动服务
stopped #停止服务
restarted #重启服务
reloaded #重载服务
enabled #开机自启
5.用户管理模块
ansible
管理用户与组,通常时间user
、group
模块
1.group组模块
[root@m01 ~]# ansible webservers -m group -a "name=oldgirl gid=888"
name #指定创建的组名
gid #指定组的gid
state
absent #移除远端主机的组
present #创建远端主机的组(默认)
2.user模块
1.创建用户指定uid和gid,不创建家目录也不允许登陆
[root@m01 ~]# ansible oldboy -m user -a "name=oldgirl uid=888 group=888 shell=/sbin/nologin create_home=no"
2.删除用户
[root@m01 ~]# ansible webservers -m user -a "name=tmd state=absent" -i ./hosts
3.给新创建的用户生成ssh密钥对
[root@m01 ~]# ansible webservers -m user -a "name=oo uid=6677 group=adm generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa" -i ./hosts
4.将明文密码进行hash加密,然后进行用户创建
[root@m01 ~]# ansible localhost -m debug -a "msg={{ '123456' | password_hash('sha512', 'salt') }}"
localhost | SUCCESS => {
"msg": "$6$salt$MktMKPZJ6t59GfxcJU20DwcwQzfMvOlHFVZiOVD71w.igcOo1R7vBYR65JquIQ/7siC7VRpmteKvZmfSkNc69."
}
[root@m01 ~]# ansible webservers -m user -a 'name=xlw password=$6$salt$MktMKPZJ6t59GfxcJU20DwcwQzfMvOlHFVZiOVD71w.igcOo1R7vBYR65JquIQ/7siC7VRpmteKvZmfSkNc69. create_home=yes shell=/bin/bash' -i ./hosts
uid #指定用户的uid
group #指定用户组名称
groups #指定附加组名称
password #给用户添加密码(记得单引号)
shell #指定用户登录shell
create_home #是否创建家目录
6.crond定时任务模块
正常使用crond服务(默认没写的时间都算*表示)
[root@m01 ~]# crontab -l
- - - - - /bin/sh /server/scripts/yum.sh
使用ansible添加一条定时任务
[root@m01 ~]# ansible webservers -m cron -a "minute=* hour=* day=* month=* weekday=* job='/bin/sh test.sh'"
[root@m01 ~]# ansible webservers -m cron -a "job='/bin/sh /server/scripts/test.sh'"
设置定时任务注释信息,防止重复,name设定
[root@m01 ~]# ansible webservers -m cron -a "name='cron01' job='/bin/sh /server/scripts/test.sh'"
删除相应定时任务
[root@m01 ~]# ansible webservers -m cron -a "name='ansible cron02' minute=0 hour=0 job='/bin/sh test.sh' state=absent"
注释相应定时任务,使定时任务失效
[root@m01 scripts]# ansible oldboy -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh test.sh' disabled=yes"
7.mount挂载模块7 nfs 8客户端挂载
[root@m01 ~]# ansible web01 -m yum -a 'name=nfs-utils state=present' -i ./hosts
[root@m01 ~]# ansible web01 -m file -a 'path=/data state=directory' -i ./hosts
[root@m01 ~]# ansible web01 -m copy -a 'content="/data 172.16.1.0/24(rw,sync,no_all_squash)" dest=/etc/exports' -i ./hosts
[root@m01 ~]# ansible web01 -m systemd -a "name=nfs state=started enabled=yes" -i ./hosts
[root@m01 ~]# ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=present"
[root@m01 ~]# ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=mounted"
[root@m01 ~]# ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=unmounted"
[root@m01 ~]# ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=absent"
present # 开机挂载,仅将挂载配置写入/etc/fstab
mounted # 挂载设备,并将配置写入/etc/fstab
unmounted # 卸载设备,不会清除/etc/fstab写入的配置
absent # 卸载设备,会清理/etc/fstab写入的配置
8.防火墙管理模块
#Selinux模块
[root@m01 ~]# ansible webservers -m selinux -a "state=disabled" -i ./hosts
#firewalld模块
[root@m01 ~]# ansible webservers -m systemd -a "name=firewalld state=started" -i ./hosts
[root@m01 ~]# ansible webservers -m firewalld -a "service=http immediate=yes permanent=yes state=enabled" -i ./hosts
[root@m01 ~]# ansible webservers -m firewalld -a "port=8080-8090/tcp immediate=yes permanent=yes state=enabled" -i ./hosts
service #指定开放或关闭的服务名称
port #指定开放或关闭的端口
masquerade #开启地址伪装
immediate #临时生效
permanent #是否添加永久生效
state #开启或是关闭
zone #指定配置某个区域
rich_rule #配置富规则
source #指定来源IP
9.常用模块练习
1.安装http服务 yum
2.编写简单网页测试内容 copy
3.启动httpd服务并加入开机自启 systemd|service
4.放行对应的firewalld防火墙端口。临时和永久生效 firewalld