1. file模块

功能:为被控端创建文件或目录,设定权限属性;

主要参数如下:

参数 说明
path 指定远程服务器的路径,也可以写成‘dest’,‘name’
state 状态,可以将值设定为directory表示创建目录,设定为touch表示创建文件,设定为link表示创建软连接,设定为hard表示创建硬连接,设定为absent表示删除目录文件或链接
mode 文件复制到远程并设定权限,默认file=644,directory=755
owner 文件复制到远程并设定属主,默认为root
group 文件复制到远程并设定属组,默认为root
recurese 递归修改
  • 示例一:创建文件/root/f1.sh,并设定属主、属组、权限:

    [root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/f1.sh owner=root group=root mode=755 state=touch'
    192.168.20.23 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "dest": "/root/f1.sh",
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "size": 0,
    "state": "file",
    "uid": 0
    }
    192.168.20.22 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "dest": "/root/f1.sh",
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "size": 0,
    "state": "file",
    "uid": 0
    }
  • 示例二:修改上例中文件的属主属组为xu

    [root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/f1.sh owner=xu group=xu'
    192.168.20.22 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "gid": 1000,
    "group": "xu",
    "mode": "0755",
    "owner": "xu",
    "path": "/root/f1.sh",
    "size": 0,
    "state": "file",
    "uid": 1000
    }
    192.168.20.23 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "gid": 1000,
    "group": "xu",
    "mode": "0755",
    "owner": "xu",
    "path": "/root/f1.sh",
    "size": 0,
    "state": "file",
    "uid": 1000
    } [root@nginx03 ~]# ll /root/f1.sh
    -rwxr-xr-x 1 xu xu 0 Aug 1 22:22 /root/f1.sh
  • 示例三:创建目录/root/test/,并设定属主、属组、权限

    [root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/test owner=root group=root mode=755 state=directory'
    192.168.20.23 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/root/test",
    "size": 6,
    "state": "directory",
    "uid": 0
    }
    192.168.20.22 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/root/test",
    "size": 6,
    "state": "directory",
    "uid": 0
    } [root@nginx03 ~]# ll /root/test/ -d
    drwxr-xr-x 2 root root 6 Aug 1 22:26 /root/test/
  • 示例四:为/root/f1.sh创建软链接文件/root/f1.sh.link

    [root@xuzhichao ~]# ansible NginxWebs -m file -a 'src=/root/f1.sh dest=/root/f1.sh.link state=link'
    192.168.20.23 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "dest": "/root/f1.sh.link",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "size": 11,
    "src": "/root/f1.sh",
    "state": "link",
    "uid": 0
    }
    192.168.20.22 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "dest": "/root/f1.sh.link",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "size": 11,
    "src": "/root/f1.sh",
    "state": "link",
    "uid": 0
    } [root@nginx03 ~]# ll /root/f1.sh*
    -rwxr-xr-x 1 xu xu 0 Aug 1 22:22 /root/f1.sh
    lrwxrwxrwx 1 root root 11 Aug 1 22:28 /root/f1.sh.link -> /root/f1.sh
  • 示例五:删除以上示例中创建的目录和文件:

    [root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/f1.sh.link state=absent'
    192.168.20.23 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "path": "/root/f1.sh.link",
    "state": "absent"
    }
    192.168.20.22 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "path": "/root/f1.sh.link",
    "state": "absent"
    }
    [root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/f1.sh state=absent' [root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/test state=absent'
  • 示例六:也可以删除一级目录(挂载点),会报错,但是可以清楚数据:

    [root@localhost /data] #ansible app -m file -a 'dest=/data/ state=absent'
    192.168.169.130 | FAILED! => {
    "changed": false, <==报错 [root@localhost /data] #ansible app -a 'ls -l /data/'
    total 0 <==数据已经清空
  • 示例七:递归授权目录,类似于-R的作用:

    [root@manger ~]# ansible webservers -m file -a "path=/tmp/foo state=directory owner=root group=root mode=777 recurse=yes"

ansible(7)--ansible的file模块的更多相关文章

  1. ansible 的file 模块

    创建.修改.删除文件或者目录: file模块 file模块常用的几个参数:state.path.src.dest.mode.owner.group.name.recurse state后面跟的参数:  ...

  2. ansible使用file模块管理受控机的目录与文件(ansible2.9.5)

    一,ansible的file模块的用途 file 模块实现对文件的基本操作. 例如: 创建文件或目录 删除文件或目录 修改文件权限等 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https:// ...

  3. ansible学习系列2-ansible常用模块使用

    1. 查看支持的模块 [root@localhost ~]# ansible-doc -l 这里我们看下ansible的支持的模块个数 [root@localhost ~]# ansible-doc ...

  4. ansible学习基础知识和模块(一)

    基础知识补充: 常用自动化运维工具 Ansible:使用python来开发的,无需设置Agentless(代理),一般管理几百台.与ssh的方式也不一样,ssh是基于c/s模式(客户端+服务器)来使用 ...

  5. Ansible安装部署以及常用模块详解

    一.  Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...

  6. ansible环境部署及常用模块总结 - 运维笔记

    一.  Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...

  7. Ansible安装部署及常用模块详解

    Ansible命令使用 Ansible语法使用ansible <pattern_goes_here> -m <module_name> -a <arguments> ...

  8. Ansible基础配置与常用模块使用

    环境介绍: Ansible服务端IP:192.168.2.215 Ansible客户端IP:192.168.2.216.192.168.2.218.192.168.2.113   一.创建Ansibl ...

  9. Ansible 开发调试 之【模块调试】

    本地调试 需要安装jinja2 库 yum -y install python-jinja2 使用官方提供的测试脚本调试 git clone git://github.com/ansible/ansi ...

  10. 10.Python之Ansible自动化运维常用模块

    Ansible中文权威文档:http://www.ansible.com.cn/docs/ Ansible从入门到精通:https://www.bilibili.com/video/av3361175 ...

随机推荐

  1. KingbaseES 查询优化消除SubPlan

    说明: 日常业务系统在使用SQL语句进行查询时,开发人员容易将sql查询的子查询放到select语句中进行使用,会造成sql性能的下降. 数据准备: test=# test=# select coun ...

  2. Typora基础使用教程

    Typora基础使用教程(入门级) 安装和激活 安装 typora任意地方搜索下载即可 激活 百度网盘链接链接:https://pan.baidu.com/s/1WKig_3-hkDZTRjS1rgG ...

  3. 配置腾讯云轻量级linux服务器用到的资源和步骤

    pasv_address=82.157.112.34 #请修改为您的 Linux 云服务器公网 IPsftp://82.157.112.34:21 ①下载系统可视化https://cloud.tenc ...

  4. 【已解决】(MySQL)SQL注入绕过登陆验证直接登陆---用户名输入框注释sql密码语句段

    今天学习了一种sql注入方法,通过注释密码验证部分的sql语句. 这是登陆界面 在用户名如果输入 15284206891' and 1=1 # 密码可以随意输入即可登陆成功 原理如下: 在sql可视化 ...

  5. Docker容器编排技术解析与实践

    本文全面探索了容器编排技术的核心概念.工具和高级应用,包括Docker Compose.Kubernetes等主要平台及其高级功能如网络和存储管理.监控.安全等.此外,文章还探讨了这些技术在实际应用中 ...

  6. 使用 MediaStream Recording API 和 Web Audio API 在浏览器中处理音频(未完待续)

    使用 MediaStream Recording API 和 Web Audio API 在浏览器中处理音频 1. 背景 最近项目上有个需求,需要实现:录音.回放录音.实现音频可视化效果.上传wav格 ...

  7. #单调栈#CodeChef Meteor

    METEORAK 分析 设 \(dp[l][r]\) 表示第 \(l\) 到 \(r\) 行的答案,可以发现它由 \(f[l][r],dp[l][r+1],dp[l+1][r]\) 转移而来. 关键就 ...

  8. #Splay#洛谷 1486 [NOI2004]郁闷的出纳员

    题目 分析 考虑加减工资直接打标记,查询第\(k\)多可以用平衡树, 删除有点恶心,这里考虑Splay,将需要删除的部分的后继splay到根节点并将左子树断边 代码 #include <cstd ...

  9. MyBatis-Plus 代码生成(旧)

    MyBatis-Plus官网的代码生成器配置不是特别全,在此整理了较为完整的配置,供自己和大家查阅学习. // 代码生成器 AutoGenerator mpg = new AutoGenerator( ...

  10. MogDB/openGauss 坏块测试-对启动的影响-测试笔记1

    MogDB/openGauss 坏块测试-对启动的影响-测试笔记 1 在 UPDATE 操作提交后,脏块落盘前 kill 掉 mogdb 数据库,然后对 UPDATE 修改的坏进行以下破坏操作,仍然能 ...