ansible模塊文件操做

Ansible經常使用模塊文件操做

[root@tiandong etc]# ansible-doc -l   列出ansible所支持的模塊正則表達式

[root@tiandong ~]# ansible-doc -s ping(模塊名)   能夠查看模塊的詳細信息ssh

 

Ping模塊

[root@tiandong ~]# ansible all -m pingfetch

Fetch模塊。

從遠程主機拉去文件到ansiblespa

[root@tiandong etc]# ansible all -m fetch -a "src=/etc/fstab dest=/testdir/ansible/"3d

                        表示調用fetch模塊   -a選項用於傳遞模塊所須要的參數rest

Copy模塊

複製ansible上的文件文件到遠程主機。regexp

[root@tiandong ansible]# ansible all -m copy -a "src=/testdir/copy dest=/tmp/"blog

在遠程主機/tmp目錄下面生成文件copy遞歸

[root@tiandong ansible]# ansible all -m copy -a "src=/testdir/copy dest=/tmp/ force=no"ip

當遠程主機已經存在複製的文件時,不會執行任何操做

當返回信息爲綠色,’changed’false,表示ansible沒有進行任何操做

[root@tiandong ansible]# ansible all -m copy -a "src=/testdir/copy dest=/tmp/ force=yes"

forceyes的話會執行操做覆蓋以前的文件

[root@tiandong ansible]# ansible all -m copy -a "src=/testdir/copy dest=/tmp/ backup=yes"

在拷貝以前會將源文件重命名已作備份,而後進行復制。

 

[root@tiandong ansible]# ansible all -m copy -a "src=/testdir/copy dest=/tmp/ backup=yes mode=755 owner=tom group=tom"

拷貝文件時制定文件的屬主、屬組、權限

[root@tiandong ansible]# ansible all -m copy -a "content='welcom to beijing' dest=/tmp/test"

在遠程主機上生成文件test,內容爲'welcom to beijing'

在遠程主機上查看文件:

File模塊

能夠進行文件的基本操做,建立(刪除)文件或者目錄、修改文件權限

[root@tiandong ~]# ansible all -m file -a "path=/tmp/test_file state=touch"

在遠程主機上建立test_file的文件。若文件存在會更新文件的時間戳

[root@tiandong ~]# ansible all -m file -a "path=/tmp/test_dir state=directory"

在遠程主機上建立test_dir的目錄,若目錄存在不進行任何操做。

[root@tiandong ~]# ansible all -m file -a "path=/tmp/test_file state=touch mode=755 owner=tom group=tom"

在遠程主機上建立文件指定權限或者,修改屬主或者屬組

[root@tiandong ~]# ansible all -m file -a "path=/tmp/winter state=directory owner=tom group=tom recurse=yes"

操做遠程主機的目錄時,遞歸的將目錄中的文件的屬主屬組設置爲tom

[root@tiandong ~]# ansible all -m file -a "path=/tmp/test_file state=absent"

刪除遠程主機端的文件或者目錄

Blockinfile模塊

[root@tiandong ~]# ansible all -m blockinfile -a "path=/tmp/rc.local block='service restart sshd\nservice restart httpd'"

在文件末尾插入兩行

在遠程主機上查看:

# BEGIN ANSIBLE MANAGED BLOCK   # END ANSIBLE MANAGED BLOCKblockinfile模塊自動添加的文本快標記。

[root@tiandong ~]# ansible all -m blockinfile -a "path=/tmp/rc.local block='service restart iptables' marker='#{mark} service to restart'"

使用marker能夠自定義文本快標記。

#{mark} service to restart這個標記已經存在於文本中:

block對應的內容與以前的內容不一樣,這樣對應的文本塊內容會被更新而不是插入在末尾。

Block對應的內容爲空,刪除對應的文本塊。

[root@tiandong ~]# ansible all -m blockinfile -a "path=/tmp/rc.local marker='#{mark} service to restart' state=absent"  這樣依然能夠刪除對應的文本塊

使用將state的值設置爲absent,刪除對應的文本塊。

默認文本塊是插入在文件末尾的,能夠將文件塊插入指定的位置

[root@tiandong ~]# ansible all -m blockinfile -a "path=/tmp/rc.local block='###blockinfile test###' marker='#{mark} test' insertbefore=BOF"

在文件的開頭插入。

[root@tiandong ~]# ansible all -m blockinfile -a "path=/tmp/rc.local block='###blockinfile test reg###' marker='#{mark} test reg' insertbefore='^touch /var/lock/subsys/local'"

根據正則表達式插入

[root@tiandong ~]# ansible all -m blockinfile -a "path=/tmp/rc.local marker='#{mark} test' state=absent backup=yes"

使用backup參數,在刪除模塊的時候先進行備份在進行刪除。

[root@tiandong ~]# ansible all -m blockinfile -a "path=/tmp/create_test block='create test' marker='#{mark} test' create=yes"

使用create參數,當文件不存在時進行建立

Lineinfile模塊

[root@tiandong ~]# ansible all -m lineinfile -a "path=/tmp/lineinfile_test line='I come to beijing for qq'"

當文件中存在這行時不行任何操做,不存在時在末尾插入

此時不存在,在文件末尾插入該行。

再次插入時就不進行如何操做。

[root@tiandong ~]# ansible all -m lineinfile -a "path=/tmp/lineinfile_test line='tiandong' insertafter='I come to beijing for qq'"

'I come to beijing for qq'以後插入line的內容

[root@tiandong ~]# ansible all -m lineinfile -a "path=/tmp/lineinfile_test line='thunder' insertafter='^tian'"

也能夠使用正則表達式插入行的內容

[root@tiandong ~]# ansible all -m lineinfile -a "path=/tmp/lineinfile_test regexp='^beijing' line='xian'"

根據正則表達式替換某一行,若是有多行匹配到的話只修改最後一個匹配到的行,如果沒有匹配到的話line中的內容就添加到文件的末尾,如果有backrefs=yes這個參數,沒有匹配到的話就不作任何操做

在遠程主機上查看只修改了最後一處被匹配到的地方。

[root@tiandong ~]# ansible all -m lineinfile -a "path=/tmp/lineinfile_test regexp='^thunder' line='tiandong' backrefs=yes"

[root@tiandong ~]# ansible all -m lineinfile -a "path=/tmp/lineinfile_test regexp='^thunder' line='tiandong'"

[root@tiandong ~]# ansible all -m lineinfile -a "path=/tmp/lineinfile_test regexp='^beijing' state=absent"

正則表達式匹配到的行進行刪除。

line內容同樣的進行刪除。

Replace模塊

根據指定的正則表達式替換文件中的字符串,全部匹配到的都會被替換

[root@tiandong ansible]# ansible all -m replace -a 'path=/tmp/test regexp="winter" replace=WINTER'

查看被控制端的文件:

[root@tiandong ansible]# ansible all -m replace -a 'path=/tmp/test regexp="winter" replace=WINTER backup=yes'    該參數在替換以前進行備份

查看被控制端:

相關文章
相關標籤/搜索