文章目录
1、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)安装ansible,充当控制节点
rpm -ivh sshpass
yum install python3-jinja2 -y
yum install ansible -y
2)查看ansible版本信息
ansible --version
3 )使用setup模块验证python
ansible -m setup localhost | grep ansible_python
2、部署ansible
(1)构建ansible清单
什么是清单?
定义了ansible将要管理的一批主机
使用workstation管理servera、serverb和serverc
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)验证清单
ansible 主机名 --list-hosts
当前主机中没有受管主机
5)默认清单位置
/etc/ansible/hosts #一般不使用,而是自己创建
6)动态清单
可以从开源社区的脚本中获取
7)自定义清单
mkdir deploy-inventory #建立清单目录
(2)管理ansible配置文件
1)配置ansible
配置文件
/etc/ansible/ansible.cfg #基本配置文件,如果找不到其他配置文件,使用这个
~/.ansible.cfg #如果存在此配置并且当前工作目录中也没有ansible.cfg,此文件替代/etc/ansible/ansible.cfg
./ansible.cfg #执行ansible命令的目录中如果有ansible.cfg,就用它,不使用上面两个(推荐使用,上面两个不常用)
2)显示使用的配置文件
ansible westos -i inventory --list-hosts -v
3)管理配置文件中的设置
[defaults] #部分设置ansible操作的默认值
[privilege_escalation] #配置ansible如何在受管主机上执行特权升级
使用sudo进行权限下放
使用超级用户编辑文件:
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模块
ansible all -m ping
serverb.lab.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
3)使用临时命令通过模块执行任务
ansible-doc -l #列出所有模块
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.example.com且uid为1000
ansible -m user -a 'name=student uid=1000 state=present' servera.example.com
在受管主机上运行命令
ansible webservers -m command -a /usr/bin/hostname
ansible webservers -m command -a /usr/bin/hostname -o #加上-o参数,单行显示
注意:command模块允许执行远程命令,但这些命令不是shell处理,所以无法访问shell环境变量,所以不能执行重定向、传送等操作
使用shell和command的区别:
使用command模块执行临时命令
ansible localhost -m command -a 'id'
ansible localhost -m command -a 'id' -u student #通过-u选项使用student进行连接并执行id命令
执行上面两条命令时,需要在新建的目录中执行,否则结果不会变
使用copy模块
(1)首先使用student用户,因为student用户没有写权限,会失败
ansible localhost -m copy -a 'content="hello westos\n" dest=/etc/motd' -u student
2)使用特权升级
‘注意:此处使用特权升级需要先编辑/etc/sudoers.d/student文件’
student ALL=(ALL) NOPASSWD: ALL
以root身份运行
ansible localhost -m copy -a 'content="hello westos\n" dest=/etc/motd' -u student --become
使用all参数一次更改servera、serverb和localhost
ansible all -m copy -a 'content="hello westos\n" dest=/etc/motd' -u student --become
可以看到localhost显示SUCCESS,servera和serverb显示CHANGED,因为localhost已经处于正确状态
查看
ansible all -m command -a 'cat /etc/motd' -u student