• 快速学会文件操作模块


    blockinfile模块

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

    [root@node1 ~]# echo ansible > ansible_text
    
    • 1

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

    [root@server ~]# 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"
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

    [root@server ~]# 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"
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在blockinfile insert content之前插入insertbefore

    [root@server ~]# 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"
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在blockinfile insert content之后插入 insertafter

    [root@server ~]# 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"
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    lineinfile模块

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

    # cat /root/test  
    123
    234
    345
    456
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    [root@server ~]# ansible node -m lineinfile -a "path=/root/test line='test text' "
    
    
    [root@node1 ~]# cat test
    123
    234
    345
    456
    test text
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

    [root@server ~]# 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"
    }
    
    [root@node1 ~]# cat test
    test text
    234
    345
    456
    test text
    
    [root@server ~]# 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"
    }
    
    [root@node1 ~]# cat test
    test text
    234
    345
    456
    test text
    testtext
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

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

    [root@server ~]# 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": ""
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

    [root@server ~]# 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"
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

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

    [root@server ~]# 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"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

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

    [root@server ~]# 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"
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    7

    unarchive模块

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

    [root@server ~]# ansible node  -m unarchive -a 'src=/root/Music.zip dest=/root/' 
    
    • 1

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

    [root@server ~]# ansible node  -m unarchive -a 'src=/root/test.zip dest=/root/ copy=no'  
    
    • 1

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

    [root@server ~]# ansible node  -m unarchive -a 'src=/root/Music.zip dest=/root/ mode=644'
    
    • 1

    archive模块

    将node上的目录进行压缩

    [root@server ~]# ansible node -m archive -a 'path=/root/at_test dest=/root/at_test.bz2 format=bz2  owner=student mode=0600' 
    
    • 1
    [root@server ~]# ansible node -m archive -a 'path=/root/at_test dest=/root/at_test.tar.bz2 format=bz2  owner=student mode=0600' 
    
    • 1
  • 相关阅读:
    实验报告3:《RHEL7+服务器安装与配置》
    达梦:SQL调优
    GMTSAR软件InSAR时序处理流程
    斐波那契散列和hashMap实践
    如何自学网络安全?零基础入门看这篇就够了(含路线图)_网络安全自学
    机器学习如何做到疫情可视化——疫情数据分析与预测实战
    【配电变电站的最佳位置和容量】基于遗传算法的最优配电变电站放置(Matlab代码实现)
    大腿神经网络解剖图片,大腿神经网络解剖图谱
    Qt中表格属性相关操作,调整表格宽度高度自适应内容等
    SpringCloud-6-pom文件中常用的标签
  • 原文地址:https://blog.csdn.net/m0_63342921/article/details/126183060