文件管理的模塊非常多,可以參考https://docs.ansible.com/ansible/2.3/list_of_files_modules.html腌零, 包括acl, archive, copy, fetch, file, find, patch, replace, stat, synchronize, unarchive等等,比較常用的有copy, fetch, synchronize這些噩斟。
拷貝一個目錄或文件到目標(biāo)主機(jī)胖烛,腳本如下:
---
# synchronize
- hosts: axtestubuntu
tasks:
- name: copy local folder to remote host
copy: src=/home/axing/ansible/axtest/ dest=/home/axing/axtest/
運(yùn)行一下,可以看到诫惭,.69的主機(jī)先沒有axtest目錄,運(yùn)行劇本后就多了這個目錄:
axing@ax:~/ansible$ ansible axtestubuntu -m command -a "ls /home/axing/"
xx.xxx.xxx.69 | CHANGED | rc=0 >>
sources.list
axing@ax:~/ansible$ ansible-playbook synch.yml
PLAY [axtestubuntu] **********************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [xx.xxx.xxx.69]
TASK [copy local folder to remote host] **************************************************************
changed: [xx.xxx.xxx.69]
PLAY RECAP *******************************************************************************************
xx.xxx.xxx.69 : ok=2 changed=1 unreachable=0 failed=0
axing@ax:~/ansible$ ansible axtestubuntu -m command -a "ls /home/axing/"
xx.xxx.xxx.69 | CHANGED | rc=0 >>
axtest
sources.list
現(xiàn)在從.69的主機(jī)取得source.list文件蔓挖,同步到.99的主機(jī)上夕土,腳本如下:
---
- hosts: axtestubuntu
tasks:
- name: Fetch the file from the server69
run_once: yes
fetch: src=/home/axing/sources.list dest=/home/axing/ansible/axtest/ flat=yes
- hosts: axtestubuntu
tasks:
- name: Copy the file from master to axtest99
copy: src=/home/axing/ansible/axtest/sources.list dest=/home/axing/
運(yùn)行之,可以看到:
axing@ax:~/ansible$ ansible-playbook syn2.yml
PLAY [axtestubuntu] **********************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [xx.xxx.xxx.69]
TASK [Fetch the file from the server69] **************************************************************
changed: [xx.xxx.xxx.69]
PLAY [axtest99] **************************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [xx.xxx.xxx.99]
TASK [Copy the file from master to axtest99] *********************************************************
changed: [xx.xxx.xxx.99]
PLAY RECAP *******************************************************************************************
xx.xxx.xxx.69 : ok=2 changed=1 unreachable=0 failed=0
xx.xxx.xxx.99 : ok=2 changed=1 unreachable=0 failed=0
axing@ax:~/ansible$ ansible axtest99 -m command -a "ls /home/axing/"
xx.xxx.xxx.99 | CHANGED | rc=0 >>
sources.list
也可以直接使用命令行:
1、從目標(biāo)主機(jī)拷貝目錄到本機(jī)(會連目錄一起拷過來怨绣,就是會生成目錄:/tmp/xx.xxx.xxx.69/home/axing)角溃,如果不想創(chuàng)建目錄結(jié)構(gòu)的話加參數(shù)flat=yes
ansible axtestubuntu -m fetch -a "src=/home/axing/sources.list dest=/tmp/ mode=0600 owner=axing group=axing flat=yes"
2、從本地拷貝目錄到目標(biāo)主機(jī)
ansible axtest99 -m copy -a "src=/home/axing/ansible/axtest/sources.list dest=/tmp/ mode=0600 owner=axing group=axing "
3篮撑、目標(biāo)主機(jī)文件移動(remote_src=true參數(shù))
ansible axtestubuntu -m copy -a "remote_src=true src=/home/axing/sources.list dest=/tmp/ mode=0600 owner=axing group=axing "
使用 synchronize模塊减细,ansible和目標(biāo)主機(jī)必須都安裝運(yùn)行rsync軟件包,劇本如下赢笨, 其中mode的push和pull分別代表上傳和拷貝
---
# synchronize
- name: sync local folder to remote host
hosts: axtestubuntu
tasks:
- name: push local folder to remote host
synchronize:
mode: push
src: /home/axing/ansible/axtest/
dest: /home/axing/axtest/
寫成命令行是:
ansible antest99 -m synchronize -a 'src=sources.list dest=/tmp/'