• 快速学会文件操作模块


    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
  • 相关阅读:
    SpringCloud Alibaba学习
    微信小程序-发送Get/Post请求
    MPX 小程序框架 - Tracy 小笔记
    华为云14天鸿蒙设备开发-Day9网络应用开发
    【蓝桥杯选拔赛真题48】python最小矩阵 青少年组蓝桥杯python 选拔赛STEMA比赛真题解析
    SpringBoot文件上传
    Go :测试编译器诊断函数缺少返回语句(附完整源码)
    MongDB介绍及其下载地址
    我们为何看好投资 DAO?
    计算机毕业设计Java城市交通海量数据管理系统(源码+系统+mysql数据库+lw文档)
  • 原文地址:https://blog.csdn.net/m0_63342921/article/details/126183060