hebei
如上面的示例中,我指定了杭州组我有host1、hosts2;嘉兴组我有host3、host4主机;我又指定了一个组浙江组,同时包含杭州和嘉兴;同时为该组内的所有主机指定了四个vars变量。后面我又设定了一个组中国组,包含浙江、河南、山东、河北。
注:由于vars变量在ansible ad-hoc部分中基本用不到,主要用在ansible-playbook中,后面的章节部分也会提到。 五、ansible常用功能 更多更详细的话参考:http://www.361way.com/ansible-modules/4415.html 1、ping模块:看到ping字段为pong即为通,该功能主要用于批量检测连通性
ansible -m ping test 2、执行shell命令的command默认模块,用于远程执行命令,不支持变量,例子:
#查看当前用户
ansible -m command -a 'w' test
#查看日期
ansible -m command -a 'date' test
#给节点增加用户
ansible -m command -a 'useradd mark' test
ansible -m command -a 'grep mark /etc/passwd' test
#重定向输出到文件
ansible -m command -a 'df -Th' test > /root/test.log 3、cron定时任务,例子:
ansible test -m cron -a 'minute="*/1" job="/bin/echo "hell"" name="test job" '
#查看是否成功,另外注意:这里既然生成了,就立即生效,不用重启crond进程
ansible test -m command -a 'crontab -l' 4、user管理,例子:
ansible test -m user -a 'name=mark'
#查看是否成功,现在不明白和command useradd添加有什么区别,但是既然有用户管理还是用它好了
ansible -m command -a 'grep mark /etc/passwd' test 5、远程复制模块copy,例子:
#如果要想远程复制文件成功,必须给所有客户端安装这个。。。不知道为什么,如果不装复制失败报错
ansible -m command -a 'yum install libselinux-python -y' 'test
ansible -m copy -a 'src=/root/VMwareTools-9.4.10-2068191.tar.gz dest=/root owner=root mode=640' 'test'
#往远程主机文件写入内容:使用content代替src
ansible -m copy -a 'content="aaabbb\n" dest=/root/test.log' test 6、设置文件属性模块file,例子:
#当然group\mode\owner不写也行
ansible test -m file -a 'owner=mark group=root mode=644 path=/root/test.log'
#创建文件符号链接
ansible test -m file -a 'path=/root/test.1 src=/root/test.log state=link' 7、service指定服务状态模块管理(须保证远程主机已安装好该服务),例子:
#enableed:是否开机自动启动 true/false
#name:服务名称
#start:状态,取值为started,stoped,restarted
ansible test -m service -a 'enabled=true name=httpd state=started' 8、shell模块: 在远程主机上运行命令,支持管道、变量等在使用复制命令时使用,例子:
#该功能好用,可以批量更改客户端用户密码
ansible test -m shell -a 'echo password |passwd –stdin mark' 9、script将本地脚步复制到远程主机上并运行(注意本地主机脚步路径使用相对路劲),例子:
ansible test -m script -a "test.sh" 10、yum程序安装,例子
#name指定要安装的程序包,可以带上版本号
#state:preset,laster表示安装最新,absent卸载
ansible test -m yum -a 'name=lrzsz' 11、setup收集远程主机的facts,例子:
#查看所有
ansible test -m setup
#查看IP配置
ansible test -m setup -a 'filter=ansible_default_ipv4'
#查看内存信息
ansible test -m setup -a 'filter=ansible_memory_mb'
#查看主机名
ansible test -m setup -a 'filter=ansible_nodename'
每个被管理节点在接受并运行管理命令之前,会将自己主机相关信息,操作系统版本、ip地址等会报告给ansible主机用于将状态报告给ansible主机直接调用其变量。 12、其他
#查看模块帮助命令
ansible-doc -l