首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >ansible简易版

ansible简易版

原创
作者头像
贺春旸的技术博客
发布2023-09-26 09:10:26
发布2023-09-26 09:10:26
2770
举报
文章被收录于专栏:DBA 平台和工具DBA 平台和工具

ansible-simple

ansible简易版,是我日常工作中经常使用到的批量执行命令和上传文件,参考了ansible工作模式,根据自己的情况定制了一个ansible-simple

依赖的第三方模块库

代码语言:javascript
复制
shell> pip3 install paramiko  -i "http://mirrors.aliyun.com/pypi/simple" --trusted-host "mirrors.aliyun.com"

shell> pip3 install argparse  -i "http://mirrors.aliyun.com/pypi/simple" --trusted-host "mirrors.aliyun.com"

shell> pip3 install tqdm  -i "http://mirrors.aliyun.com/pypi/simple" --trusted-host "mirrors.aliyun.com"

使用介绍:

代码语言:javascript
复制
# chmod 755 ansible-simple
# ./ansible-simple --help

usage: ansible-simple [-h] [-c] [-p ] inventory

ansible-simple简易版(默认按照CPU核数并发执行)

positional arguments:

inventory host.txt主机ip文件

optional arguments:

-h, --help show this help message and exit

-c , --cmd 输入执行的命令,多个命令用;分号分割

-p , --mput sftp上传目录文件 [本地路径] [远程路径]

1)host.txt文件格式如下:

ip:ssh端口,用户名,密码(端口不能省,必须加上)

代码语言:javascript
复制
shell> cat host.txt

192.168.137.131:22,hechunyang,123456

如果想设置连续多个ip机器列表,加一个横杠,如下:

192.168.137.131-150:22,hechunyang,123456

2)支持上传文件和目录

代码语言:javascript
复制
shell> ./ansible-simple host.txt -p '/root/soft' '/tmp/soft/'

将本地/root/soft文件夹上传至远程主机/tmp/soft/目录下

3) 执行远程主机Linux命令

代码语言:javascript
复制
shell> ./ansible-simple host.txt -c 'df -hT;date'

批量创建用户修改密码
代码语言:javascript
复制
shell> ./ansible-simple host.txt -c 'useradd hechunyang;echo "123456" | passwd --stdin hechunyang;echo "hechunyang    ALL=(ALL)NOPASSWD: ALL" >> /etc/sudoers'
注:工具适用于Centos7 系统。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ansible-simple
  • 依赖的第三方模块库
  • 1)host.txt文件格式如下:
  • 2)支持上传文件和目录
  • 3) 执行远程主机Linux命令
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档