# ssh root@192.168.31.214 'ntpdate pool.ntp.org' 5)如何查看模块帮助
ansible-doc -l
ansible-doc -s MODULE_NAME 6)ansible命令应用基础
语法: ansible <host-pattern> [-f forks]
[-m module_name] [-a args]
-f forks:启动的并发线程数;
-m module_name:要使用的模块
-a args:模块特有的参数; 7)ansible常见模块
## command : 命令模块,默认模块,用于在远程执行命令;
如:ansible all -a 'date'
# ansible websrvs -m command -a 'date'
# ansible dbsrvs -m command -a 'date'
# ansible all -m command -a 'date' ##运用所有主机
## cron : 添加移动计划任务
state:
present:安装
absent:移除
添加:
# ansible websrvs -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron"'
移除:
# ansible websrvs -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron" state=absent'
## user :添加移除用户
name=:指明创建的用户名
添加用户
# ansible all -m user -a 'name="user1"'
移除用户
# ansible all -m user -a 'name="user1" state=absent'
## group :添加移动组
# ansible websrvs -m group -
a 'name=mysql gid=306 system=yes'
# ansible websrvs -m user -a 'name=mysql uid=306 system=yes group=mysql'
## copy : 复制文件
src=:定义本地源文件路径
dest=:定义远程目标绝对路径
content=:取代src=,表示直接用此处指定的信息生成为目标文件的内容;
# ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640'
# ansible all -m copy -a 'content="hello Ansible\nHi magedu" dest=/tmp/test.ansible'
## file :设定文件属性
path=:指定文件路径,可以使用name或dest来替换;
创建文件的符号链接:
src=:指明源文件
path=:指定符号链接文件路径
# ansible all -m file -a 'owner=mysql group=mysql mode=644 path=/tmp/fstab.ansible'
创建符号链接:
# ansible all -m file -a 'path=/tmp/fstab.link src=/tmp/fstab.ansible state=link'
## ping : 测试指定主机是否能连接;
# ansible all -m ping ## service:指定运行状态;
enabled=:是否开机自动启动,取值为true或者false;
name=:服务名称
state=:状态,取值有started,stopped,restarted;
# ansible websrvs -m service -a 'enabled=true name=httpd state=started'
# ansible websrvs -a 'service httpd status'
# ansible websrvs -a 'chkconfig --list httpd'
## shell : 在远程主机上运行命令
尤其是用到管道等功能的复杂命令;
# ansible all -m user -a 'name=user1'
# ansible all -m shell -a 'echo mageedu | passwd --stdin user1'
## script: 将本地脚本复制到远程主机并运行之;
注意:要使用相对路径指定脚本
[root@node1 ~]# cat /tmp/test.sh
#!/bin/bash
echo "hello ansible from script" > /tmp/script.ansible
useradd user2
# ansible all -m script -a "/tmp/test.sh"
## yum :安装程序包
name=:指明要安装的程序包,可以带上版本号;
state=:present,latest表示安装,absent表示卸载;
# ansible all -m yum -a "name=zsh"
# ansible all -m yum -a "name=zsh state=absent"
## setup : 收集远程主机的facts
每个被管理节点在接收并运行管理命令之前,会将自己主机相关信息,如操作系统版本、IP地址等报告给远程的ansible主机;
# ansible all -m setup