ansible模块

ansible常用模块

根据官方的分类,将模块按功能分类为:云模块、命令模块、数据库模块、文件模块、资产模块、消息模块、监控模块、网络模块、通知模块、包管理模块、源码控制模块、系统模块、单元模块、web设施模块、windows模块 ,具体可以参看官方页面。
可以通过ansible-doc -l查看所有模块,可以使用ansible-doc -s module来查看某个模块的参数,也可以使用ansible-doc help module来查看该模块更详细的信息
这里从官方分类的模块里选择最常用的一些模块进行介绍

ansible

​ [root@localhost ~]# cat ansible/host
​ [http]
​ 192.168.37.15
​ 192.168.37.16

ping
测试主机是否是通的,用法很简单,不涉及参数:

​	[root@localhost ~]# ansible http -i /root/ansible/host -m ping
​	192.168.37.15 | SUCCESS => {
​    	"changed": false,
  	  "ping": "pong"
​	}
​	192.168.37.16 | SUCCESS => {
   	 "changed": false,
   	 "ping": "pong"
​	}

setup
setup模块,主要用于获取主机信息,在playbooks里经常会用到的一个参数gather_facts就与该模块相关。setup模块下经常使用的一个参数是filter参数,具体使用示例如下:

#查看主机内存信息
[root@localhost ~]# ansible http -i /root/ansible/host -m setup -a 'filter=ansible_*_mb'
#查看主机网卡信息
[root@localhost ansible]# ansible http -i host -m setup -a 'filter=ansible_ens32'
#将所有主机的信息输入到/tmp/facts目录下,每台主机的信息输入到主机名文件中(/etc/ansible/hosts里的主机名)
[root@localhost ansible]# ansible http -i host -m setup --tree /tmp/facts
[root@localhost ansible]# ls /tmp/facts/
192.168.37.15  192.168.37.16

file

file模块主要用于远程主机上的文件操作,file模块包含如下选项:

 - force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
- group:定义文件/目录的属组  mode:定义文件/目录的权限 owner:定义文件/目录的属主
- path:必选项,定义文件/目录的路径 recurse:递归的设置文件的属性,只对目录有效
-  src:要被链接的源文件的路径,只应用于state=link的情况 dest:被链接到的路径,只应用于state=link的情况 
- state:  directory:如果目录不存在,创建目录 file:即使文件不存在,也不会被创建 link:创建软链接
- hard:创建硬链接 touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
- absent:删除目录、文件或者取消链接文件

使用示例:

[root@localhost ~]# ansible http -i /root/ansible/host -m file -a ‘src=/etc/fstab dest=/tmp/fstab state=link’
192.168.37.16 | SUCCESS => {
“changed”: true,
“dest”: “/tmp/fstab”,
“gid”: 0,
“group”: “root”,
“mode”: “0777”,
“owner”: “root”,
“size”: 10,
“src”: “/etc/fstab”,
“state”: “link”,
“uid”: 0
}
192.168.37.15 | SUCCESS => {
“changed”: true,
“dest”: “/tmp/fstab”,
“gid”: 0,
“group”: “root”,
“mode”: “0777”,
“owner”: “root”,
“size”: 10,
“src”: “/etc/fstab”,
“state”: “link”,
“uid”: 0
}

[root@localhost ansible]# ansible http -i host -a ‘ls -l /tmp/fstab’
192.168.37.16 | SUCCESS | rc=0 >>
lrwxrwxrwx 1 root root 10 Aug 2 23:23 /tmp/fstab -> /etc/fstab

192.168.37.15 | SUCCESS | rc=0 >>
lrwxrwxrwx 1 root root 10 Aug 2 23:23 /tmp/fstab -> /etc/fstab

[root@localhost ansible]# ansible http -i host -m file -a ‘path=/tmp/fstab state=absent’
192.168.37.16 | SUCCESS => {
“changed”: true,
“path”: “/tmp/fstab”,
“state”: “absent”
}
192.168.37.15 | SUCCESS => {
“changed”: true,
“path”: “/tmp/fstab”,
“state”: “absent”
}

[root@localhost ansible]# ansible http -i host -a ‘ls /tmp/’
192.168.37.16 | SUCCESS | rc=0 >>
ansible_LFDyy1
systemd-private-bd2827005d0a462dacc12b042f32d693-httpd.service-h924Xv
systemd-private-bd2827005d0a462dacc12b042f32d693-vgauthd.service-4Poduf
systemd-private-bd2827005d0a462dacc12b042f32d693-vmtoolsd.service-PIbHFQ

192.168.37.15 | SUCCESS | rc=0 >>
ansible_8C_9V3
systemd-private-1a40e3215b4b40afb5ccc8ea69b89fee-httpd.service-uV2C2I
systemd-private-1a40e3215b4b40afb5ccc8ea69b89fee-vgauthd.service-nGb5Uz
systemd-private-1a40e3215b4b40afb5ccc8ea69b89fee-vmtoolsd.service-QSzUZt

copy
复制文件到远程主机,copy模块包含如下选项:

  • backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no

  • content:用于替代"src",可以直接设定指定文件的值

  • dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录

  • directory_mode:递归的设定目录的权限,默认为系统默认权限

  • force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes

  • others:所有的file模块里的选项都可以在这里使用

  • src:要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用"/“来结尾,则只复制目录里的内容,如果没有使用”/"来结尾,则包含目录在内的整个内容全部复制,类似于rsync。

  • validate :要在复制到位之前运行的验证命令。要验证的文件的路径是通过’%s’传递的,必须存在,如下例所示。该命令安全地传递,因此扩展和管道等shell功能将无法正常工作。

     [root@localhost ansible]# ansible http -i host -m copy -a 'src=/tmp/test dest=/tmp/ owner=foo group=foo mode=0644 backup=yes'
     192.168.37.16 | SUCCESS => {
         "changed": true,
         "checksum": "951d90f5d45d8c19e78d170aa12932722340f38a",
         "dest": "/tmp/test/test.txt",
         "gid": 1000,
         "group": "foo",
         "md5sum": "655dc4b8b68d6db8c2fd6d9b3852136b",
         "mode": "0644",
         "owner": "foo",
         "size": 9,
         "src": "/root/.ansible/tmp/ansible-tmp-1564789479.13-62025558773941/source",
         "state": "file",
         "uid": 1000
     }
     192.168.37.15 | SUCCESS => {
         "changed": true,
         "checksum": "951d90f5d45d8c19e78d170aa12932722340f38a",
         "dest": "/tmp/test/test.txt",
         "gid": 1000,
         "group": "foo",
         "md5sum": "655dc4b8b68d6db8c2fd6d9b3852136b",
         "mode": "0644",
         "owner": "foo",
         "size": 9,
         "src": "/root/.ansible/tmp/ansible-tmp-1564789478.99-21749859046550/source",
         "state": "file",
         "uid": 1000
     }
    

command
该模块通过-a跟上要执行的命令可以直接执行,不过命令里如果有带有如下字符部分则执行不成功 “ so variables like $HOME and operations like “<”, “>”, “|”, and “&” will not work (use the shell module if you need these features).”

command模块包含如下选项:

  • creates:一个文件名,当该文件存在,则该命令不执行

  • free_form:要执行的linux指令

  • chdir:在执行指令之前,先切换到该指定的目录 removes:删除一个文件名,当该文件不存在,则该选项不执行

  • executable:切换shell来执行指令,该执行路径必须是一个绝对路径
    注解:command模块不是调用的shell的指令,所以没有bash的环境变量,也不能使用shell的一些操作方式,其他和shell没有区别

     [root@localhost ansible]# ansible http -i host -a "df -h"
     192.168.37.16 | SUCCESS | rc=0 >>
     Filesystem               Size  Used Avail Use% Mounted on
     /dev/mapper/centos-root  7.0G 1016M  6.0G  15% /
     devtmpfs                 226M     0  226M   0% /dev
     tmpfs                    237M     0  237M   0% /dev/shm
     tmpfs                    237M  4.5M  232M   2% /run
     tmpfs                    237M     0  237M   0% /sys/fs/cgroup
     /dev/sda1                197M  103M   95M  53% /boot
     tmpfs                     48M     0   48M   0% /run/user/0
     
     192.168.37.15 | SUCCESS | rc=0 >>
     Filesystem               Size  Used Avail Use% Mounted on
     /dev/mapper/centos-root  7.0G  1.1G  6.0G  16% /
     devtmpfs                 226M     0  226M   0% /dev
     tmpfs                    237M     0  237M   0% /dev/shm
     tmpfs                    237M  4.5M  232M   2% /run
     tmpfs                    237M     0  237M   0% /sys/fs/cgroup
     /dev/sda1                197M  103M   95M  53% /boot
     tmpfs                     48M     0   48M   0% /run/user/0
    

shell
用法其本和command一样,不过的是其是通过/bin/sh进行执行,所以shell 模块可以执行任何命令,就像在本机执行一样,“ It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.”
注解:shell模块调用的/bin/sh指令执行

[root@localhost ansible]# ansible http -i host -m shell -a "ps -ef|grep httpd"
192.168.37.15 | SUCCESS | rc=0 >>
root        882      1  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      970    882  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      971    882  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      972    882  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      973    882  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      974    882  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root       1264   1263  0 08:46 pts/0    00:00:00 /bin/sh -c ps -ef|grep httpd
root       1266   1264  0 08:46 pts/0    00:00:00 /bin/sh -c ps -ef|grep httpd

192.168.37.16 | SUCCESS | rc=0 >>
root        877      1  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      971    877  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      972    877  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      973    877  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      974    877  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      975    877  0 08:38 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root       1262   1261  0 08:46 pts/0    00:00:00 /bin/sh -c ps -ef|grep httpd
root       1264   1262  0 08:46 pts/0    00:00:00 /bin/sh -c ps -ef|grep httpd

raw
用法和shell 模块一样 ,其也可以执行任意命令,就像在本机执行一样,

[root@localhost ansible]# ansible http -i host -m raw -a 'ps -ef |grep sshd'
192.168.37.15 | SUCCESS | rc=0 >>
root        879      1  0 08:38 ?        00:00:00 /usr/sbin/sshd -D
root       1271    879  0 08:48 ?        00:00:00 sshd: root@pts/0
root       1273   1271  0 08:48 pts/0    00:00:00 bash -c ps -ef |grep sshd
root       1285   1273  0 08:48 pts/0    00:00:00 bash -c ps -ef |grep sshd
Shared connection to 192.168.37.15 closed.

192.168.37.16 | SUCCESS | rc=0 >>
root        880      1  0 08:38 ?        00:00:00 /usr/sbin/sshd -D
root       1270    880  0 08:48 ?        00:00:00 sshd: root@pts/0
root       1272   1270  0 08:48 pts/0    00:00:00 bash -c ps -ef |grep sshd
root       1284   1272  0 08:48 pts/0    00:00:00 bash -c ps -ef |grep sshd
Shared connection to 192.168.37.16 closed.

script
将管理端的shell 在被管理主机上执行,其原理是先将shell 复制到远程主机,再在远程主机上执行,原理类似于raw模块
注:raw模块和comand、shell 模块不同的是其没有chdir、creates、removes参数,chdir参数的作用就是先切到chdir指定的目录后,再执行后面的命令,这在后面很多模块里都会有该参数 。

[root@localhost ansible]# ansible http -i host -m script -a "/root/ansible/test.sh"
192.168.37.15 | SUCCESS => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.37.15 closed.\r\n",
    "stdout": "",
    "stdout_lines": []
}
192.168.37.16 | SUCCESS => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.37.16 closed.\r\n",
    "stdout": "",
    "stdout_lines": []
}
[root@localhost ansible]# ansible http -i host -a "ls"
192.168.37.16 | SUCCESS | rc=0 >>
1
1.2816.2019-08-02@23:55:11~
anaconda-ks.cfg
file10.txt
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
time.sh

192.168.37.15 | SUCCESS | rc=0 >>
1
anaconda-ks.cfg
file10.txt
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
time.sh

service
用于管理服务
该模块包含如下选项:

  • arguments:给命令行提供一些选项 enabled:是否开机启动 yes|no
  • name:必选项,服务名称
  • pattern:定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行
  • runlevel:运行级别 sleep:如果执行了restarted,在则stop和start之间沉睡几秒钟
  • state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)

使用示例:

[root@localhost ansible]# ansible http -i host -m service -a 'name=httpd state=restarted enabled=yes'
[root@localhost ansible]# ansible http -i host -m shell -a 'systemctl status httpd'

cton
用于管理计划任务
包含如下选项:

  • backup:对远程主机上的原任务计划内容修改之前做备份
  • cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划
  • day:日(1-31,/2,……)
  • hour:小时(0-23,/2,……)
  • minute:分钟(0-59,/2,……)
  • month:月(1-12,/2,……)
  • weekday:周(0-7,*,……)
  • job:要执行的任务,依赖于state=present
  • name:该任务的描述
  • special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly
  • state:确认该任务计划是创建还是删除
  • user:以哪个用户的身份执行

示例:

[root@localhost ansible]# ansible http -i host -m cron -a 'name="check dirs" hour="5,2" job="ls -a>/dev/null"'
192.168.37.15 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "check dirs"
    ]
}
192.168.37.16 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "check dirs"
    ]
}
[root@localhost ansible]# ansible http -i host -a "crontab -l"
192.168.37.16 | SUCCESS | rc=0 >>
#Ansible: check dirs
* 5,2 * * * ls -a>/dev/null

192.168.37.15 | SUCCESS | rc=0 >>
#Ansible: check dirs
* 5,2 * * * ls -a>/dev/null

filesystem
在块设备上创建文件系统
选项:

  • dev:目标块设备

  • force:在一个已有文件系统的设备上强制创建

  • fstype:文件系统的类型

  • opts:传递给mkfs命令的选项

     [root@localhost ansible]# ansible http -i host -a "lsblk"
     192.168.37.15 | SUCCESS | rc=0 >>
     NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
     sda               8:0    0   10G  0 disk
     ├─sda1            8:1    0  200M  0 part /boot
     └─sda2            8:2    0    9G  0 part
       ├─centos-root 253:0    0    7G  0 lvm  /
       └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
     sdb               8:16   0    1G  0 disk
     sr0              11:0    1  4.2G  0 rom
     
     192.168.37.16 | SUCCESS | rc=0 >>
     NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
     sda               8:0    0   10G  0 disk
     ├─sda1            8:1    0  200M  0 part /boot
     └─sda2            8:2    0    9G  0 part
       ├─centos-root 253:0    0    7G  0 lvm  /
       └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
     sdb               8:16   0    1G  0 disk
     sr0              11:0    1  4.2G  0 rom
     [root@localhost ansible]# ansible http -i host -m filesystem -a "dev=/dev/sdb fstype=xfs"
     192.168.37.16 | SUCCESS => {
         "changed": true
     }
     192.168.37.15 | SUCCESS => {
         "changed": true
     }
    

    yum
    使用yum包管理器来管理软件包,其选项有:

  • config_file:yum的配置文件

  • disable_gpg_check:关闭gpg_check

  • disablerepo:不启用某个源

  • enablerepo:启用某个源

  • name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径

  • state:状态(present,absent,latest)
    示例:

    [root@localhost ansible]# ansible root -i host -m yum -a "name=httpd state=latest"
    

    user与group
    user模块是请求的是useradd, userdel, usermod三个指令,goup模块请求的是groupadd, groupdel, groupmod 三个指令,具体参数这里不再细讲,直接上示例。
    1、user模块示例
    #创建用户,并知道家目录和shell等

     [root@localhost ansible]# ansible http -i host -m user -a 'createhome=yes home=/home/user1 password=123456 name=user1 shell=/bin/bash state=present'
     192.168.37.16 | SUCCESS => {
         "changed": true,
         "comment": "",
         "createhome": true,
         "group": 1001,
         "home": "/home/user1",
         "name": "user1",
         "password": "NOT_LOGGING_PASSWORD",
         "shell": "/bin/bash",
         "state": "present",
         "system": false,
         "uid": 1001
     }
     192.168.37.15 | SUCCESS => {
         "changed": true,
         "comment": "",
         "createhome": true,
         "group": 1001,
         "home": "/home/user1",
         "name": "user1",
         "password": "NOT_LOGGING_PASSWORD",
         "shell": "/bin/bash",
         "state": "present",
         "system": false,
         "uid": 1001
     }
    

#删除用户

[root@localhost ansible]# ansible http -i host -m user -a 'remove=yes name=user1 state=absent'
192.168.37.16 | SUCCESS => {
    "changed": true,
    "force": false,
    "name": "user1",
    "remove": true,
    "state": "absent"
}
192.168.37.15 | SUCCESS => {
    "changed": true,
    "force": false,
    "name": "user1",
    "remove": true,
    "state": "absent"
}

synchronize
使用rsync同步文件,其参数如下:

  • archive:归档,相当于同时开启recursive(递归)、links、perms、times、owner、group、-D选项都为yes ,默认该项为开启

  • checksum: 跳过检测sum值,默认关闭

  • compress:是否开启压缩

  • copy_links:复制链接文件,默认为no ,注意后面还有一个links参数

  • delete: 删除不存在的文件,默认no

  • dest:目录路径 dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议

  • dirs:传速目录不进行递归,默认为no,即进行目录递归

  • rsync_opts:rsync参数部分

  • set_remote_user:主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不同的情况

  • mode: push或pull 模块,push模的话,一般用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件
    示例:

     [root@localhost ansible]# echo 'hello world' > /tmp/helloworld
     [root@localhost ansible]# ansible http -i host -m file -a 'path=/var/www/ state=directory'
     [root@localhost ansible]# ansible http -i host -m synchronize -a 'src=/tmp/helloworld dest=/var/www/'
     192.168.37.16 | SUCCESS => {
         "changed": true,
         "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L /tmp/helloworld 192.168.37.16:/var/www/",
         "msg": "<f+++++++++ helloworld\n",
         "rc": 0,
         "stdout_lines": [
             "<f+++++++++ helloworld"
         ]
     }
     192.168.37.15 | SUCCESS => {
         "changed": true,
         "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L /tmp/helloworld 192.168.37.15:/var/www/",
         "msg": "<f+++++++++ helloworld\n",
         "rc": 0,
         "stdout_lines": [
             "<f+++++++++ helloworld"
         ]
     }
    

    mount

配置挂载点
选项:

  • dump

  • fstype:必选项,挂载文件的类型

  • name:必选项,挂载点

  • opts:传递给mount命令的参数

  • src:必选项,要挂载的文件

  • state:必选项

  • present:只处理fstab中的配置

  • absent:删除挂载点

  • mounted:自动创建挂载点并挂载之

  • umounted:卸载
    示例:
    #创建设备

      [root@localhost ansible]# ansible http -i host -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'
      192.168.37.16 | SUCCESS | rc=0 >>
      1024+0 records in
      1024+0 records out
      4194304 bytes (4.2 MB) copied, 0.00886388 s, 473 MB/s
      
      192.168.37.15 | SUCCESS | rc=0 >>
      1024+0 records in
      1024+0 records out
      4194304 bytes (4.2 MB) copied, 0.0345449 s, 121 MB/s
    

#与/dev/loop1关联

[root@localhost ansible]# ansible http -i host -a 'losetup /dev/loop1 /disk.img'
192.168.37.16 | SUCCESS | rc=0 >>


192.168.37.15 | SUCCESS | rc=0 >>

#格式化

[root@localhost ansible]# ansible http -i host -m filesystem -a 'fstype=ext4 force=yes opts=-F dev=/dev/loop1'
192.168.37.16 | SUCCESS => {
    "changed": true
}
192.168.37.15 | SUCCESS => {
    "changed": true
}

#挂载

[root@localhost ansible]# ansible http -i host -m mount -a 'name=/mnt src=/dev/loop1 fstype=ext4 state=mounted opts=rw'
192.168.37.16 | SUCCESS => {
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "ext4",
    "name": "/mnt",
    "opts": "rw",
    "passno": "0",
    "src": "/dev/loop1"
}
192.168.37.15 | SUCCESS => {
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "ext4",
    "name": "/mnt",
    "opts": "rw",
    "passno": "0",
    "src": "/dev/loop1"
}
[root@localhost ansible]# ansible http -i host -a 'df -h'
192.168.37.16 | SUCCESS | rc=0 >>
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  7.0G  1.1G  6.0G  16% /
devtmpfs                 226M     0  226M   0% /dev
tmpfs                    237M     0  237M   0% /dev/shm
tmpfs                    237M  4.6M  232M   2% /run
tmpfs                    237M     0  237M   0% /sys/fs/cgroup
/dev/sda1                197M  103M   95M  53% /boot
tmpfs                     48M     0   48M   0% /run/user/0
/dev/loop1               2.9M   45K  2.6M   2% /mnt

192.168.37.15 | SUCCESS | rc=0 >>
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  7.0G  1.1G  6.0G  16% /
devtmpfs                 226M     0  226M   0% /dev
tmpfs                    237M     0  237M   0% /dev/shm
tmpfs                    237M  4.6M  232M   2% /run
tmpfs                    237M     0  237M   0% /sys/fs/cgroup
/dev/sda1                197M  103M   95M  53% /boot
tmpfs                     48M     0   48M   0% /run/user/0
/dev/loop1               2.9M   45K  2.6M   2% /mnt

#创建文件

[root@localhost ansible]# ansible http -i host -m file -a 'path=/mnt/test.txt state=touch'
192.168.37.16 | SUCCESS => {
    "changed": true,
    "dest": "/mnt/test.txt",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "state": "file",
    "uid": 0
}
192.168.37.15 | SUCCESS => {
    "changed": true,
    "dest": "/mnt/test.txt",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "state": "file",
    "uid": 0
}

#卸载

[root@localhost ansible]# ansible http -i host -m file -a 'path=/mnt/test.txt state=absen
192.168.37.16 | SUCCESS => {
    "changed": true,
    "path": "/mnt/test.txt",
    "state": "absent"
}
192.168.37.15 | SUCCESS => {
    "changed": true,
    "path": "/mnt/test.txt",
    "state": "absent"
}
[root@localhost ansible]# ansible http -i host -m mount -a 'name=/mnt src=/dev/loop1 fsty=ext4 state=unmounted'
192.168.37.16 | SUCCESS => {
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "ext4",
    "name": "/mnt",
    "opts": "defaults",
    "passno": "0",
    "src": "/dev/loop1"
}
192.168.37.15 | SUCCESS => {
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "ext4",
    "name": "/mnt",
    "opts": "defaults",
    "passno": "0",
    "src": "/dev/loop1"
}

#取消关联

[root@localhost ansible]# ansible http -i host -a 'losetup -d /dev/loop1'
192.168.37.16 | SUCCESS | rc=0 >>


192.168.37.15 | SUCCESS | rc=0 >>

[root@localhost ansible]# ansible http -i host  -m file -a 'path=/disk.img state=absent'
192.168.37.16 | SUCCESS => {
    "changed": true,
    "path": "/disk.img",
    "state": "absent"
}
192.168.37.15 | SUCCESS => {
    "changed": true,
    "path": "/disk.img",
    "state": "absent"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值