file模块:
file模块用于对文件相关的操作(创建、删除、属性修改、软链接等)touch是创建。
[root@localhost ~]# ansible group1 -m file -a "path=/tmp/111 state=touch"
192.168.17.105 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/111",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
192.168.17.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/111",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
[root@localhost ~]# ansible group1 -m file -a "path=/tmp/111 state=touch owner=ftp group=daemon mode=777"
192.168.17.105 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/111",
"gid": 2,
"group": "daemon",
"mode": "0777",
"owner": "ftp",
"size": 0,
"state": "file",
"uid": 14
}
192.168.17.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/111",
"gid": 2,
"group": "daemon",
"mode": "0777",
"owner": "ftp",
"size": 0,
"state": "file",
"uid": 14
}
创建目录,并更改权限:directory:
[root@localhost ~]# ansible group1 -m file -a "path=/test/aaa/bbb state=directory mode=777"
192.168.17.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"path": "/test/aaa/bbb",
"size": 6,
"state": "directory",
"uid": 0
}
192.168.17.105 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"path": "/test/aaa/bbb",
"size": 6,
"state": "directory",
"uid": 0
}
[root@agent1 tmp]# cd /test/aaa/bbb
[root@agent1 bbb]# ll
total 0
[root@agent1 bbb]# pwd
/test/aaa/bbb
删除文件:absent
[root@localhost ~]# ansible group1 -m file -a "path=/tmp/112 state=absent"
192.168.17.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/tmp/112",
"state": "absent"
}
192.168.17.105 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/tmp/112",
"state": "absent"
}
absent:没有、缺席、缺乏的意思。
删除目录:
[root@localhost ~]# ansible group1 -m file -a "path=/test state=absent"
192.168.17.105 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/test",
"state": "absent"
}
192.168.17.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/test",
"state": "absent"
}
absent:目录将会递归被删除。文件和软链接将会unlinked。
创建软链接:
[root@localhost ~]# ansible group1 -m file -a "src=/tmp/111 path=/tmp/222 state=link"
192.168.17.105 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/222",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 8,
"src": "/tmp/111",
"state": "link",
"uid": 0
}
192.168.17.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/222",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 8,
"src": "/tmp/111",
"state": "link",
"uid": 0
}
我们看到软链接的文件的大小稍微大点。
硬链接:hard
[root@localhost ~]# ansible group1 -m file -a "src=/tmp/111 path=/tmp/555 state=hard"
192.168.17.105 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/555",
"gid": 2,
"group": "daemon",
"mode": "0777",
"owner": "ftp",
"size": 0,
"src": "/tmp/111",
"state": "hard",
"uid": 14
}
192.168.17.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/555",
"gid": 2,
"group": "daemon",
"mode": "0777",
"owner": "ftp",
"size": 0,
"src": "/tmp/111",
"state": "hard",
"uid": 14
}
硬链接是看inode号。
group的组配置,要注意下是要存在的。
修改硬链接的属性:file ,如果是硬链接,就需要添加state=file,这个要注意的。
[root@localhost ~]# ansible group1 -m file -a "path=/tmp/555 state=file owner=ftp group=daemon mode=000"
192.168.17.105 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 2,
"group": "daemon",
"mode": "0000",
"owner": "ftp",
"path": "/tmp/555",
"size": 0,
"state": "hard",
"uid": 14
}
192.168.17.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 2,
"group": "daemon",
"mode": "0000",
"owner": "ftp",
"path": "/tmp/555",
"size": 0,
"state": "hard",
"uid": 14
}
修改文件的拥有者和属组:文章来源:https://www.toymoban.com/news/detail-682874.html
[root@localhost ~]# ansible group1 -m file -a "path=/tmp/aaa owner=ftp group=daemon mode=000"
192.168.17.105 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 2,
"group": "daemon",
"mode": "0000",
"owner": "ftp",
"path": "/tmp/aaa",
"size": 0,
"state": "file",
"uid": 14
}
192.168.17.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 2,
"group": "daemon",
"mode": "0000",
"owner": "ftp",
"path": "/tmp/aaa",
"size": 0,
"state": "file",
"uid": 14
}
文章来源地址https://www.toymoban.com/news/detail-682874.html
到了这里,关于Ansible学习笔记4的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!