ansible简介
1.什么是ansible
ansible是一款开源自动化平台,是一个配置管理工具,自动化运维工具
2.ansible的优点
1.跨平台支持
2.人类可读自动化: ansible提供linux,Windows,unix和网络设备的无代理支持,适用于物理、虚拟、云和容器环境
3.完美描述应用:playbook
4.轻松管理版本控制:playbook是纯文本,可视作源代码
5.支持动态清单
6.编排可与其他系统轻松集成:puppet、jenkins
7.基础架构即代码
8.减少人为错误
任务、play和playbook设计为具有幂等性,所以在运行playbook时,
如果目标主机处于正确状态,则不会进行任何更改。
3.安装ansible
1.在workstation上安装ansible,充当控制节点
yum install -y ansible
2.查看ansible版本信息
ansible --version
3.使用setup模块验证python
ansible -m setup localhost | grep ansible_python_version
“ansible_python_version”: “3.6.8”,
部署ansible
什么是清单? --> 定义了ansible将要管理的一批主机
1.静态清单
每行一个,填写主机名或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
‘注意:一台主机可以存在于多个主机组’
2.定义嵌套组
ansible主机清单可以包含多个主机组构成的组,如:
[webservers]
server1.westos.org
server2.westos.org
172.25.0.1[dbservers]
node1.westos.org
node1.westos.org[servers:children]
webservers
dbservers
3.通过范围简化主机规格
可以指定主机名称或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
4.验证清单
[root@workstation ~]# ansible workstation.lab.example.com --list-hosts
‘当前主机中没有受管主机’
5.默认清单位置:
/etc/ansible/hosts #一般不使用,而是自己新建
6.动态清单–>
可以从开源社区的脚本中获取
######练习
[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
自定义清单
```bash
mkdir deploy-inventory #建立清单目录
列出主机
[root@workstation deploy-inventory]# ansible all -i inventory --list-hosts
[root@workstation deploy-inventory]# ansible ungrouped -i inventory --list-hosts
[root@workstation deploy-inventory]# ansible www -i inventory --list-hosts #列出www组中的主机
[root@workstation deploy-inventory]# ansible westos
-i inventory --list-hosts #westos组包含www组和bbs组
2.管理ansible配置文件
1.配置ansible
配置文件:1. /etc/ansible/ansible.cfg #基本配置文件,如果找不到其他配置文件,使用这个
2. ~/.ansible.cfg #如果存在此配置并且当前工作目录中也没有ansible.cfg,此文件替代/etc/ansible/ansible.cfg
3. ./ansible.cfg #执行ansible命令的目录中如果有ansible.cfg,就用它,不使用上面两个(推荐使用,上面两个不常用)
#显示使用的配置文件
[root@workstation deploy-inventory]# ansible www -i inventory --list-hosts -v
Using /etc/ansible/ansible.cfg as config file
hosts (1):
servera.lab.example.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
3.运行临时命令
使用临时命令可以快速测试和更改,无需编写playbook
1.格式:
ansible host-pattern -m module [-a 'module arguments'] [-i inventory]
2.检查能否在受管主机上运行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"
}
3.使用临时命令通过模块执行任务
[root@workstation ~]# ansible-doc -l #列出所有模块
[root@workstation ~]# 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服务交互
4.例:
使用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的区别:
[root@workstation ~]# ansible localhost -m command -a set
localhost | FAILED | rc=2 >>
[Errno 2] No such file or directory: ‘set’: ‘set’[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 student
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