searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

ansible在管理大数据集群中的应用

2024-09-25 09:31:50
4
0
  1. 命令举例, 打印指定主机的主机名:

ansible安装命令自行百度:

ansible all -i tmp01.hosts  -m shell -a 'echo $HOSTNAME'

tmp01.hosts的文件内容如下:

192.168.8.11
192.168.8.12

执行结果如下:

命令说明:

 all -i tmp01.hosts:命令针对tmp01.hosts文件中的所有主机。

-m:  指定ansible的模块,此处制定的是shell模块

-a:  制定要执行的命令

 

2. 查看命令的文档

比如要查看模块shell的用法,ansible-doc -s shell

 

shell模块其它命令举例:

ansible all -i tmp01.hosts  -m shell -a "cat /etc/os-release"

ansible all -i tmp01.hosts  -m shell -a "systemctl status firewalld"

 

3. 将文件批量scp到指定主机,并制定scp之后的文件权限

src是指源文件,dest是目标主机的文件地址,mode是指将scp之后的文件设置为0755权限

#copy模块
ansible all -i tmp01.hosts  -m copy -a "src=/home/op/tmp01.hosts dest=/home/op/tmp01.hosts mode=0755"

 

4.批量创建目录、删除目录

#state=directory是指创建目录
ansible all -i tmp01.hosts  -m file -a 'path=/home/op/test092401 state=directory'
#查看创建后的目录
ansible all -i tmp01.hosts  -m shell -a 'ls /home/op/'
#state=absent是指删除目录
ansible all -i tmp01.hosts  -m file -a 'path=/home/op/test092401  state=absent'
#查看目录是否被删除
ansible all -i tmp01.hosts  -m shell -a 'ls /home/op/'

 

5.远程执行脚本

#script模块
ansible all -i tmp01.hosts  -m script -a '/home/op/test/printHostname.sh'

 

6.将压缩包scp到指定主机并解压

参数copy=yes是指将压缩包scp到指定主机,如果是=no,就是认为指定主机上有压缩包

#解压缩模
ansible all -i tmp01.hosts  -m unarchive -a 'src=/home/op/test.tar.gz dest=/home/op/test0914 copy=yes'
0条评论
0 / 1000
天凉好个秋
7文章数
0粉丝数
天凉好个秋
7 文章 | 0 粉丝
原创

ansible在管理大数据集群中的应用

2024-09-25 09:31:50
4
0
  1. 命令举例, 打印指定主机的主机名:

ansible安装命令自行百度:

ansible all -i tmp01.hosts  -m shell -a 'echo $HOSTNAME'

tmp01.hosts的文件内容如下:

192.168.8.11
192.168.8.12

执行结果如下:

命令说明:

 all -i tmp01.hosts:命令针对tmp01.hosts文件中的所有主机。

-m:  指定ansible的模块,此处制定的是shell模块

-a:  制定要执行的命令

 

2. 查看命令的文档

比如要查看模块shell的用法,ansible-doc -s shell

 

shell模块其它命令举例:

ansible all -i tmp01.hosts  -m shell -a "cat /etc/os-release"

ansible all -i tmp01.hosts  -m shell -a "systemctl status firewalld"

 

3. 将文件批量scp到指定主机,并制定scp之后的文件权限

src是指源文件,dest是目标主机的文件地址,mode是指将scp之后的文件设置为0755权限

#copy模块
ansible all -i tmp01.hosts  -m copy -a "src=/home/op/tmp01.hosts dest=/home/op/tmp01.hosts mode=0755"

 

4.批量创建目录、删除目录

#state=directory是指创建目录
ansible all -i tmp01.hosts  -m file -a 'path=/home/op/test092401 state=directory'
#查看创建后的目录
ansible all -i tmp01.hosts  -m shell -a 'ls /home/op/'
#state=absent是指删除目录
ansible all -i tmp01.hosts  -m file -a 'path=/home/op/test092401  state=absent'
#查看目录是否被删除
ansible all -i tmp01.hosts  -m shell -a 'ls /home/op/'

 

5.远程执行脚本

#script模块
ansible all -i tmp01.hosts  -m script -a '/home/op/test/printHostname.sh'

 

6.将压缩包scp到指定主机并解压

参数copy=yes是指将压缩包scp到指定主机,如果是=no,就是认为指定主机上有压缩包

#解压缩模
ansible all -i tmp01.hosts  -m unarchive -a 'src=/home/op/test.tar.gz dest=/home/op/test0914 copy=yes'
文章来自个人专栏
天凉好个秋-软件开发
7 文章 | 1 订阅
0条评论
0 / 1000
请输入你的评论
0
0