Ansible自动化运维

4 篇文章 0 订阅
4 篇文章 0 订阅

Ansible自动化运维

一.Ansible安装
1)通过yum安装ansible
[root@centos01 ~]# mount /dev/cdrom /mnt/
[root@centos01 ~]# ls /mnt
ansiblerepo ansible配置文件
[root@centos01 ~]# cd /mnt/ansiblerepo/ansiblerepo/
[root@centos01 ansiblerepo]# pwd
/mnt/ansiblerepo/ansiblerepo
[root@centos01 ~]# vim /etc/yum.repos.d/centos.repo //新建仓库文件
在这里插入图片描述
[root@centos01 ~]# yum -y install ansible
验证安装结果
在这里插入图片描述
2)创建ssh免交互登录
[root@centos01 ~]# ssh-keygen -t rsa //生成密钥对
[root@centos01 ~]# ssh-copy-id root@192.168.100.20 //复制公钥到远端
[root@centos01 ~]# ssh-copy-id root@192.168.100.30
在这里插入图片描述
二.Ansible配置
[root@centos01 ~]# vim /etc/ansible/hosts
在这里插入图片描述
1)客户端配置文件列表测试
[root@centos01 ~]# ansible -i /etc/ansible/hosts web -m ping
2)加载ping模块测试
[root@centos01 ~]# ansible web -m ping
在这里插入图片描述
三.Ansible命令
1、ansible的应用场景和选项
1)ansible应用场景
非固化操作
临时性批量操作
二次开发接口调用
2) ansible的选项

	-v:显示详细信息
	-i:加载客户端配置文件
	-f:指定工作线程,默认5个
	-a:指定模块参数
	-m:指定模块 
	-M:指定自定义模块
	--host-list:指定计算机列表
	--private-key:指定密钥 

3)检查所有主机是否存活
[root@centos01 ~]# ansible all -f 5 -m ping
4)列出web组所有主机列表
[root@centos01 ~]# ansible web --list
5)批量显示web组磁盘使用空间
[root@centos01 ~]# ansible web -m command -a “df -hT”
6)使用ansible交互式工具
[root@centos01 ~]# ansible-console
切换组
root@all (2)[f:5]$ cd web
列出客户端
root@web (2)[f:5]$ list
在这里插入图片描述
四.Ansible模块

shell

1)shell模块作用
远程执行Linux命令
支持重定向和管道符
2)使用shell模块查看sshd服务状态
[root@centos01 ~]# ansible web -m shell -a ‘netstat -anptu | grep sshd’
在这里插入图片描述
3)重定向操作
[root@centos01 ~]# ansible web -m shell -a ‘echo “centos02 192.168.100.20” >> /etc/hosts’
在这里插入图片描述
command

1)command的作用
远程指定Linux命令
不支持管道符重定向
2)远程切换目录
[root@centos01 ~]# ansible web -m command -a ‘chdir=/ ls ./’

copy

1)copy的作用
复制目录或者文件使用
修改复制数据目录
2)copy常见的选项
dest:目标文件或者目录
src:源文件或者目录
mode:修改目录文件权限
owner:修改所属用户信息
group:修改所属组信息
3)应用copy
[root@centos01 ~]# ansible web -m copy -a ‘src=/etc/hosts dest=/root/host01 mode=777 owner=test group=root’
在这里插入图片描述
yum模块

1)yum的作用
管理rpm程序包
自动解决依赖关系
2)常见的选项
name:指定安装程序名字
state:persent安装,latest卸载程序
enablerepo:开启源id
3)安装apache
[root@centos01 ~]# ansible web -m yum -a ‘name=httpd state=present’
在这里插入图片描述
service

1)service的作用
管理计算机服务
2)常见的管理选项
name:服务名字
state:started启动、stopped停止、restarted重新启动服务
enabled=yes|no:是否设置开机自动启动
runlevel:设置指定运行级别启动服务
3)管理apache服务
[root@centos01 ~]# ansible web -m service -a ‘name=httpd state=stopped’

user

1)user作用
批量化管理用户
2)选项
name:用户名字
state:present创建用户、absent删除用户
uid:指定用户ID
system=yes|no:是否为系统用户
group:指定基本组
groups:附加组
shell:指定用户是否可以登录系统
home:设置宿主目录
password:密码
remove=yes|no:是否删除用户宿主目录
comment:用户描述
3)创建用户
[root@centos01 ~]# ansible web -m user -a ‘name=bob system=yes shell=/bin/bash’
在这里插入图片描述在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值