############## ansible简介 #############################
#####1.什么是ansible#################
ansible是一款开源自动化平台,是一个配置管理工具,自动化运维工具
#####2.ansible优点###################
1.跨平台支持
2.人类可读自动化: ansible提供linux,Windows,unix和网络设备的无代理支持,适用于物理、虚拟、云和容器环境
3.完美描述应用:playbook
4.轻松管理版本控制:playbook是纯文本,可视作源代码
5.支持动态清单
6.编排可与其他系统轻松集成:puppet、jenkins
7.基础架构即代码
8.减少人为错误
#####3.安装##########################
1.安装
[root@localhost Desktop]# yum install ansible-2.8.0-1.el8ae.noarch.rpm
2.查看
[root@localhost Desktop]# ansible --version
3.使用setup模块验证python
[root@localhost Desktop]# ansible -m setup localhost | grep ansible_python
########### 部署ansible ##############
#####1.构建ansible清单############
清单:定义了ansible将要管理的一批主机
1.静态清单:
每行一个,填写主机名或ip,如:
还可以定义主机组:
注意:一台主机可以存在于多个主机组
2.定义嵌套组:
ansible主机清单可以包含多个主机组构成的组,如:
3.通过范围简化主机规格
可以指定主机名称或ip范围或者数字和字母范围
语法:[START:END]
192.168.[0:4].[0:250] ##192.168.0.0/24-192.168.4.250/24
server[01:10].example.com ##匹配server01.example.com到server20.example.com所有主机
注意:此方式不匹配server1,只匹配server01
[a:c].example.com ##匹配a.example.com到c.example.com
4.验证清单:
[root@localhost Desktop]# ansible localhost.localdomain --list-hosts ##查看主机中受管理的主机(没有)
5.默认清单位置:
[root@localhost Desktop]# cd /etc/ansible/
[root@localhost ansible]# vim hosts
[root@localhost ansible]# ansible all --list-hosts ##列出所有受管主机
[root@localhost ansible]# ansible webservers --list-hosts ##列出属于webservers组的主机
[root@localhost ansible]# ansible ungrouped --list-hosts ##列出不属于某个组的主机
自定义清单:
[root@localhost ~]# mkdir deploy-inventory ##建立目录
[root@localhost deploy-inventory]# vim inventory ##编辑清单
[root@localhost deploy-inventory]# ansible all -i inventory --list-hosts
[root@localhost deploy-inventory]# ansible ungrouped -i inventory --list-hosts
[root@localhost deploy-inventory]# ansible webservers -i inventory --list-hosts
[root@localhost deploy-inventory]# vim inventory
[root@localhost deploy-inventory]# ansible westos -i inventory --list-hosts ##westos组包含webservers组和dbservers组
#####2.管理ansible配置文件##################
1.配置ansible:
[root@localhost deploy-inventory]# vim /etc/ansible/ansible.cfg ##基本配置文件,找不到其他配置文件,使用这个
[root@localhost deploy-inventory]# ls ~/.ansible.cfg ##如果存在此配置并且当前工作目录中也没有ansible.cfg,此文件替代/etc/ansible/ansible.cfg
[root@localhost deploy-inventory]# vim ansible.cfg ##行ansible命令的目录中如果有ansible.cfg,就用它,不使用上面两个
如:
[root@localhost deploy-inventory]# ansible --version
[root@localhost deploy-inventory]# ansible westos -i inventory --list-hosts -v ##显示使用的配置文件
配置文件中的设置:
[root@localhost deploy-inventory]# vim /etc/ansible/ansible.cfg
[defaults] ##部分设置ansible操作的默认值
[privilege_escalation] ##配置ansible如何在受管主机上执行特权升级
例如:
[root@localhost deploy-inventory]# vim ansible.cfg
[defaults]
inventory = ./inventory ##当前文件
remote_user = student ##指定登录受管主机的用户,如不指定则使用当前用户名称
ask_pass = false ##是否提示输入ssh密码,做了免密就可以设置为false,否则需为true
[privilege_escalation]
become=True ##连接到受管主机上是否进行身份切换
become_method=sudo ##切换方式,默认为sudo
become_user=root ##切换到的用户
become_ask_pass=False ##是否需要为become_method提示输入密码,默认为false
使用sudo进行权限下放
使用超级用户编辑文件:
[root@localhost deploy-inventory]# vim /etc/sudoers.d/stuent
#####3.运行临时命令##################
1.格式:
ansible host-pattern -m module [-a 'module arguments'] [-i inventory]
2.检查能否在受管主机上运行python模块
[root@localhost deploy-inventory]# ansible all -m ping
3.使用临时命令通过模块执行任务
[root@localhost deploy-inventory]# ansible-doc -l ##列出所有模块
[root@localhost deploy-inventory]# ansible-doc ping ##查看ping模块帮助文档
ansible模块:
文件模块:
-copy:将本地文件复制到受管主机
-file:设置文件的权限和其他属性
-lineinfile:确保特定行是否在文件中
-synchronize:使用rsync同步内容
系统模块:
-firewalld:使用firewalld管理任意端口和服务
-reboot:重启
-service:管理服务
-user:添加、删除和管理用户账户
Net Tools模块:
-get_url:通过http、https、或者ftp下载文件
-nmcli:管理网络
-uri:与web服务交互
例子:
使用user模块确保student用户存在于servera.localdomain且uid为1001
[root@localhost ~]# ansible -m user -a 'name=student uid=1001 state=present' servera.localdomain
在受管主机上运行命令:
[root@localhost ~]# ansible webservers -m command -a /usr/bin/hostname
serverb.localdomain | CHANGED | rc=0 >> ##状态报告,显示主机名称和操作结果
serverb.localdomain ##命令输出
[root@localhost ~]# ansible webservers -m command -a /usr/bin/hostname -o ##单行显示
注意:
command模块允许执行远程命令,但这些命令不是shell处理,所以无法访问shell环境变量,所以不能执行重定向、传送等操作
使用shell和command的区别:
[root@localhost ~]# ansible localhost -m command -a set
[root@localhost ~]# ansible localhost -m shell -a set
使用command模块执行临时命令:
[root@localhost deploy-inventory]# ansible localhost -m command -a 'id'
注意:
只能在文件内使用
使用copy模块:
权限提升:
[root@localhost deploy-inventory]# cd /etc/sudoers.d/
[root@localhost sudoers.d]# vim stuent
以root身份运行
[root@localhost deploy-inventory]# ansible localhost -m copy -a 'content="hello westos\n" dest=/etc/motd' -u student --become
[root@localhost deploy-inventory]# cat /etc/motd