Ansible模块默认存储路径: /usr/share/ansible
ansible 操作目标 -m 模块名 -a 模块参数
例子:
[root@www ansible]# ansible webservers -m command -a "w"
192.168.1.145 | success | rc=0 >>
14:07:16 up 1:33, 3 users, load average: 0.04, 0.02, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
centos tty7 :0 12:38 1:32m 5.63s 0.48s pam: gdm-passwo
centos pts/0 :0.0 12:38 1:23m 0.02s 0.02s /bin/bash
root pts/1 192.168.1.106 12:44 1:19m 0.09s 0.09s -bash
192.168.1.144 | success | rc=0 >>
17:38:23 up 3:26, 3 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
centos tty7 :0 15:44 3:26m 8.98s 0.47s pam: gdm-passwo
centos pts/0 :0.0 15:49 1:45m 0.04s 0.04s /bin/bash
root pts/1 192.168.1.106 15:53 1:45m 0.04s 0.04s -bash
[root@www ansible]#
运程命令模块:
模块包括command(默认)、script、shell。
command :在远程执行权限内的shell命令
script:在远程主机执行控制端的脚本文件。
shell:在控制端执行远程主机上的shell脚本文件
copy模块:
控制端向目标主机拷贝文件,类似scp
ansible webservers -m copy -a "src=/root/ dest=/tmp/"
stat模块:
获取远程主机文件状态信息。
ansible webservers -m stat -a "path=/root/filename"
get_url模块:
实现在远程主机下载指定url到远程主机上
ansible webservers -m get_url -a "url=http://www.baidu.com dest=/root/index.html"
yum 模块:
实现包管理 有yum 和apt-get两种
ansible webservers -m apt -a "pkg=curl state=latest"
ansible webservers -m yum -a "name=curl state=latest"
cron模块:
远程主机crontab操作
ansible webservers -m cron -a "name='check file' hour='5,2' job='ls -al' "
mout模块:
远程主机分挂载:
ansible wenservers -m mount -a "name=/mnt/data src=/dev/sda fstype =ext3"
service 模块:
远程主机系统服务管理:
ansible webservers -m service -a "name=httpd state=restarted"
ansible webservers -m service -a "name=httpd state=started"
sysctl 包管理模块:
远程主机sysctl配置
user 模块:
远程主机系统用户管理
添加用户fuchao
ansible webservers -m user -a "name=fuchao "
删除用户:
ansible webservers -m user -a "name=fuchao state=absent remove=yes"