什么是ansible
ansible是一个自动化的配置管理工具。
ansible可以完成哪些功能
- 批量执行远程命令,可以对n多台主机进行命令的执行
- 批量配置软件服务,可以进行自动化的方式配置和管理服务
- 实现软件开发功能,jumpserver底层使用ansible来实现的自动化管理
- 编排高级的IT任务,ansible的playbook是一门编程语言,可以用来描绘一套IT架构
ansible特点
- 容易学习,无代理模式,不像saltstack既要学客户端与服务端,还要学习客户端与服务端中间通讯协议。
- 操作灵活,体现在ansible有较多的模块,提供了丰富的功能、playbook则提供类似于编程语言的复杂功能
- 简单易用,体现在ansible一个命令可以完成很多事情
- 安全可靠,因为ansible使用了ssh协议进行通讯,即稳定又安全
- 移植性高、可以将写好的playbook拷贝至任意机器进行执行
ansible架构
安装
yum install ansible -y5.Ansible安装
yum install ansible #2.9版本
简单使用
基于密钥:
1.定义主机清单
[root@manager ~]# cat /etc/ansible/hosts
[webservers]
172.16.1.7
172.16.1.8
2.推送 公钥给各个节点
3.执行ansible ad-hoc 测试 是否能与该清单定义的节点通讯
[root@manager ~]# ansible webservers -m ping
基于密码
1.定义主机清单
[root@manager ~]# cat /etc/ansible/hosts
[webservers]
172.16.1.7 ansuble_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
172.16.1.8 ansuble_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
2.执行ansible ad-hoc 测试 是否能与该清单定义的节点通讯
[root@manager ~]# ansible webservers -m ping
使用主机名识别主机
[root@db ~]# cat /etc/ansible/hosts
[webservers]
web01 ansible_ssh_host=172.16.1.7 ansuble_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
web02 ansible_ssh_host=172.16.1.8 ansuble_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456
2.执行ansible ad-hoc 测试 是否能与该清单定义的节点通讯
[root@manager ~]# ansible webservers -m ping
移除指纹认证
修改ansible.cfg文件中的host_key_check的注释删除
# uncomment this to disable SSH key host checking
host_key_checking = False
ansible配置文件
配置文件查找的顺序
1.$ANSIBLE_CONFIG
2.当前目录下的ansible.cfg
3.当前用户家目录下的.ansible.cfg
4.查找/etc/ansible/ansible.cfg
ad-hoc模块:
0.命令 command | shell
1.安装 yum | yum_repository
2.配置 copy | file
3.启动 systemd | service
4.挂载 mount
5.定时 cron
6.用户 user | group
7.防火墙 selinux | firewalld
yum模块使用示例
#示例一、安装当前最新的Apache软件,如果存在则不安装
# ansible webservers -m yum -a "name=httpd state=present"
#示例二、安装当前最新的Apache软件,通过epel仓库安装
# ansible webservers -m yum -a "name=httpd state=present enablerepo=epel"
#示例三、通过公网URL安装rpm软件
# ansible webservers -m yum -a "name=https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.0-1.el7.x86_64.rpm state=present"
#示例四、安装最新版本的Apache软件,如果存在则更新Apache
# ansible webservers -m yum -a "name=httpd state=latest"
#示例五、更新所有的软件包,但排除和kernel相关的
# ansible webservers -m yum -a "name=* state=latest exclude=kernel"
#示例六、删除Apache软件
# ansible webservers -m yum -a "name=httpd state=absent"
copy模块使用示例
copy模块:文件配置
copy: 拷贝当前目录下的文件到被控端指定的路径
src 要拷贝的文件(相对路径 绝对路径)
dest 拷贝到目标主机的哪个路径下
owner 设定文件的属主
group 设定文件的数组
mode 设定文件的权限
backup 备份(只有有变化时候会做备份)
content 往目标主机文件中增加内容(重定向)
[root@manager ansible_adhoc]# ansible webservers -m copy -a "src=./exports.j2 dest=/etc/exports owner=root group=root mode=644"
[root@manager ansible_adhoc]# ansible webservers -m copy -a "src=./exports.j2 dest=/etc/exports owner=root group=root mode=644 backup=yes"
[root@manager ansible_adhoc]# #ansible webservers -m copy -a "content="http-test" dest=/tmp/1.txt"
user | group模块
group:
state: present、absent
gid:指定gid
[root@manager ansible_adhoc]# ansible webservers -m group -a "name=www gid=666 state=present"
user:
name: 用户名称
uid: uid
group:指定组
groups:指定附加组 append = yes
shell: 指定登陆shell
create_home: 创建用户家目录
state
present 创建
absent 删除
remove:移除用户相关的文件
示例一、创建test用户、uid为555
[root@manager ansible_adhoc]# ansible webservers -m user -a "name=test uid=555"
示例二、移除test用户,同时移除家目录
[root@manager ansible_adhoc]# ansible webservers -m user -a "name=test uid=555 state=absent remove=yes"
#示例三、创建jsm用户,为其添加123作为登录密码,并且创建家目录
# ansible localhost -m debug -a "msg={{ '123' | password_hash('sha512', 'salt') }}"
# ansible webservers -m user -a 'name=jsm password="$6$salt$jkHSO0tOjmLW0S1NFlw5veSIDRAVsiQQMTrkOKy4xdCCLPNIsHhZkIRlzfzIvKyXeGdOfCBoW1wJZPLyQ9Qx/1" create_home=yes'
file模块(创建目录、授权)
path:指定被控端的路径
state:
touch
directory
link
owner:属主 默认root
group:属组 默认root
mode:文件默认644、目录755
recurse:递归授权
创建目录,/data 权限为755 属主www 属组www
[root@manager ~]# ansible webservers -m file -a "path=/data state=directory owner=www group=www mode=755 recurse=yes"
创建文件,/data/test 权限644 属主www 属组www
[root@manager ~]# ansible webservers -m file -a "path=/data/test state=touch owner=www group=www mode=644"
systemd | service模块
mount模块
客户端测试mount
path: 被控端要挂载的目录 /data
src: 设备| nfs|磁盘| 光盘 /dev/sda1
fstype:
iso9660 光盘
nfs
xfs
opts:
ro,noauto
defaults
state:
mounted:挂载设备,并加入开机自启动 ***
present:写入fstab,不挂载
absent:卸载设备,会清除/etc/fstab ***
unmounted:卸载,不会清除/etc/fstab
remounted:重新再挂载一次
ansible去控制172.16.1.31:
172.16.1.7 /data 挂载到 172.16.1.31 /opt
172.16.1.8 /data 挂载到 172.16.1.31 /mnt
[root@manager ansible_adhoc]# ansible client -m mount -a "src=172.16.1.7:/data path=/opt fstype=nfs opts=defaults state=mounted"
[root@manager ansible_adhoc]# ansible client -m mount -a "src=172.16.1.8:/data path=/mnt fstype=nfs opts=defaults state=mounted"
会清除/etc/fstab
[root@manager ansible_adhoc]# ansible client -m mount -a "path=/mnt src=172.16.1.8:/data fstype=nfs opts=defaults state=unmounted"
不会清除fstab
[root@manager ansible_adhoc]# ansible client -m mount -a "path=/opt src=172.16.1.7:/data fstype=nfs opts=defaults state=absent"