ansible(7)--ansible的file模块
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模块的更多相关文章
- ansible 的file 模块
创建.修改.删除文件或者目录: file模块 file模块常用的几个参数:state.path.src.dest.mode.owner.group.name.recurse state后面跟的参数: ...
- ansible使用file模块管理受控机的目录与文件(ansible2.9.5)
一,ansible的file模块的用途 file 模块实现对文件的基本操作. 例如: 创建文件或目录 删除文件或目录 修改文件权限等 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https:// ...
- ansible学习系列2-ansible常用模块使用
1. 查看支持的模块 [root@localhost ~]# ansible-doc -l 这里我们看下ansible的支持的模块个数 [root@localhost ~]# ansible-doc ...
- ansible学习基础知识和模块(一)
基础知识补充: 常用自动化运维工具 Ansible:使用python来开发的,无需设置Agentless(代理),一般管理几百台.与ssh的方式也不一样,ssh是基于c/s模式(客户端+服务器)来使用 ...
- Ansible安装部署以及常用模块详解
一. Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...
- ansible环境部署及常用模块总结 - 运维笔记
一. Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...
- Ansible安装部署及常用模块详解
Ansible命令使用 Ansible语法使用ansible <pattern_goes_here> -m <module_name> -a <arguments> ...
- Ansible基础配置与常用模块使用
环境介绍: Ansible服务端IP:192.168.2.215 Ansible客户端IP:192.168.2.216.192.168.2.218.192.168.2.113 一.创建Ansibl ...
- Ansible 开发调试 之【模块调试】
本地调试 需要安装jinja2 库 yum -y install python-jinja2 使用官方提供的测试脚本调试 git clone git://github.com/ansible/ansi ...
- 10.Python之Ansible自动化运维常用模块
Ansible中文权威文档:http://www.ansible.com.cn/docs/ Ansible从入门到精通:https://www.bilibili.com/video/av3361175 ...
随机推荐
- 【Java】归并排序
代码: 1 public static void mergeSort(int[] arr) { 2 if (arr == null || arr.length < 2) { 3 return; ...
- Azkaban 2.5 Documentation
Overview Azkaban was implemented at LinkedIn to solve the problem of Hadoop job dependencies. We had ...
- 小师妹学JavaIO之:try with和它的底层原理
目录 简介 IO关闭的问题 使用try with resource try with resource的原理 自定义resource 总结 简介 小师妹是个java初学者,最近正在学习使用java I ...
- OpenHarmony兼容性平台更新上线
一.前言 OpenAtom OpenHarmony(以下简称"OpenHarmony")兼容性测评平台已经运行了一年,随着 OpenHarmony 开源项目的不断向前演进和兼容性测 ...
- Counter 1000
From a 1000 Hz clock, derive a 1 Hz signal, called OneHertz, that could be used to drive an Enable s ...
- 直播预告丨Hello HarmonyOS进阶课程第三课——游戏开发实践
为了帮助初识HarmonyOS的开发者快速入门,我们曾推出Hello HarmonyOS系列一共5期课程,从最基础的配置IDE和创建Hello World开始,详细介绍HarmonyOS基础.开发环境 ...
- 第十篇:异步IO、消息队列
一.协程 二.异步IO_Gevent 三.协程异步IO操作 四.事件驱动模型 五.IO多路复用 六.异步IO理论 一.回顾 线程 vs 进程 线程:CPU最小调度单位,内存共享: 线程同时修改同一份数 ...
- redis 简单整理——缓存设计[三十二]
前言 简单整理一下缓存设计. 正文 缓存的好处: ·加速读写:因为缓存通常都是全内存的(例如Redis.Memcache),而 存储层通常读写性能不够强悍(例如MySQL),通过缓存的使用可以有效 地 ...
- 本地部署Llama3-8B/72b 并进行逻辑推理测试
美国当地时间4月18日,Meta开源了Llama3大模型,目前开源版本为8B和70B.Llama 3模型相比Llama 2具有重大飞跃,并在8B和70B参数尺度上建立了LLM模型的新技术.由于预训练和 ...
- 【Oracle】使用PL/SQL快速查询出1-9数字
[Oracle]使用PL/SQL快速查询出1-9数字 简单来说,直接Recursive WITH Clauses 在Oracle 里面就直接使用WITH result(参数)即可 WITH resul ...