Linux基礎命令 ansible 和文件復制相關的幾個模塊
發布時間:2021-06-17
ansible 和文件復制相關的幾個模塊
ansible 復制遠程主機的文件到本地主機——fetch
例子1:
[root@centos_7 ansible]# cat fetched.yml
----
hosts: centos7
remote_user: root tasks:
- name : fetched file from centos7 into local
fetch:
src: /tmp/fstab_from_centos6
dest: /tmp/centos
說明:src - 是遠程主機的文件,這個參數只能使用文件,不能使用目錄
dest - 是本地主機,用來保存文件的目錄。它會將這里的參數看成是目錄。
注意,在執行ansible 成功后,dest 的路徑會自動將遠程文件保存到 /tmp/centos/remote_host/tmp/fstab_from_centos6
上面例子的結果:
[root@centos_7 ansible]# ls /tmp/centos/example.fetch.host/tmp
fstab_from_centos6
example.fetch.host 是在 /etc/ansible/hosts 主機列表里 centos7 對應的名稱
例子2:保存到本地時,直接使用遠程的文件名,不需要remote_host 創建新的目錄
[root@centos_7 ansible]# cat fetched.yml
----
hosts: centos7
remote_user: root
tasks:
- name : fetched file from centos7 into local
fetch:
src: /tmp/fstab_from_centos6
dest: /tmp/
flat: yes
增加 flat 參數,當flat 為 yes 時,有兩種情況:
(1)dest 是以"/" 結尾,文件直接保存在 dest 的目錄下,所以現在的結果是:fstab_from_centos6 保存在本地主機的 /tmp/fstab_from_centos6
(2) dest 不是以 "/" 結尾,則dest 被看做文件,相當于 fstab_from_centos6 會重命名再保存。
例如:[root@centos_7 ansible]# cat fetched.yml
----
hosts: centos7
remote_user: root
tasks:
- name : fetched file from centos7 into local
fetch:
src: /tmp/fstab_from_centos6
dest: /tmp/centos
flat: yes
fstab_from_centos6 被保存為 /tmp/cnetos
復制本地(或遠程)文件 到遠程主機
# Example from Ansible Playbooks
- copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: 0644
# The same example as above, but using a symbolic mode equivalent to 0644
- copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: u=rw,g=r,o=rfile
模塊file 模塊用來修改文件、目錄和符號文件的屬性權限等。利用它的 state 參數,有更多豐富的用法。
增加 flat 參數,當flat 為 yes 時,有兩種情況:
(1)dest 是以"/" 結尾,文件直接保存在 dest 的目錄下,所以現在的結果是:fstab_from_centos6 保存在本地主機的 /tmp/fstab_from_centos6
(2) dest 不是以 "/" 結尾,則dest 被看做文件,相當于 fstab_from_centos6 會重命名再保存。
例如:[root@centos_7 ansible]# cat fetched.yml
----
hosts: centos7
remote_user: root
tasks:
- name : fetched file from centos7 into local
fetch:
src: /tmp/fstab_from_centos6
dest: /tmp/centos
flat: yes
fstab_from_centos6 被保存為 /tmp/cnetos
復制本地(或遠程)文件 到遠程主機
# Example from Ansible Playbooks
- copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: 0644
# The same example as above, but using a symbolic mode equivalent to 0644
- copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: u=rw,g=r,o=rfile
模塊file 模塊用來修改文件、目錄和符號文件的屬性權限等。利用它的 state 參數,有更多豐富的用法。
修改權限屬性:
[root@centos_7 ansible]# ansible centos6 -m command -a "chdir=/tmp/new/ ls 1.txt -l"
192.168.188.109 | SUCCESS | rc=0 >>-rw-r--r-- 1 root root 0 Jan 16 12:22 1.txt
[root@centos_7 ansible]# ansible centos6 -m file -a "path=/tmp/new/1.txt mode=0777"
192.168.188.109 | SUCCESS =>
{ "changed": true, "gid": 0, "group": "root", "mode": "0777", "owner": "root",
"path": "/tmp/new/1.txt", "size": 0, "state": "file", "uid": 0}
[root@centos_7 ansible]# ansible centos6 -m command -a "chdir=/tmp/new/ ls 1.txt -l"
192.168.188.109 | SUCCESS | rc=0 >>-rwxrwxrwx 1 root root 0 Jan 16 12:22 1.txt
說明: 將遠程主機 /tmp/new/1.txt 的文件修改權限屬性為0777;
ansible 命令中,path 相當于 dest,指明了目標文件。如果想修改所屬主和所屬組,
可以:ansible centos6 -m file -a "path=/tmp/new/1.txt mode=0755 ower=apple group=apple"
命令執行結果:
[root@centos_7 ansible]# ansible centos6 -m file -a "path=/tmp/new/1.txt mode=0755 owner=apple group=apple"
192.168.188.109 | SUCCESS => { "changed": true, "gid": 508, "group": "apple", "mode": "0755", "owner": "apple", "path": "/tmp/new/1.txt", "size": 0, "state": "file", "uid": 508}
[root@centos_7 ansible]# ansible centos6 -m command -a "chdir=/tmp/new/ ls 1.txt -l"
192.168.188.109 | SUCCESS | rc=0 >>-rwxr-xr-x 1 apple apple 0 Jan 16 12:22 1.txt
說明: 只要增加 owner 和 group 參數就可以了。
file 的 state 參數
熟悉 state 的幾個參數,基本就滿足我們平時對文件、目錄和符號鏈接文件的操作了。先來看看 state 有幾個值?
state 的值和我們要進行的操作,其實是一一對應的。
1. state=directory -- 如果想創建一個目錄
[root@centos_7 ansible]# ansible test -m file -a "path=/tmp/testdir state=directory"
192.168.188.109 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/testdir", "size": 4096, "state": "directory", "uid": 0}
可以看到,在遠程主機已經創建了 /tmp/testdir 目錄:
[root@centos_7 ansible]# ansible test -m command -a "chdir=/tmp ls -lthr"
192.168.188.109 | SUCCESS | rc=0 >>
total 16K
-rw-r--r-- 1 root root 484 Jan 12 15:08 fstab
drwxr-xr-x 2 root root 4.0K Jan 16 12:22 new
drwxr-xr-x 2 root root 4.0K Jan 16 14:50 testdir
drwx------ 2 root root 4.0K Jan 16 14:51 ansible_IOZLxg
2. state=touch -- 創建文件
[root@centos_7 ansible]# ansible test -m file -a "path=/tmp/new/1.txt state=touch"
[root@centos_7 ansible]# ansible test -m command -a "chdir=/tmp/new ls -lthr"
192.168.188.109 | SUCCESS | rc=0 >>
total 0-rw-r--r-- 1 root root 0 Jan 16 15:00 1.txt
注意: 使用 state=file ,當 path 指定的文件不存在的時候,并不能新創建文件。
重復對相同的文件使用 touch 只能改變文件的元數據屬性,例如訪問時間等等 stat 查看到的數據。
3. state=link -- 創建符號鏈接
當stat=link 的時候,就需要使用另一個 src 的參數一起搭配使用了。
[root@centos_7 ansible]# ansible test -m file -a "path=/tmp/new/1_link.txt src=/tmp/new/1.txt state=link"
結果:
[root@centos_7 ansible]# ansible test -m command -a "ls /tmp/new -l"
192.168.188.109 | SUCCESS | rc=0 >>
total 0
lrwxrwxrwx 1 root root 14 Jan 16 15:25 1_link.txt -> /tmp/new/1.txt
-rw-r--r-- 1 root root 0 Jan 16 15:02 1.txt
說明: path 指定了生成的符號鏈接文件的路徑, src 指定了 被鏈接的文件是什么。
4. state=hard -- 創建硬鏈接
[root@centos_7 ansible]# ansible test -m file -a "path=/tmp/new/1_hard.txt src=/tmp/new/1.txt state=hard"
[root@centos_7 ansible]# ansible test -m command -a "ls -l /tmp/new "
192.168.188.109 | SUCCESS | rc=0 >>
total 0
-rw-r--r-- 2 root root 0 Jan 16 15:02 1_hard.txt
lrwxrwxrwx 1 root root 14 Jan 16 15:25 1_link.txt -> /tmp/new/1.txt
-rw-r--r-- 2 root root 0 Jan 16 15:02 1.txt
可以看到,1.txt 和 1_hard.txt 文件的屬性都是一樣的。
5. state=absent -- 刪除目錄、文件和符號鏈接
[root@centos_7 ansible]# ansible test -m file -a "path=/tmp/new/1_link.txt state=absent"
這樣就刪除了一個符號鏈接文件。
template 模塊
template 模塊官網說明:template使用了Jinja2格式作為文件模版,進行文檔內變量的替換的模塊。
它的每次使用都會被ansible標記為”changed”狀態。通俗來講,就是在本地 file.j2 文件里的變量狀態。
例如在file.j2 文件里有變量: username="{{ admin_username }}" password="{{ admin_password }}"。
那么,file.j2 文件會一直保持變量狀態,直到 file.j2 文件被 ansible 的 template 模塊執行后,文件里的變量就會被具體的值代替,
并且傳送到遠程主機。例如,在roles 里面調用了 template 模塊,那么ansible 會在 roles 同級目錄 global_vars 里找到存儲變量的文件,
或者有時會在 roles 目錄里的 vars 目錄定義變量的文件,然后,找到對應變量的值,再將變量傳遞給 file.j2 文件.
