天天看點

ansible檔案操作子產品<6>find子產品replace子產品:

前面我對ansible的fetch子產品,copy,file,blockinfile,lineinfile進行了學習,這次再次跟随 http://www.zsythink.net/archives/2560 學習ansible的其它子產品

find子產品

類似于我們的find指令,可以幫助我們找到受管主機中符合條件的檔案或者字元

先來看一下參數:

ansible檔案操作子產品<6>find子產品replace子產品:

ansible test177 -m find -a 'path=/root/ contains=".love."'

在我的177上的root目錄下查找包含字元串‘love’的檔案,不會遞歸查找,隻會在此目錄下找對應的檔案

ansible檔案操作子產品<6>find子產品replace子產品:
ansible檔案操作子產品<6>find子產品replace子產品:

假如我要找的檔案在二級目錄或者三級目錄下,那麼我就需要用到下面這條指令:

ansible test177 -m find -a 'paths=/root/ contains=".love." recurse=yes'

在root的子目錄下找關鍵詞所對應的檔案,但是不包含隐藏檔案:

ansible檔案操作子產品<6>find子產品replace子產品:

ansible test177 -m find -a 'paths=/root/ patterns="*.sh" file_type=any hidden=yes'

在root目錄下找出所有以.sh結尾的檔案,包括隐藏檔案,檔案類型不限,但不進行遞歸查找:

ansible檔案操作子產品<6>find子產品replace子產品:

ansible test177 -m find -a 'path=/root/ age=-4d recurse=yes'

在root下找出四天内的檔案,不包含隐藏檔案,目錄和軟連接配接檔案類型

ansible檔案操作子產品<6>find子產品replace子產品:

ansible test177 -m find -a 'path=/root/ size=2k recurse=yes'

在177的root下找出大于2k的檔案,不包含隐藏檔案,目錄或者軟連接配接等檔案類型

replace子產品:

ansible檔案操作子產品<6>find子產品replace子產品:

很明顯。replace是作為ansible的一個替換功能的子產品,會将你指定的字元替換成你想要的新字元

ansible test177 -m replace -a 'path=/root/file1 regexp="love" replace=loving'

将177的file1 檔案中的‘love’字元替換成‘loving’

ansible檔案操作子產品<6>find子產品replace子產品:

ansible test177 -m replace -a 'path=/root/file1 regexp="loving" replace=love backup=yes'

把177的file1中的loving替換成love,但是在替換之前進行源檔案備份

ansible檔案操作子產品<6>find子產品replace子產品: