| Ansible自动化管理工具-常用命令汇总1 定义主机清单
[root@server1 ~]
	[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
/*
	参数说明:
		-i:指定host文件的路径,默认路径为/etc/ansible/hosts(也可以不适用-i参数进行指定,默认使用/etc/ansible/hosts文件)
*/
[root@server1 ~]
[root@server1 ~]
/*
	参数说明:
		command模块:ansible的默认模块,执行shell命令,可以执行权限范围内的所有shell命令
*/
[root@server1 ~]
[root@server1 ~]
[root@server1 ~]
[root@server1 ~]
[root@server1 ~]
[root@server1 ~]
 2 ansible常见模块的高级使用方法2.1 三个远程命令模块的区别/*
	command,shell,script三个远程命令模块的区别:
		command模块:为ansible的默认模块,不指定-m参数时,默认使用command模块,command模块
			只用于一些简单的命令,其命令的执行不是通过shell执行,不支持">","<","|","&"等.
		shell模块:在远程命令通过/bin/sh执行,在终端中输入的各种的命令都是可以使用的.
			eg1:ansible -m shell -a "cat /etc/passwd | grep root" hadoop
			注:但是自己定义在~/.bashrc或~/bash_profile中的环境变量,shell模块由于没有加载,
			所有无法识别,如果需要使用自定义的环境变量,就需要在最开始,执行加载自定义脚本的语句.
			对shell模块的使用分为两种情况:
				1.执行语句少,可以写在一条语句中:
					eg2:ansible -m shell -a "soucre ~/.bash_profile && df -h | grep boot" hadoop
				2.如果在远程执行的语句较多时,可以写成一个脚本,通过copy模块传到远端,然后在执行,
				但这又涉及到两次ansible的调用,这时就建议使用scritpt模块. 
		script模块:在本地写一个脚本,在远程服务器上执行.
*/
案例1:使用script模块,在远程服务器上执行
[root@server1 ~]
 2.2 copy模块/*
	copy模块:实现主控端向目标主机拷贝文件,类似于scp
*/
案例1:把ansible主机上的/etc/hosts文件复制到节点机器的/tmp目录下
[root@server1 ~]
 2.3 file模块/*
	file模块:设置文件属性
*/
案例1:修改/tmp/hosts文件的权限为0777
[root@server1 ~]
[root@server1 ~]
 2.4 stat模块/*
	stat模块:获取远程节点文件的信息
*/
案例1:获取所有节点上/tmp目录下hosts文件信息
[root@server1 ~]
 2.5 get_url模块/*
	get_url模块:实现远程主机下载指定url到本地,支持sha256sum文件校验
	参数说明:
		force参数:
			如果force=yes,当下载文件时,如果所下的内容与源目录下的文件内容不一样,则替换源文件,如
			果一样,则不下载		
*/
案例1:下载epel-release-latest-7.noarch.rpm到主机清淡的/tmp目录下
[root@server1 ~]
[root@server1 ~]
 2.6 yum模块/*
	yum模块:Linux平台软件包管理
		statue状态:
			安装:latest,present,installed
			卸载:removed,absent
*/
案例1:安装httpd软件
[root@server1 ~]
[root@server1 ~]
 2.7 cron模块/*
	cron模块:远程主机crontab配置
*/
案例1:每30分钟执行ls /tmp
[root@server1 ~]
[root@server1 ~]
 2.8 service模块/*
	service模块:远程主机系统服务管理
		常用参数:
			1.name参数:需要管理的服务名称
			2.state参数:用于指定服务的状态,
				started:启动远程服务
				stopped:停止远程主机的服务
				restarted:重启服务
				reloaded:重新加载服务
			3.enabled参数:
				yes:开机自启动
				no:开机不自启动
		注:若想使用service模块启动服务,被启动的服务,必须可以使用service命令启动或者关闭服务.
*/
案例1:远程启动apache服务
[root@server1 ~]
[root@server1 ~]
 2.9 sysctl模块/*
	sysctl模块:远程主机sysctl配置
*/
案例1:开启路由转发功能
[root@server1 ~]
[root@server1 ~]
 2.10 user模块/*
	user模块:远程主机管理
*/
[root@server1 ~]
[root@server1 ~]
 |