ansible模块之file模块和mount模块(九)

简介: 1.file模块语法格式

1.file模块

语法格式

ansible 主机组 -m file -a "path=目标路径 owner=属主 group=属组 mode=权限"
参数
path  //目录文件路径,相当于copy模块的dest,其他模块的name
src   //源文件路径
owner   //属主
group   //属组
mode    //权限
state   //控制状态
  absent    //删除
  directory //创建目录
  file    //修改文件属性(默认)
  touch   //创建文件
  link hard //链接
recurse   //递归,recurse=yes

案例1:修改目录/data属性,属主6666,属组6666

[root@ansible ~]# ansible all -m file -a "path=/data owner=6666 group=6666"

案例2:创建/data2目录,并设置父目录、子目录属主属组都为6666并且权限为755

[root@ansible ~]# ansible all -m file -a "path=/data2 owner=linux group=linux mode=755 state=directory recurse=yes"
192.168.81.220 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 6666, 
    "group": "linux", 
    "mode": "0755", 
    "owner": "linux", 
    "path": "/data2", 
    "size": 6, 
    "state": "directory", 
    "uid": 6666
}
[root@ansible ~]# ansible all -m command -a "ls -ld /data2"
192.168.81.220 | CHANGED | rc=0 >>
drwxr-xr-x 2 linux linux 6 6月   7 18:11 /data2

案例3:创建文件

[root@ansible ~]# ansible all -m file -a "name=/data2/aa.txt state=touch"

案例4:创建链接文件

[root@ansible ~]# ansible all -m file -a "src=/etc/hosts path=/tmp/hosts state=link"

2.mount模块

语法格式

ansible 主机组 -m mount -a "src=设备路径 path=挂载点 fstype=文件系统 state=挂载类型"
参数
path    //挂载点
src     //需要挂载的设备
fstype    //挂载设备的文件系统
  iso9660 //光驱
  ext4、xfs、nfs
  cifs samba的共享文件系统
  ntfs windows磁盘文件系统
opts    //挂载属性
  notime
  noexec
  nosuid
state   //挂载动作
  present   //开机挂载,仅将挂载配置写入/etc/fstab并不会真的挂载
  mounted   //挂载设备,并将配置写入/etc/fstab
  unmounted   //卸载设备,不会清除/etc/fstab写入的配置
  absent    //卸载设备,并清理/etc/fstab写入的配置

案例:配置nfs服务

1.安装服务器

[root@ansible ~]# ansible nfs -m yum -a "name=nfs-utils state=installed"

2.修改配置文件

方法1:通过传输文件来实现
[root@ansible ~]# mkdir /server/conf -p
[root@ansible ~]# echo "/data 192.168.81.0/24(rw,sync,all_squash,anonuid=6666,anongid=6666)" > /server/conf/exports
[root@ansible ~]# ansible nfs -m copy -a "src=/server/conf/exports dest=/etc/"
方法2:使用copy模块的content参数
[root@ansible ~]# ansible nfs -m copy -a "content='/data 192.168.81.0/24(rw,sync,all_squash,anonuid=6666,anongid=6666)' dest=/etc/exports"

3.启动服务

[root@ansible ~]# ansible nfs -m service -a "name=rpcbind state=restarted"
[root@ansible ~]# ansible nfs -m service -a "name=nfs state=restarted"

4.创建用户、目录、并修改所属

创建组
[root@ansible ~]# ansible nfs -m group -a "name=linuxnfs gid=6666"
创建用户
[root@ansible ~]# ansible nfs -m user -a "name=linux uid=6666 group=6666 shell=/bin/bash create_home=yes"
创建目录并修改所属
[root@ansible ~]# ansible nfs -m file -a "path=/data2 owner=linuxnfs group=linuxnfs state=directory recurse=yes"

5.在web上挂载nfs目录

[root@ansible ~]# ansible web -m mount -a "src=192.168.81.230:/data2 path=/var/www/html fstype=nfs state=mounted"
目录
相关文章
|
运维 Shell Linux
Ansible自动化运维工具之常用模块使用实战(5)
Ansible自动化运维工具之常用模块使用实战(5)
402 0
|
6月前
|
缓存 Shell Linux
[ansible]常用内置模块
[ansible]常用内置模块
|
7月前
|
Shell 应用服务中间件 Linux
Ansible的常用模块
Ansible的常用模块
160 6
|
7月前
|
Shell 数据安全/隐私保护
Ansible Ad-hoc,命令执行模块
Ansible Ad-hoc,命令执行模块
75 1
|
7月前
|
运维 Linux 应用服务中间件
Linux之自动化运维工具ansible、ansible模块(2)
Linux之自动化运维工具ansible、ansible模块(2)
|
7月前
|
运维 Linux Shell
Linux之自动化运维工具ansible、ansible模块(1)
Linux之自动化运维工具ansible、ansible模块(1)
|
9月前
|
算法 安全 Linux
Ansible 中的copy 复制模块应用详解
Ansible 中的copy 复制模块应用详解
526 1
|
运维 Linux
Ansible自动化运维工具之常用模块使用实战(6)
Ansible自动化运维工具之常用模块使用实战(6)
205 0
|
网络安全 数据安全/隐私保护
ansible的get_url模块
ansible的get_url模块
164 1
|
存储 Linux Python
ansible手动添加模块
ansible手动添加模块
118 0