ansible自动化运维

安装ansible

首先生成密钥对

[root@centos ~]# ssh-keygen -t RSA

将密钥对发送到20和30

[root@centos ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.20

[root@centos ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.30

删除系统自带的yum源

[root@centos ~]# rm -rf /etc/yum.repos.d/C*

挂载ansible光盘

[root@centos ~]# mount /dev/cdrom /mnt

修改yum源将挂载点修改为/mnt/ansiblerepo/ansiblerepo

[root@centos yum.repos.d]# vim /etc/yum.repos.d/local.repo

保存退出之后删除yum缓存

[root@centos ~]# yum clean all

然后用yum安装ansible

[root@centos ~]# yum -y install ansible

配置ansbile的客户端

[root@centos01 ~]# vim /etc/ansible/hosts

测试客户端的连接性

客户端配置文件列表测试 :

[root@centos ~]# ansible -i /etc/ansible/hosts web -m ping

加载ping模块测试 :

[root@centos ~]# ansible web -m ping

ansible的应用

1、ansible的应用场景和选项

1)ansible应用场景 非固化操作 临时性批量操作 二次开发接口调用

2)ansible的选项

-v:显示详细信息

-i:加载客户端配置文件

-f:指定工作线程,默认5个

-a:指定模块参数

-m:指定模块

-M:指定自定义模块

--host-list:指定计算机列表

--private-key:指定密钥

shell模块的作用:

1.远程执行Linux命令

2.支持重定向和管道符   

使用shell模块查看sshd服务状态

command的作用 :

远程指定Linux命令 不支持管道符重定向

远程切换目录 : [root@centos ~]# ansible web -m command -a 'chdir=/ ls ./'

copy的作用:    复制目录或者文件使用     修改复制数据目录

copy常见的选项

dest:目标文件或者目录

src:源文件或者目录

mode:修改目录文件权限

owner:修改所属用户信息

group:修改所属组信息

yum模块 

yum的作用:

管理rpm程序包

自动解决依赖关系

常见的选项:

name:指定安装程序名字

state:persent安装

latest卸载程序

enablerepo:开启源id

安装apache : [root@centos01 ~]# ansible web -m yum -a 'name=httpd state=present'

service 模块

service的作用:

管理计算机服务

常见的管理选项:

name:服务名字

state:started启动、stopped停止、

restarted重新启动服务

enabled=yes|no:是否设置开机自动启动

runlevel:设置指定运行级别启动服务

user模块

user作用 :

批量化管理用户

选项

name:用户名字

state:present创建用户、absent删除用户

uid:指定用户ID

system=yes|no:是否为系统用户

group:指定基本组

groups:附加组

shell:指定用户是否可以登录系统

home:设置宿主目录

password:密码

remove=yes|no:是否删除用户宿主目录

comment:用户描述

创建用户:

[root@centos ~]# ansible web -m user -a 'name=bob system=yes shell=/bin/bash'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

过客不予秋风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值