linux cpu占用率如何看
252
2022-10-14
自动化运维之Ansible剑客参上
自动化运维之Ansible
Asible概述与核心组件
Ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。 Ansible可以看作是基于模块进行工作的框架结构,批量部署能力就是由Ansible所运行的模块实现的。简而言之Ansible是基于“模块”完成各种“任务”的。Ansible基本构架由六个部分组成: (1)、连接插件connection plugins:负责和被监控端实现通信;(2)、host inventory主机清单:指定操作的主机,是一个配置文件里面定义监控的主机;(3)、Core modules核心模块:是Ansible自带的模块,使用致谢模块将资源分发到被管理主机,使其执行特定任务或匹配特定的状态;(4)、Custom modules自定义模块:用于完全模块功能的补成,可借助相关插件完成记录日志、发送邮件等功能。(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。(6)、Asible core核心引擎。
安装部署Ansible服务
Ansible自动化运维环境由控制主机与被管理主机组成,由于Ansible是基于SSH协议进行通信的,所以控制主机安装Ansible软件后不需要重启或进行任何程序,被管理主机也不需要安装和运行任何代理程序。
1、Ansible实验案例环境
角色 | IP地址 | 组名 |
---|---|---|
控制主机 | 192.168.190.130 | |
被管理主机 | 192.168.190.128 | test01 |
被管理主机 | 1982.168.190.131 | test02 |
2、安装配置Ansible
yum install -y epel-release #安装epel源 yum install ansible -y ansible --version #查看ansible版本 yum install tree -y tree /etc/ansible/ #树状结构展示文件夹
/etc/ansible/ ├── ansible.cfg #ansible的配置文件 ├── hosts #ansible的主仓库,用于存储需要管理的远程主机的相关信息 └── roles #角色
cd /etc/ansible vim hosts #配置主机清单
[test01] 192.168.190.128 [test02] 192.168.190.131
ssh-keygen -t rsa #基于SSH秘钥的连接 ssh-copy-id root@192.168.190.128 #配置秘钥对验证 ssh-copy-id root@192.168.190.131 ssh-agent bash #免交互代理 ssh-add
Ansible命令基础
Ansible的命令行管理工具都是由一系列模块、参数所支持的,可以在命令后面加上-h或--help获得帮助。如使用ansible-doc工具可以通过ansible-doc -h 或ansible-doc --help查看其帮助信息。 ansible-doc是用来查看模块帮助信息的工具,主要的选项-l用来列出可使用的模块,-s用来列出某个模块的描述信息和使用示例。
1、command模块
Asible管理工具使用-m选项来指定使用模块,默认使用command模块,即-m选项省略是会运行此模块,用于在被管理主机上运行。
例:在被管理的主机上执行data命令,显示被管理主机的时间,有三种执行命令的方式去管理写入主机清单中的主机: (1)、指定ip执行date ansible 192.168.192.128 -m command -a 'date' (2)、指定分类执行date ansible test01 -m command -a 'date' ansible test02 -m command -a 'date' (3)、所有hosts主机执行date命令 ansible all -m command -a 'date' (4)、如果不加-m模块,则默认运行command模块 ansible all -a 'ls /'
2、cron模块
Ansible中的cron模块用于定义任务计划。其中有两种状态(state):present表示添加(省略状态时默认使用),absent表示移除。
ansible-doc -s cron #查看cron模块信息 ansible test01 -m cron -a 'minute="*/1" job="/bin/echo heihei" name="test cron job"' #添加任务计划 ansible test01 -a 'crontab -l' #查看任务 ansible test01 -m cron -a 'name="test cron job" state=absent' #移除计划任务,假如该计划任务没有取名字,name=None即可可
3、user模块
Ansible中的user模块用于创建新用户和更改、删除已存在的用户。其中name选项用来指明创建用户的名称。
ansible test01 -m user -a 'name="test001"' #创建用户test001 ansible test01 -m command -a 'tail /etc/passwd' #查看添加的用户 ansible test01 -m user -a 'name="test01" state=absent' #删除用户test001
4、group模块
Ansible中的group模块用于对用户组进行管理。
nsible test01 -m group -a 'name=mysql gid=306 system=yes' #创建mysql组 ansible test01 -a 'tail /etc/group' #查看组 ansible test01 -m user -a 'name=test001 uid=306 system=yes group=mysql' #创建test001用户添加到mysql组中 ansible test01 -a 'tail /etc/passwd' #查看用户 ansible test01 -a 'id test001' #查看用户的属组
5、copy模块
Ansible中的copy模块用于实现文件复制和批量下发文件。其中src来定义本地源文件路径,使用dest定义被管理主机文件路径,使用content则同伙指定信息内容来生成目标文件。
(1)、例:将本地文件/etc/fstab复制到被管理主机的/opt/fstab.back,将所有者设置为root,权限设置为640; ansible test001 -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640' ansible test001 -a 'ls -l /opt' #查看详细信息 ansible test001 -a 'cat /opt/fstab.back' #查看内容 (2)、将“hello heihei!”写入到/opt/fstab.back文件中; ansible test001 -m copy -a 'content="hello heihei!" dest=/opt/fstab.back' #将hello heihei!写入/opt/fstab.back ansible test001 -a 'cat /opt/fstab.back' #查看内容
6、file模块
在Ansible中使用file模块来设置文件属性。其中使用path指定文件路径,使用src定义源文件路径,使用name或dest来替换创建文件的符号链接。
ansible test01 -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back' #设置文件/opt/fstab.back的属主为mysql,属组为mysql,权限为644 ansible test01 -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link' #设置/opt/fstab.link为/opt/fstab.back的链接文件 ansible test01 -m file -a "path=/opt/fstab.back state=absent" #删除/opt/fstab.back文件
7、ping模块
在Ansible中使用ping模块来指定主机的连通性。
ansible all -m ping
8、service模块
在Ansible中使用service模块来控制管理服务的运行状态。其中,使用enable表示是否开机自动启动,取值为true或false;使用name定义服务名称;使用state指定服务状态,取值分别为started、stopped、restarted。
ansible test02 -a 'systemctl status #查看test02服务器httpd运行状态 ansible test02 -m service -a 'enabled=true name=state=started' #启动mysql -m shell -a 'echo abc123|passwd --stdin mysql' #创建用户使用无交互模式给用户设置密码
10、script模块
Ansible中的script模块可以将本地脚本复制到被管理主机上进行运行。需要注意的是,使用相对路径来指定脚本。
例:编写一个本地脚本test.sh,复制到被管理主机上运行 vim test.sh #编写脚本 #!/bin/bash echo "hello ansible from script"> /opt/script.txt chmod +x test.sh #赋予执行权限 ansible test01 -m script -a 'test.sh' #将脚本复制到test01上 cat /opt/script.txt #到test01上查看
11、yum模块
Ansible中的yum模块负责在被管理主机上安装或卸载软件包,但是需要提前在每个节点配置自己的yum仓库。其中使用name指定要安装的软件包,还需要带上软件包的版本号,否则安装最新的软件包;使用state指定安装软件包的状态,present、latest用来表示安装,absent表示卸载。
ansible test01 -m yum -a 'name=zsh' #安装zsh软件包 ansible test01 -m yum -a 'name=zsh state=absent' #卸载zsh软件包
12、setup模块
在Ansible中使用setup模块收集、查看被管理主机的facts(facts是Ansible采集被管理主机设备信息的一个功能)。每个被管理主机在接收并运行管理命令之前,都会将自己的相关信息(操作系统版本、IP地址等)发送给控制主机。
ansible tets01 -m setup //获取test01组主机的facts信息
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~