自动化运维工具------ansible

自动化运维工具------ansible

一、ansible概述

1、简介

Ansible 基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优

点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身

没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。

2、特性

1)、不需要在被管控主机上安装任何客户端,更新时,只需在操作机上进行一次更新即可

2)、无服务器端,使用时直接运行命令即可

3)、基于模块工作,可使用任意语言开发模块

4)、使用yaml语言定制剧本playbook

5)、基于SSH工作

6)、可实现多级指挥

二、ansible安装

环境部署

ansible:192.168.132.60

node1: 192.168.132.40

node2:192.168.132.50

1、关闭防火墙及安全机制

systemctl stop firewalld 

setenforce 0

2、安装epel扩展源

yum -y install epel-release
yum -y install ansible

树型查询工具

yum -y install tree

tree /etc/ansible

3、配置主机清单文件

vim /etc/ansible/hosts

#配置主机清单

[webserver]
192.168.132.40
[mysql]
192.168.132.50

在这里插入图片描述

4、生成密钥对

ssh-keygen -t rsa

在这里插入图片描述

ssh-copy-id root@192.168.132.40

ssh-copy-id root@192.168.132.50

在这里插入图片描述

#每次查询都需要输入密钥
#可以使用ssh-agent代理

ssh-agent bash
ssh-add
123123

ansible webserver -m command -a 'date'

在这里插入图片描述

三、ansible命令模块

1、command模块
命令格式:ansible [主机] [-m 模块] [-a args]

#列出所有已安装的模块,按q退出
ansible-doc -l    		

#所有主机执行data命令,其中all可以换成IP或者分类名称,例:192.168.132.40 / webserver
ansible all -m command -a 'date'   

#不加-m模块,则默认使用command模块
ansible all -a 'date'
ansible all -a 'ls /root'

在这里插入图片描述

2、cron模块
两种状态(state):present表示添加(可以省略),absent表示移除

#查看cron模块信息
ansible-doc -s cron

#webserver:分类  -m指定模块 -a输出模块内的指令  分钟:每分钟,工作:输出hello,工作名称:test
ansible webserver -m cron -a 'minute="*/1" job="/usr/bin/echo hello" name="test"'

#查看计划性任务命令
ansible webserver -a 'crontab -l'

#移除计划性任务
ansible webserver -m cron -a 'name="test" state=absent'
3、user模块
#模块信息
ansible-doc -s user

#创建用户
ansible all -m user -a 'name=zhangsan'

#查看用户账户信息
ansible all -m 'command' -a 'tail -1 /etc/passwd'

#移除指令
ansible all -m user -a 'name="zhangsan" state=absent'

在这里插入图片描述

在这里插入图片描述

4、group模块
#查看模块信息
ansible-doc -s group

#system=yes 创建系统组
ansible mysql -m group -a 'name=mysql gid=1111 system=yes'

#查看组账户信息
ansible mysql -a 'tail -1 /etc/group'

#创建用户并加入组
ansible mysql -m user -a 'name=zhangsan uid=1234 group=mysql system=yes'

#查看用户test02的用户id和组id信息
ansible mysql -a 'id zhangsan'

在这里插入图片描述

5、copy模块
ansible-doc -s copy

ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back'

ansible mysql -a 'ls /opt'
ansible mysql -a 'cat /opt/fstab.back'

ansible mysql -m copy -a 'content="hello" dest=/opt/test.txt'
ansible mysql -a 'cat /opt/test.txt'

在这里插入图片描述

6、file模块
ansible-doc -s file

ansible mysql -m user -a 'name=mysql system=yes'

ansible mysql -m file -a 'owner=mysql group=mysql mode=600 path=/opt/test.txt'
ansible mysql -a 'ls -l /opt/test.txt'

#创建
#ansible mysql -m file -a 'path=/opt/abc.txt state=touch'

ansible mysql -m file -a 'src=/opt/test.txt path=/opt/test.txt.link state=link'
ansible mysql -a 'ls -l /opt'

#移除文件/opt/test.txt
ansible mysql -m file -a 'path=/opt/test.txt state=absent'
7、ping模块
ansible all -m ping 

在这里插入图片描述

8、service模块
ansible-doc -s service

#192.168.132.40执行
yum -y install httpd



ansible webserver -a 'systemctl status httpd'

ansible webserver -m service -a 'enabled=true name=httpd state=started'

systemctl status httpd
systemctl is-enabled httpd

在这里插入图片描述

在这里插入图片描述

9、shell、script模块
ansible-doc -s shell

ansible mysql -m user -a 'name=zhangsan'
ansible mysql -m shell -a 'echo 123123 | passwd --stdin zhangsan'



ansible-doc -s script

vim test.sh
#!/bin/bash
echo 'hello ansible from script' > /opt/script.txt

chmod +x test.sh
ansible all -m script -a 'test.sh'

在这里插入图片描述

在这里插入图片描述

10、setup模块

ansible-doc -s setup
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值