Ansible常用模塊文件操作
[root@tiandong etc]# ansible-doc -l 列出ansible所支持的模塊
[root@tiandong ~]# ansible-doc -s ping(模塊名) 可以查看模塊的詳細信息
Ping模塊
[root@tiandong ~]# ansible all -m ping
Fetch模塊。
從遠程主機拉去文件到ansible
[root@tiandong etc]# ansible all -m fetch -a "src=/etc/fstab dest=/testdir/ansible/"
表示調用fetch模塊 -a選項用於傳遞模塊所需要的參數
Copy模塊
復制ansible上的文件文件到遠程主機。
[root@tiandong ansible]# ansible all -m copy -a "src=/testdir/copy dest=/tmp/"
在遠程主機/tmp目錄下面生成文件copy
[root@tiandong ansible]# ansible all -m copy -a "src=/testdir/copy dest=/tmp/ force=no"
當遠程主機已經存在復制的文件時,不會執行任何操作
當返回信息為綠色,’changed’為false,表示ansible沒有進行任何操作
[root@tiandong ansible]# ansible all -m copy -a "src=/testdir/copy dest=/tmp/ force=yes"
若force為yes的話會執行操作覆蓋之前的文件
[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 BLOCK是blockinfile模塊自動添加的文本快標記。
[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' 該參數在替換之前進行備份
查看被控制端: