栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Ansible自动化管理工具

Ansible自动化管理工具

Ansible自动化管理工具-常用命令汇总 1 定义主机清单
#配置ansible的主机清单文件
[root@server1 ~]# vim /etc/ansible/hosts
	[hadoop]	#主机清单的组
	192.168.71.10 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=root
	192.168.71.20 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=root
	192.168.71.30 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=root
#使用ping模块进行连通性测试

[root@server1 ~]# ansible -i /etc/ansible/hosts hadoop -m ping
[root@server1 ~]# ansible hadoop -m ping 


#案例1:检查ansible节点的运行时间
[root@server1 ~]# ansible hadoop -m command -a "uptime"
# 主机清单也可以写在最后面,方便阅读命令
[root@server1 ~]# ansible -m command -a "uptime" hadoop

#案例2:检查节点的内核版本
[root@server1 ~]# ansible -m command -a "uname -r" hadoop

#案例3:给节点添加用户
[root@server1 ~]# ansible -m command -a "useradd shutterbug -s /bin/bash -d /home/shutterbug" hadoop
[root@server1 ~]# ansible -m command -a "tail -n 1 /etc/passwd" hadoop

#案例4:将df -h在所有节点执行后的结果,重定向到本机的/tmp/command_output.txt文件中
[root@server1 ~]# ansible -m command -a "df -Th" hadoop > /tmp/command_output.txt
2 ansible常见模块的高级使用方法 2.1 三个远程命令模块的区别
案例1:使用script模块,在远程服务器上执行
[root@server1 ~]# ansible -m script -a "/etc/ansible/test_sh/net.sh" hadoop
2.2 copy模块
案例1:把ansible主机上的/etc/hosts文件复制到节点机器的/tmp目录下
[root@server1 ~]# ansible -m copy -a "src=/etc/hosts dest=/tmp/ owner=root group=root mode=0755" hadoop
2.3 file模块
案例1:修改/tmp/hosts文件的权限为0777
[root@server1 ~]# ansible -m file -a "path=/tmp/hosts mode=0777" hadoop
[root@server1 ~]# ansible -m shell -a "ll /tmp/hosts" hadoop
2.4 stat模块
案例1:获取所有节点上/tmp目录下hosts文件信息
[root@server1 ~]# ansible -m stat -a "path=/tmp/hosts" hadoop
2.5 get_url模块
案例1:下载epel-release-latest-7.noarch.rpm到主机清淡的/tmp目录下
[root@server1 ~]# ansible -m get_url -a "url=https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm dest=/tmp/ force=yes" hadoop
[root@server1 ~]# ansible -m shell -a "ls -l /tmp | grep rpm" hadoop
2.6 yum模块
案例1:安装httpd软件
[root@server1 ~]# ansible -m yum -a "name=httpd state=latest" hadoop
[root@server1 ~]# ansible -m shell -a "rpm -qa | grep http" hadoop
2.7 cron模块
案例1:每30分钟执行ls /tmp
[root@server1 ~]# ansible -m cron -a "name='list dir' minute='*/30' job='ls /tmp'" hadoop
[root@server1 ~]# ansible -m shell -a "crontab -l" hadoop
2.8 service模块
案例1:远程启动apache服务
[root@server1 ~]# ansible -m service -a "name=httpd state=started" hadoop
[root@server1 ~]# ansible -m shell -a "netstat -antup | grep 80" hadoop
2.9 sysctl模块
案例1:开启路由转发功能
[root@server1 ~]# ansible -m sysctl -a "name=net.ipv4.ip_forward value=1 reload=yes" hadoop
[root@server1 ~]# ansible -m shell -a "cat /proc/sys/net/ipv4/ip_forward" hadoop
2.10 user模块
[root@server1 ~]# ansible -m user -a "name=shutterbug state=present" hadoop
[root@server1 ~]# ansible -m shell -a "id shutterbug" hadoop
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/460526.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号