当前位置:网站首页>快速学会文件操作模块

快速学会文件操作模块

2022-08-06 07:37:35我是渣渣辉

blockinfile模块

在node1上文件ansible_text文件中写入内容 ansible

[[email protected] ~]# echo ansible > ansible_text

然后使用blockinfile模块,在文件中插入内容 blockinfile insert content

[[email protected] ~]# ansible node -m blockinfile -a "path=/root/ansible_text block='blockinfile insert content' state=present "
node1.example.com | CHANGED => {
    
    "ansible_facts": {
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Block inserted"
}

然后插入内容 blockinfile with marker 且使用指定标记: marker=#{mark}test

[[email protected] ~]# ansible node -m blockinfile -a "path=/root/ansible_text block='blockinfile with marker' marker='#{mark}test' state=present "
node1.example.com | CHANGED => {
    
    "ansible_facts": {
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Block inserted"
}

在blockinfile insert content之前插入insertbefore

[[email protected] ~]# ansible node -m blockinfile -a "path=/root/ansible_text block='insertbefore' insertbefore='blockinfile insert content' marker='#{mark}test1' state=present "
node1.example.com | CHANGED => {
    
    "ansible_facts": {
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Block inserted"
}

在blockinfile insert content之后插入 insertafter

[[email protected] ~]# ansible node -m blockinfile -a "path=/root/ansible_text block='insertafter' insertafter='blockinfile insert content' marker='#{mark}test2' state=present " 
node1.example.com | CHANGED => {
    
    "ansible_facts": {
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Block inserted"
}

lineinfile模块

我们使用/root/test文件作为被操作的文件,test文件内容如下

# cat /root/test 
123
234
345
456

1.插入内容。 判断文件中test text内容是否存在,不存在则在文档尾插入该内容

[[email protected] ~]# ansible node -m lineinfile -a "path=/root/test line='test text' "


[[email protected] ~]# cat test
123
234
345
456
test text

2.regexp支持正则符匹配可以将匹配的行进行替换,当匹配不到则在文档尾追加写入

[[email protected] ~]# ansible node -m lineinfile -a "path=/root/test line='test text' regexp='^1'"
node1.example.com | CHANGED => {
    
    "ansible_facts": {
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}

[[email protected] ~]# cat test
test text
234
345
456
test text

[[email protected] ~]# ansible node -m lineinfile -a "path=/root/test line='testtext' regexp='^1'"
node1.example.com | CHANGED => {
    
    "ansible_facts": {
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}

[[email protected] ~]# cat test
test text
234
345
456
test text
testtext

3.基于正则替换行,当没有匹配到指定行则不做任何更改

[[email protected] ~]# ansible node -m lineinfile -a "path=/root/test line='test text' regexp='^line'"
node1.example.com | SUCCESS => {
    
    "ansible_facts": {
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": false,
    "msg": ""
}

4.匹配指定内容的行删除(若多行相同全部删除)

[[email protected] ~]# ansible node -m lineinfile -a "path=/root/test state=absent line='test text'"
node1.example.com | CHANGED => {
    
    "ansible_facts": {
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "found": 2,
    "msg": "2 line(s) removed"
}

根据正则表达式删除对应行,如果有多行都满足正则表达式,那么所有匹配的行都会被删除

[[email protected] ~]# ansible node -m lineinfile -a "path=/root/test state=absent regexp='4'"
node1.example.com | CHANGED => {
    
    "ansible_facts": {
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "found": 3,
    "msg": "3 line(s) removed"
}

如果将backrefs设置为yes,表示开启支持后向引用,使用如下命令,可以将test示例文件中的"Hello ansible,Hiiii"替换成"Hiiii",如果不设置backrefs=yes,则不支持后向引用,那么"Hello ansible,Hiiii"将被替换成"\2"

[[email protected] ~]# ansible node -m lineinfile -a 'path=/root/test regexp="(H.{4}).*(H.{4})" line="\2" backrefs=yes' 
node1.example.com | CHANGED => {
    
    "ansible_facts": {
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}

7

unarchive模块

1、将ansible主机上的压缩包解压到远程主机上

[[email protected] ~]# ansible node -m unarchive -a 'src=/root/Music.zip dest=/root/' 

2、将远程主机上的包解压至远程主机特定目录

[[email protected] ~]# ansible node -m unarchive -a 'src=/root/test.zip dest=/root/ copy=no' 

3、将ansible主机上的包解压到node主机且设置权限为644

[[email protected] ~]# ansible node -m unarchive -a 'src=/root/Music.zip dest=/root/ mode=644'

archive模块

将node上的目录进行压缩

[[email protected] ~]# ansible node -m archive -a 'path=/root/at_test dest=/root/at_test.bz2 format=bz2 owner=student mode=0600' 
[[email protected] ~]# ansible node -m archive -a 'path=/root/at_test dest=/root/at_test.tar.bz2 format=bz2 owner=student mode=0600' 
原网站

版权声明
本文为[我是渣渣辉]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_63342921/article/details/126183060

随机推荐