前言
什么是ansible
ansible是一款开源自动化平台,是一个配置管理工具,自动化运维工具
ansible命令相当于linux命令
ansible是基于模块开发的,常用的20~30个
YAML语法是自带语法
playbook(剧本)相当于脚本
ansible roles(角色)一大堆脚本
ansible的优点
1.跨平台支持
2.人类可读自动化: ansible提供linux,Windows,unix和网络设备的无代理支持,适用于>物理、虚拟、云和容器环境
3.完美描述应用:playbook
4.轻松管理版本控制:playbook是纯文本,可视作源代码
5.支持动态清单
6.编排可与其他系统轻松集成:puppet、jenkins
7.基础架构即代码
8.减少人为错误
任务、play和playbook设计为具有幂等性,所以在运行playbook时,
如果目标主机处于正确状态,则不会进行任何更改。
安装ansible
-
在机子上安装ansible(先确定已经搭建好了epel仓库)
dnf install -y ansible
-
查看ansible版本信息
ansible --version
示例:
[root@rhel8_node2 /]# ansible --version ansible 2.9.5 config file = /etc/ansible/ansible.cfg configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3.6/site-packages/ansible executable location = /usr/bin/ansible python version = 3.6.8 (default, Jan 11 2019, 02:17:16) [GCC 8.2.1 20180905 (Red Hat 8.2.1-3)]
-
使用setup模块验证python
ansible -m setup localhost | grep ansible_python_version "ansible_python_version": "3.6.8",
部署ansible
构建ansible清单(讲解)
- 清单:它定义了ansible将要管理的一批主机
静态清单
每行一个,填写主机名或ip,如:
www.westos.org
172.25.254.250
还可以定义主机组:
[webservers]
server1.westos.org
server2.westos.org
172.25.0.1
[dbservers]
node1.westos.org
node1.westos.org
‘注意:一台主机可以存在于多个主机组’
定义嵌套组
ansible主机清单可以包含多个主机组构成的组,如:
[webservers]
server1.westos.org
server2.westos.org
172.25.0.1
[dbservers]
node1.westos.org
node1.westos.org
[servers:children]
webservers
dbservers
通过范围简化主机规格
可以指定主机名称或ip范围或者数字和字母范围
语法:[START:END]
172.25.[0:4].[0:254] (匹配172.25.0.0/24,172.25.1.0/24 …)
server[01:10].example.com (匹配server01.example.com到server20.example.com>所有主机,此方式不匹配server1,之匹配server01)
[a:c].example.com (匹配a.example.com到c.example.com)
-
验证清单
[root@workstation ~]# ansible workstation.lab.example.com --list-hosts
‘当前主机中没有受管主机’ -
默认清单位置:/etc/ansible/hosts(一般不使用,而是自己新建)
-
动态清单–> 可以从开源社区的脚本中获取
练习
配置默认清单:
[root@workstation ~]# vim /etc/ansible/hosts
servera.lab.example.com
[webservers]
serverb.lab.example.com
列出清单中所有受管主机:
[root@workstation ~]# ansible all --list-hosts
列出不属于某个组的主机:
[root@workstation ~]# ansible ungrouped --list-hosts
列出属于webservers组的主机
[root@workstation ~]# ansible webservers --list-hosts
自定义清单
mkdir deploy-inventory #建立清单目录
列出主机
[root@workstation deploy-inventory]# ansible all -i inventory --list-hosts
列出未入组主机
[root@workstation deploy-inventory]# ansible ungrouped -i inventory --list-hosts
列出www组中的主机
[root@workstation deploy-inventory]# ansible www -i inventory --list-hosts
westos组包含www组和bbs组
[root@workstation deploy-inventory]# ansible westos -i inventory --list-hosts
管理ansible配置文件
-
配置ansible
配置文件:- /etc/ansible/ansible.cfg (基本配置文件,如果找不到其他配置文件,>使用这个)
- ~/.ansible.cfg(如果存在此配置并且当前工作目录中也没有ansible.cfg,此文件替代/etc/ansible/ansible.cfg)
- ./ansible.cfg(执行ansible命令的目录中如果有ansible.cfg,就用它,不使用上面两个(推荐使用,上面两个不常用))
显示使用的配置文件:
[root@rhel8_node1 deploy-adhoc]# ansible web -i inventory --list-host -v
Using /root/.ssh/deploy-adhoc/ansible.cfg as config file
hosts (2):
rhel7_node1.westos.com
rhel7_node2.westos.com -
管理配置文件中的设置:
[defaults] (部分设置ansible操作的默认值)
[privilege_escalation] (配置ansible如何在受管主机上执行特权升级)例:
[root@workstation 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@workstation ~]# vim /etc/sudoers.d/student
student ALL=(ALL) NOPASSWD: ALL
注意: 需要在每一个需要用户下放权限的主机都进行权力下放
运行临时命令
使用临时命令可以快速测试和更改,无需编写playbook
-
格式:
ansible host-pattern -m module [-a ‘module arguments’] [-i inventory] -
检查能否在受管主机上运行python模块
[root@workstation ~]# ansible all -m ping
serverb.lab.example.com | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: false,
“ping”: “pong”
}
在运行过程可能会出现一些问题,常见问题的解决办法 -
使用临时命令通过模块执行任务
列出所有模块:[root@workstation ~]# ansible-doc -l
查看ping模块帮助文档:
[root@workstation ~]# ansible-doc 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.lab.example.com且uid为1000[root@workstation ~]# ansible -m user -a 'name=student uid=1000 state=present' servera.lab.example.com
在受管主机上运行命令:
[root@workstation ~]# ansible webservers -m command -a /usr/bin/hostname serverb.lab.example.com | CHANGED | rc=0 >> #状态报告,显示主机名称和操作结>果 serverb.lab.example.com #命令输出
使结果显示在一行:
[root@workstation ~]# ansible webservers -m command -a /usr/bin/hostname -o #加上-o参数,单行显示 serverb.lab.example.com | CHANGED | rc=0 | (stdout) serverb.lab.example.com
注意: command模块允许执行远程命令,但这些命令不是shell处理,所以无法访问shell环境变量,所以不能执行重定向、传送等操作
-
使用shell和command的区别
shell:[root@workstation ~]# ansible localhost -m command -a set localhost | FAILED | rc=2 >> [Errno 2] No such file or directory: 'set': 'set'
command:
[root@workstation ~]# ansible localhost -m shell -a set localhost | CHANGED | rc=0 >> BASH=/bin/sh ...
-
使用command模块执行临时命令
[root@workstation deploy-adhoc]# ansible localhost -m command -a 'id' [root@workstation deploy-adhoc]# ansible localhost -m command -a 'id' -u student #通过-u选项使用student进行连接并执行id命令
注意: 执行上面两条命令时,需要在新建的目录中执行,否则结果不会变
-
使用copy模块
1.首先使用student用户,因为student用户没有写权限,会失败
[root@workstation deploy-adhoc]# ansible localhost -m copy -a ‘content=“westos ansible\n” dest=/etc/motd’ -u westos
(使用copy模块,以westos用户登录,使用root权限,把“westos ansible” 复制给本台主机的/etc/motd文件,–become给westos用户提升权限)
2.使用特权升级
‘注意:此处使用特权升级需要先编辑/etc/sudoers.d/student文件’
[root@workstation deploy-adhoc]# cat /etc/sudoers.d/student
student ALL=(ALL) NOPASSWD: ALL
以root身份运行
[root@workstation deploy-adhoc]# ansible localhost -m copy -a ‘content=“westos ansible\n” dest=/etc/motd’ -u student --become
查看是否更改
[root@workstation deploy-adhoc]# cat /etc/motd
westos ansible
使用all参数一次更改servera和localhost
[root@workstation deploy-adhoc]# ansible all -m copy -a ‘content=“westos ansible\n” dest=/etc/motd’ -u student --become
可以看到localhost显示SUCCESS,servera显示CHANGED,因为localhost已经处于正确状态
查看
[root@workstation deploy-adhoc]# ansible all -m command -a ‘cat /etc/motd’ -u student
后记
练习and记忆