快速学会文件操作模块

blockinfile模块

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

[root@node1 ~]# echo ansible > ansible_text

然后使用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"
}

然后插入内容 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"
}

在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"
}

在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"
}

lineinfile模块

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

# cat /root/test  
123
234
345
456

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

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

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": ""
}

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"
}

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

[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"
}

如果将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"
}

7

unarchive模块

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

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

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

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

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

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

archive模块

将node上的目录进行压缩

[root@server ~]# ansible node -m archive -a 'path=/root/at_test dest=/root/at_test.bz2 format=bz2  owner=student mode=0600' 
[root@server ~]# ansible node -m archive -a 'path=/root/at_test dest=/root/at_test.tar.bz2 format=bz2  owner=student mode=0600' 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值