ansible笔记-(4)-常用模块-文件操作

2019-07-30  本文已影响0人  张都尉

copy模块

file模块使用方法

操作演示

  ansible 192.168.3.41 -m file -a "path=/testdir/testfile state=touch"
ansible 192.168.3.41 -m file -a "path=/testdir/testdir state=directory"   
 ansible 192.168.3.41 -m file -a "path=/testdir/linkfile state=link src=/testdir/testfile"
 ansible 192.168.3.41 -m file -a "path=/testdir/hardfile state=link src=/testdir/testfile"
ansible 192.168.3.41 -m file -a "path=/testdir/linkfile state=link src=sourcefile force=yes"
ansible 192.168.3.41 -m file -a "path=/testdir/testdir state=absent"
 ansible 192.168.3.41 -m file -a "path=/testdir/abc state=touch owner=zsy"
 ansible 192.168.3.41 -m file -a "path=/testdir/abc owner=zsy"
 ansible 192.168.3.41 -m file -a "path=/testdir/abc state=directory owner=zsy"
 ansible 192.168.3.41 -m file -a "path=/testdir/abb state=touch group=zsy"
 ansible 192.168.3.41 -m file -a "path=/testdir/abb group=zsy"
 ansible 192.168.3.41 -m file -a "path=/testdir/abb state=directory group=zsy"
ansible 192.168.3.41 -m file -a "path=/testdir/abb state=touch mode=0644"
ansible 192.168.3.41 -m file -a "path=/testdir/abb mode=0644"
ansible 192.168.3.41 -m file -a "path=/testdir/binfile mode=4700"
ansible 192.168.3.41 -m file -a "path=/testdir/abb state=directory mode=0644"
ansible 192.168.3.41 -m file -a "path=/testdir/abc state=directory owner=zsy group=zsy recurse=yes"

blockinfile模块

blockinfile模块可以帮助我们在指定的文件中插入一段“文本”,这段文件是被标记的,换句话说就是,我们在这段文件上做了记号,以便在以后的操作可以通过“标记”找到这段文本。然后进行修改或删除。单靠这样的描述不容易理解,结合下面的示例应该就很快的明白了。

blockinfile常用参数

实际操作举例

为方便举例,我们将/etc/rc.d/rc.local文件复制到/testdir目录中去,以做测试 :cp cp /etc/rc.d/rc.local /testdir/
我们在/testdir/rc.local文件末尾添加两行命令

 #BEGIN service to start
 systemctl start nginx
 systemclt start httpd
 #END service to start
ansible 192.168.3.41 -m blockinfile -a 'path=/testdir/rc.local block="" marker="#{mark} serivce to start" '
ansible 192.168.3.41 -m blockinfile -a 'path=/testdir/rc.local  marker="#{mark} service to start" state=absent'
ansible 192.168.3.41 -m blockinfile -a 'path=/testdir/rc.local block="####blockinfile test####"  marker="#{mark} ansible" i
nsertbefore=BOF'
执行成功
验证
 ansible 192.168.3.41 -m blockinfile -a 'path=/testdir/rc.local block="####blockinfile test####"  marker="#{mark} ansible" i
nsertbefore=EOF'
EOF
验证结果
mark标记字段也可以为中文
中文字段
验证中文字段
 ansible 192.168.3.41 -m blockinfile -a 'path=/testdir/rc.local block="####ansible blockinfile test####"  marker="#{mark} test reg" insertafter="^#!/bin/bash" '
验证
ansible 192.168.3.41 -m blockfile -a 'path=/testdir/rc.local marker="#{mark} ansible" state=absent backup=yes'
执行结果
验证
ansible 192.168.3.41 -m blockinfile -a 'path=/testdir/test block="ansible" marker="#{mark} ansible" create=yes'
creat参数示例

lineinfile模块

我们可以借用lineinfile模块,确保“某一行文本”存在于指定的文件中,或者确保从文件中删除指定文本(即确保指定的文本不存在于文件中)。还可以根据正则表达式,替换“某一行文件” 通俗的讲这个相当于liunx的sed命令。
接下来我们看一下lineinfile模块常用的一些参数

lineinfile示例操作

我们新建立files文件

# cat /testdir/test
Hello ansible,Hiiii
lineinfile -
Ensure a particular line is in a file,
lineinfile -
or replace an existing line using a back-referenced regular expression.

ansible 192.168.3.41 -m lineinfile -a 'path=/testdir/test line="test text"'
验证示例
ansible 192.168.3.41 -m lineinfile -a 'path=/testdir/test regexp="^line" line="test text" '
验证
ansible 192.168.3.41 -m lineinfile -a 'path=/testdir/test regexp="^line" line="test text" backrefs=yes '
ansible 192.168.3.41 -m lineinfile path=/testdir/test line="lineinfile -" state=absent'
ansible 192.168.3.41 -m  lineinfile -a 'path=/testdir/test regexp="^lineinfile" state=absent'
上一篇 下一篇

猜你喜欢

热点阅读