command , shell , raw, script 模块
1)ping模块
[root@ansible .ssh]# ansible web1 -m ping
2)command模块
[root@ansible .ssh]# ansible web1 -m command -a 'chdir=/tmp touch f1' //创建成功
3)shell模块
[root@ansible .ssh]# ansible web1 -m shell -a 'chdir=/tmp touch f2' //创建成功
[root@web1 ~]# cd /tmp/
[root@web1 tmp]# ls //在web1上面查看
f2
4)raw模块
[root@ansible .ssh]# ansible web1 -m raw -a 'chdir=/tmp touch f3'
//文件可以创建,但无法切换目录,文件在用户家目录下生成
web1 | SUCCESS | rc=0 >>
Shared connection to web1 closed.
[root@web1 tmp]# cd /root/
[root@web1 ~]# ls //在web1上面查看
f3
5)script模块
对于太复杂的命令,可以写个脚本,然后用script模块执行
在web1主机上创建zhangsan3用户,修改zhangsan3的密码为123456,设置zhangsan3第一次登陆必须修改密码
用命令写:
[root@ansible .ssh]# ansible web1 -m shell -a 'useradd zhangsan3'
[root@ansible .ssh]# ansible web1 -m shell -a 'echo 123456 | passwd --stdin zhangsan3'
[root@ansible .ssh]# ssh -l zhangsan3 web1
zhangsan3@web1's password: //输入zhangsan3的密码
[root@ansible .ssh]# ansible web1 -m shell -a 'chage -d 0 zhangsan3'
[root@ansible .ssh]# ssh -l zhangsan3 web1
用脚本写,script模块执行:
[root@ansible .ssh]# vim user.sh
#!/bin/bash
useradd zhangsan3
echo 123456 | passwd --stdin zhangsan3
chage -d 0 zhangsan3
echo
[root@ansible .ssh]# ansible web1 -m script -a './user.sh'