文件操作模块
1、copy模块
将管理主机上的文件拷贝到远程主机
[root@self1-centos7-2 11:28:42 /test/self2-1/etc]#ansible-doc -s copy
- name: Copy files to remote locations
copy:
backup: # 当远程已经存在同名文件时,对远程文件备份,然后写入 新文件(yes).
content: # 不用src参数,直接指定内容
dest: # (required) 远程目的目录
force: # 当远程已经存在同名文件时,yes覆盖 no远程文件保持不变
group: # 指定远程文件的属组
mode: # 指定远程文件的主机,如mode=644,mode=u+x
owner: # 指定cp后文件的属主
src: # 本地文件或目录
示例:
[root@self1-centos7-2 12:22:40 /test]#ansible database -m copy -a "src=/test/cptest dest=/opt/"
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "5239c17f7592ab4f8fea163a4d7d1177f02e1c58",
"dest": "/opt/cptest",
"gid": 0,
"group": "root",
"md5sum": "bd0110896b0e14894d3528f1af618111",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 19,
"src": "/root/.ansible/tmp/ansible-tmp-1560342232.01-253224523824701/source",
"state": "file",
"uid": 0
}
[root@self1-centos7-2 12:23:54 /test]#ansible web -m copy -a 'content="test\n\cptest" dest=/opt/cptest'
self2-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "84a272da978d71820a634051ffb0b0a53f717184",
"dest": "/opt/cptest",
"gid": 0,
"group": "root",
"md5sum": "6bb426890e3cfa08a3332a6df68241a0",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 12,
"src": "/root/.ansible/tmp/ansible-tmp-1560342325.56-25853370168249/source",
"state": "file",
"uid": 0
}
10.2.0.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "84a272da978d71820a634051ffb0b0a53f717184",
"dest": "/opt/cptest",
"gid": 0,
"group": "root",
"md5sum": "6bb426890e3cfa08a3332a6df68241a0",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 12,
"src": "/root/.ansible/tmp/ansible-tmp-1560342325.51-19642783475550/source",
"state": "file",
"uid": 0
}
[root@self1-centos7-2 12:27:53 /test]#ansible database -m copy -a 'content="test\n\cptest111" dest=/opt/cptest backup=yes'
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/opt/cptest.3564.2019-06-12@12:29:24~",
"changed": true,
"checksum": "15412a571b707e23722a459fca1573fc83ed116d",
"dest": "/opt/cptest",
"gid": 0,
"group": "root",
"md5sum": "2e6a87caff89c51b3af158b878e9ba1c",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 15,
"src": "/root/.ansible/tmp/ansible-tmp-1560342562.27-243677665304227/source",
"state": "file",
"uid": 0
}
[root@self1-centos7-2 12:32:12 /test]#ansible database -m copy -a "src=/test/cptest dest=/opt/ owner=centos group=centos force=yes mode=604"
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "5239c17f7592ab4f8fea163a4d7d1177f02e1c58",
"dest": "/opt/cptest",
"gid": 1000,
"group": "centos",
"mode": "0604",
"owner": "centos",
"path": "/opt/cptest",
"secontext": "system_u:object_r:usr_t:s0",
"size": 19,
"state": "file",
"uid": 1000
}
2、file模块
创建文件,删除文件、目录,修改文件权限等文件操作
[root@self1-centos7-2 12:37:05 /test]#ansible-doc -s file
- name: Manage files and file properties
file:
force: # 使用state=link时.强制创建链接文件,无论指向的文件是否存在,或者已经存在同名链接。
group: # 指定操作文件属组.
mode: # 指定文件权限(like `0644' or `01777') (like `'644'' or`'1777'') (for example, `u+rwx' or `u=rw,g=r,o=r').
owner: # 指定操作文件属主
path: # (required) 要操作的文件或目录
recurse: # 当操作的文件为目录是,yes表示可以递归的修改目录中文件的属性
src: # `state=link' and `state=hard'时,指明链接路径
state: # 指明要操作的目录或文件性质,如创建时 directory(目录) touch(文件)link(软链接)hard(硬链接)
absent(删除一个文件或目录)
示例:先创建一个目录,再创建一个文件,最后创建一个软连接
[root@self1-centos7-2 12:52:59 /test]#ansible database -m file -a "path=/test/filetest state=directory"
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/test/filetest",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
[root@self1-centos7-2 12:54:13 /test]#ansible database -m file -a "path=/test/filetest/filetest state=touch"
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/test/filetest/filetest",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 0,
"state": "file",
"uid": 0
}
[root@self1-centos7-2 12:55:19 /test]#ansible database -m file -a "path=/test/filetest/linktest state=link src=/test/filetest/filetest"
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/test/filetest/linktest",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 23,
"src": "/test/filetest/filetest",
"state": "link",
"uid": 0
}
修改属组 属主 还是要用创建的格式
[root@self1-centos7-2 12:56:47 /test]#ansible database -m file -a "path=/test/filetest/filetest state=touch owner=centos group=centos"
[root@self1-centos7-2 13:00:03 /test]#ansible database -m file -a "path=/test/filetest state=directory owner=centos group=centos"
[root@self1-centos7-2 13:00:26 /test]#ansible database -m file -a "path=/test/filetest state=directory mode=0644"
删除了目录,没管目录中有没有文件
[root@self1-centos7-2 13:04:12 /test]#ansible database -m file -a "path=/test/filetest state=absent"
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/test/filetest",
"state": "absent"
}
修改目录的属组属主,一并将目录中的文件属组属主都改了
[root@self1-centos7-2 13:00:26 /test]ansible database -m file -a "path=/test/filetest state=directory owner=centos group=centos recurse=yes"
3、blockinfile模块
在指定的文件中插入一段文本,这段文本有记号,以便以后找到,可以进行修改删除操作。
[root@self1-centos7-2 13:17:30 /etc/ansible]#ansible-doc -s blockinfile
- name: Insert/update/remove a text block surrounded by marker lines
blockinfile:
backup: # 是否在修改前进行备份
block: # 指定要操作的“一段文本”。
create: # 如果指定操作的文件不存在,创建之
group: # Name of the group that should own the file/directory, as would be fed to `chown'.
insertafter: # 默认插入位置是末尾,这个参数用来指定插入的行数,插入此行数的后面,也可以进行正则匹配确定插入位置 =EOF就是插入到末尾
insertbefore: # 默认插入位置是末尾,这个参数用来指定插入的行数,插入此行数的前面,也可以进行正则匹配确定插入位置 =BOF就是插入到开头
marker: # 自定义标记 marker=#{mark}test #BEGIN test ... #END test 插入多段,必须要用了
marker_begin: # This will be inserted at `{mark}' in the opening ansible block marker.
marker_end: # This will be inserted at `{mark}' in the closing ansible block marker.
mode: # The permissions the resulting file or directory should have. For those used to `/usr/bin/chmod' remember that modes
owner: # Name of the user that should own the file/directory, as would be fed to `chown'.
path: # (required) 指定要操作的文件
state: # present或absent 插入更新操作为present,absent删除操作
示例:
先cp一个文件过去
[root@self1-centos7-2 13:33:04 /etc/ansible]#ansible database -m copy -a "src=/etc/rc.d/rc.local dest=/test"
添加两次,一次不指定标记,一次指定标记
[root@self1-centos7-2 13:34:13 /etc/ansible]#ansible database -m blockinfile -a 'path=/test/rc.local block="systemctl start mariadb\nsystemctl start httpd"'
[root@self1-centos7-2 13:37:22 /etc/ansible]#ansible database -m blockinfile -a 'path=/test/rc.local block="systemctl start mariadb\nsystemctl start httpd" marker="#{mark}service to start"'
在database节点上
[centos@self1-centos7-3 test]$ cat rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
# BEGIN ANSIBLE MANAGED BLOCK
systemctl start mariadb
systemctl start httpd
# END ANSIBLE MANAGED BLOCK
#BEGINservice to start
systemctl start mariadb
systemctl start httpd
#ENDservice to start
在存在的块上更改内容
[root@self1-centos7-2 13:37:22 /etc/ansible]#ansible database -m blockinfile -a 'path=/test/rc.local block="systemctl start mariadb" marker="#{mark}service to start"'
清空内容,两种方式
[root@self1-centos7-2 13:37:22 /etc/ansible]#ansible database -m blockinfile -a 'path=/test/rc.local block="" marker="#{mark}service to start"'
[root@self1-centos7-2 13:37:22 /etc/ansible]#ansible database -m blockinfile -a 'path=/test/rc.local marker="#{mark}service to start" state=absent'
在头部插入内容
[root@self1-centos7-2 13:41:17 /etc/ansible]#ansible database -m blockinfile -a 'path=/test/rc.local block="top--systemctl start mariadb" marker="#{mark} toptest" insertbefore=BOF'
[centos@self1-centos7-3 test]$ cat rc.local
#BEGIN toptest
top--systemctl start mariadb
#END toptest
#!/bin/bash
# THIS FILE
用正则表达式匹配插入
[root@self1-centos7-2 13:49:34 /etc/ansible]#ansible database -m blockinfile -a 'path=/test/rc.local block="top--systemctl start mariadb" marker="#{mark} bintest" insertafter="^#!/bin/bash"'
[centos@self1-centos7-3 test]$ cat rc.local
#BEGIN toptest
top--systemctl start mariadb
#END toptest
#!/bin/bash
#BEGIN bintest
top--systemctl start mariadb
#END bintest
4、lineinfile模块
确保“某一行文本”存在于指定的文件中,或者确保从文件中删除指定的“文本”,还可以根据正则表达式,替换“某一行文本”
[root@self1-centos7-2 13:52:08 /etc/ansible]#ansible-doc -s lineinfile
- name: Manage lines in text files
lineinfile:
backrefs: # 开启后项引用(yes)。
另:开了以后在替换操作时,没有匹配到就不会对文件进行更改;不开启的话没有匹配到会把替换的内容插到最后
backup: # 修改之前备份.
create: # 文件不存在就创建.
firstmatch: # Used with `insertafter' or `insertbefore'. If set, `insertafter' and `insertbefore' find a first line has regular
expression matches.
group: # Name of the group that should own the file/directory, as would be fed to `chown'.
insertafter: # 插入指定的行之后,行号或正则匹配。backrefs=yes,没匹配到不插入,否则(backrefs=no),插入到最后。EOF
insertbefore: # 插入指定的行之前,行号或正则匹配。backrefs=yes,没匹配到不插入,否则(backrefs=no),插入到最前。BOF
line: # 使用此参数指定文本内容
mode: # The permissions the resulting file or directory should have. For those used to `/usr/bin/chmod' remember that modes are
others: # All arguments accepted by the [file] module also work here.
owner: # Name of the user that should own the file/directory, as would be fed to `chown'.
path: # (required) 指定要操作的文件
regexp: # 使用的正则表达式。替换时,多行匹配,只替换最后被匹配的;删除时,匹配到的都删除。
selevel: # The level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the `range'. When set to
`_default', it will use the `level' portion of the policy if available.
serole: # The role part of the SELinux file context. When set to `_default', it will use the `role' portion of the policy if
available.
setype: # The type part of the SELinux file context. When set to `_default', it will use the `type' portion of the policy if
available.
seuser: # The user part of the SELinux file context. By default it uses the `system' policy, where applicable. When set to
`_default', it will use the `user' portion of the policy if available.
state: # present 或 absent。删除时指定为abset
示例
确保"I am exist"在文件中,已经有就不做任何操作,如果没有就加在末尾
[root@self1-centos7-2 14:16:26 /etc/ansible]#ansible database -m lineinfile -a 'path=/test/rc.local line="I am exist"'
[centos@self1-centos7-3 test]$ cat rc.local
#BEGIN toptest
top--systemctl start mariadb
#END toptest
#!/bin/bash
.....
.....
# END ANSIBLE MANAGED BLOCK
#BEGINservice to start
systemctl start mariadb
systemctl start httpd
#ENDservice to start
I am exist
再执行一遍,changed=false
[root@self1-centos7-2 14:18:25 /etc/ansible]#ansible database -m lineinfile -a 'path=/test/rc.local line="I am exist"'
self1-1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": false,
"msg": ""
}
替换操作,发现这个主要是用来改配置参数的。判断dir参数是否存在,不存在就建立,存在可以更改
[root@self1-centos7-2 14:21:26 /etc/ansible]#ansible database -m lineinfile -a 'path=/test/rc.local regexp="^dir" line="dir=I am exist"'
[centos@self1-centos7-3 test]$ cat rc.local
#BEGIN toptest
top--systemctl start mariadb
...
I am exist
dir=I am exist
改一下参数
[root@self1-centos7-2 14:31:48 /etc/ansible]#ansible database -m lineinfile -a 'path=/test/rc.local regexp="^dir" line="dir=I am exist really"'
[centos@self1-centos7-3 test]$ cat rc.local
#BEGIN toptest
top--systemctl start mariadb
.....
I am exist
dir=I am exist really
不匹配,就不添加backrefs=yes
[root@self1-centos7-2 14:31:48 ~]#ansible database -m lineinfile -a 'path=/test/rc.local regexp="^file" line="dir=I am exist " backrefs=yes'
根据line参数删除行,如果有多行相同,这些行都会被删除,发现2行,被删掉了
[root@self1-centos7-2 14:33:04 /etc/ansible]#ansible database -m lineinfile -a 'path=/test/rc.local line="systemctl start httpd" state=absent'
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": true,
"found": 2,
"msg": "2 line(s) removed"
}
根据正则表达式删除行,匹配多行,多行删除。发现以前操作的三行#BEGIN,删了
[root@self1-centos7-2 14:40:46 /etc/ansible]#ansible database -m lineinfile -a 'path=/test/rc.local regexp="^#BEGIN" state=absent'
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": true,
"found": 3,
"msg": "3 line(s) removed"
}
将以前写入的systemctl start mariadb 更改为mariadb,而且只改了最后一个
[root@self1-centos7-2 14:44:00 /etc/ansible]#ansible database -m lineinfile -a 'path=/test/rc.local regexp="(s.{8}).*(m.{6})" line="\2" backrefs=yes'
[centos@self1-centos7-3 test]$ cat rc.local
。。。。
touch /var/lock/subsys/local
# BEGIN ANSIBLE MANAGED BLOCK
systemctl start mariadb //这里的没有改
# END ANSIBLE MANAGED BLOCK
mariadb
#ENDservice to start
I am exist
dir=I am exist really
regexp="(s.{8}).*(m.{6})“匹配systemctl start mariadb;backrefs=yes可以使用后项引用;”\2"引用第二个分组,第二个分组即(m.{6}),即mariadb。这样mariadb替换了systemctl start mariadb。不是我语文没学好,这里单词上加引号更要看晕。