经常使用模块使用linux
首先在使用模块以前咱们能够查看一下模块有多少,不过大多数用不到。下面主要看几个经常使用模块redis
[root@soso ~]# ansible-doc -l | wc -l 262
一、setup
shell
##用来查看远程主机的基本信息apache
[root@soso ~]# ansible test -m setup
二、pingbash
##用来测试远程主机的运行状态服务器
[root@soso ~]# ansible test -m ping
三、filessh
##设置文件属性ide
[root@soso ~]# ansible-doc -s file - name: Sets attributes of files action: file force # 须要在两种状况下建立软链接,一种是源文件不存在,但以后创建的状况下;另外一种是目标软链接已存在,须要先取消以前的软链,而后建立新的软链:有两个选项:yes|no group # 定义文件/目录的所属组 mode # 定义文件/目录的权限 owner # 定义文件/目录的所属主 path= # 必选项,定义文件/目录的路径 recurse # 递归设置文件的属性,只对目录有效 src # 要被连接的源文件路径,只应用于state=link的状况有效 state # 状态 directory #若是目录不存在则建立目录 file #即便文件不存在,也不会被建立 link #建立软链接 hard #建立硬连接 touch #若是文件不存在,则会建立一个新的文件;若是文件或目录存在,则更新其最后的修改时间 absent #删除目录、文件或者取消连接文件 (END)
将远程主机/etc/passwd 文件连接到 /root 目录下:测试
[root@soso ~]# ls Python-2.7.5 shell soft [root@soso ~]# ansible test -m file -a "src=/etc/passwd dest=/root/passwd state=link" 192.168.1.2 | success >> { "changed": true, "dest": "/root/passwd", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 11, "src": "/etc/passwd", "state": "link", "uid": 0 } [root@soso ~]# ll total 12 drwxr-xr-x 18 1000 1000 4096 Nov 26 11:11 Python-2.7.5 lrwxrwxrwx 1 root root 11 Dec 16 15:35 passwd -> /etc/passwd drwxr-xr-x 3 root root 4096 Dec 7 15:06 shell drwxr-xr-x 12 root root 4096 Dec 16 15:32 soft
删除建立的软链接ui
[root@soso ~]# ansible test -m file -a 'path=/root/passwd state=absent' 192.168.1.2 | success >> { "changed": true, "path": "/root/passwd", "state": "absent" } [root@soso ~]# ll total 12 drwxr-xr-x 18 1000 1000 4096 Nov 26 11:11 Python-2.7.5 drwxr-xr-x 3 root root 4096 Dec 7 15:06 shell drwxr-xr-x 12 root root 4096 Dec 16 15:32 soft
建立文件
[root@soso ~]# ansible test -m file -a 'path=/root/1 state=touch' 192.168.1.2 | success >> { "changed": true, "dest": "/root/1", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0 } [root@soso ~]# ls 1 Python-2.7.5 shell soft
四、cope
##复制本地文件到远程主机,进行统一部署
用法:
backup # 用于复制时,是否备份远程主机上的目标文件
content # 用于替代 ‘src’ ,能够直接设定指定文件的值
dest= # 必选项,要将源文件复制到远程主机的绝对路径,若是源文件是一个目录,那么远程路径也必须是一个目录
directory_mode # 递归设定目录的权限,默认为系统默认权限
force # 强制覆盖。若是目标主机包含该文件,但内容不一样,若是设置为yes,则强制覆盖;若是设置为no,则只有当目录主机的目录位置不存在该文件时,才复制。默认为yes
group # 定义文件/目录的所属组
mode # 定义文件/目录的权限
owner # 定义文件/目录的所属主
src # 要复制到远程主机的文件在本地的地址,能够是绝对路径,也能够是相对路径。若是路径是一个目录,它将递归复制。在这种状况下,若是路径使用 '/' 结尾,则只复制目录里的内容,若是没有使用 '/' 来结尾,则包含目录在内的整个内容所有复制。
实例:
讲本地文件复制到远程主机
[root@soso ~]# ansible test -m copy -a "src=/root/1 dest=/tmp/1 mode=600 " 192.168.1.2 | success >> { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/tmp/1", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0600", "owner": "root", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1450252219.51-80486766978488/source", "state": "file", "uid": 0 } [root@soso ~]# ll /tmp/1 -rw------- 1 root root 0 Dec 16 15:50 /tmp/1
五、command
##远程主机上执行命令
creates:一个文件名,当该文件存在,则该命令不执行
free_form:要执行的linux指令
chdir:在执行指令以前,先切换到该目录
removes:一个文件名,当该文件不存在,则该选项不执行
executable:切换shell来执行指令,该执行路径必须是一个绝对路径
[root@soso ~]# ansible test -m command -a 'ls /home' 192.168.1.2| success | rc=0 >> redis zabbix
实例:
使用 creates 参数,判断一个文件是否存在,存在的话,就跳事后面的执行命令
[root@soso ~]# ansible test -m command -a 'creates=/tmp/1 ls -l /etc/passwd' 192.168.1.2 | success | rc=0 >> skipped, since /tmp/1 exists [root@soso ~]# ansible test -m command -a 'creates=/tmp/2 ls -l /etc/passwd' 192.168.1.2 | success | rc=0 >> -rw-r--r-- 1 root root 1882 Dec 14 20:11 /etc/passwd
六、shell
##与command不一样的是,此模块能够支持命令管道,同时还有另外一个模块也具有此功能:raw
实例1:
[root@soso ~]# ansible test -m shell -a 'cat /etc/passwd| grep root' 192.168.1.2 | success | rc=0 >> root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
七、synchronize
使用rsync同步文件。使用rsync 模块,系统必须安装rsync 包,不然没法使用这个模块
用法:
archive # 是否采用归档模式同步,即以源文件相同属性同步到目标地址
checksum # 是否效验
compress #
copy_links # 同步的时候是否复制链接
delete # 删除源中没有而目标存在的文件
dest= # 目标地址
dest_port # 目标接受的端口
dirs # 以非递归的方式传输目录
existing_only # Skip creating new files on receiver.
group # Preserve group
links # Copy symlinks as symlinks.
mode # 模式,rsync 同步的方式 PUSH\PULL
recursive # 是否递归 yes/no
rsync_opts # 使用rsync 的参数
rsync_path # 服务的路径(源码编译时需指定)
rsync_timeout # Specify a --timeout for the rsync command in seconds.
set_remote_user # put user@ for the remote paths. If you have a custom ssh config to define the remote user for
src= # 源,同步的数据源
检查机器上是否装过rsync:
[root@soso ~]#which rsync
没有装过的主机能够远程安装rsync包:
[root@soso ~]# ansible test -m yum -a 'name=rsync state=latest'
实例:
一、将ansible 服务端/root/ 下的hello 文件同步到test的/tmp:
[root@soso ~]# ansible test -m synchronize -a 'src=/root/hello dest=/tmp/' 192.168.1.2 | success >> { "changed": true, "cmd": "rsync --delay-updates -F --compress --archive --rsh 'ssh -S none -o StrictHostKeyChecking=no' --out-format='<<CHANGED>>%i %n%L' \"/root/hello\" \"root@121.42.141.231:/tmp/\"", "msg": "<f+++++++++ hello\n", "rc": 0, "stdout_lines": [ "<f+++++++++ hello" ] } [root@soso ~]# ls /tmp/hello /tmp/hello
八、cron
cron 模块,用于管理计划任务
用法:
backup # 在对远程主机上的原计划任务修改以前作备份(也就是先备份再修改)
cron_file # 若是指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划
day # 天(1-31,*,*/2, ……)
hour # 小时( 0-23, *, */2, ……)
job # T要执行的任务,依赖于state=present
minute # 分钟(0-59,*,*/2,……)
month # 月(1-12,*,*/2,……)
name # 该任务的描述
reboot # If the job should be run at reboot. This option
special_time #指定何时执行,参数: reboot,yearly,annually,monthly,weekly,daily,hourly
state # 确认该任务计划是建立仍是删除
user #以哪一个用户的身份执行
weekday #周(0-7,*,……)
实例:
一、给test 主机建立一个计划任务:
[root@soso ~]# ansible test -m cron -a "name='echo ' hour=2 user=root job='echo 1 >> /root/hello' " 192.168.1.2 | success >> { "changed": true, "jobs": [ "echo " ] } [root@soso ~]# ansible test -m shell -a "crontab -l" 192.168.1.2 | success | rc=0 >> * * * * 1 sh /root/shell/apache_log.sh #Ansible: echo * 2 * * * echo 1 >> /root/hello
二、删除刚才建立的计划任务
[root@soso ~]# ansible test -m cron -a "name='echo' hour=2 user=root job='echo 1 >> /root/hello' state=absent" 192.168.1.2 | success >> { "changed": true, "jobs": [] } [root@soso ~]# ansible test -m shell -a "crontab -l" 192.168.1.2 | success | rc=0 >> * * * * 1 sh /root/shell/apache_log.sh
九、service
service 模块做用于一些服务,好比对某些服务器的启动、重启、中止、重载等的管理
arguments # 给命令行提供一些选项
enabled # 是否开机启动 yes|no
name= # 必选项,服务名称
pattern # 定义一个模式,若是经过status指令来查看服务的状态时,没有响应,就会经过ps指令在进程中根据该模式进行查找,若是匹配到,则认为该服务依然在运行
runlevel # 运行级别
sleep # 若是执行了restarted,在则stop和start之间沉睡几秒钟
state # 对当前服务执行启动,中止、重启、从新加载等操做 (started,stopped,restarted,reloaded)
实例:
实例:远程启动http
[root@soso ~]# ss -nl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 80 *:3306 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 *:43131 *:* [root@soso ~]# ansible test -m service -a "name=httpd state=started enabled=yes" 192.168.1.2 | success >> { "changed": true, "enabled": true, "name": "httpd", "state": "started" } [root@soso ~]# ss -nl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 80 *:3306 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 *:43131 *:*
其余经常使用的模块还有 yum user filesystem mount