指令子產品:
1:command子產品在遠端節點上執行指令:
command子產品後面緊跟要執行的指令,指令的參數以空格隔開。指定的指令會在所選的節點上執行。指令并不是通過shell執行的,是以并不能使用$HOME等環境變量和一些操作符(<,>,|,&).shell子產品可以使用。
1》chdir 在運作指令之前,先切換到指定的目錄。
[root@master ansible]# ansible testhosts -m command -a "ls -l chdir=/tmp"
127.0.0.1 | success | rc=0 >>
total 4
drwx------ 2 root root 4096 Apr 5 21:27 pymp-eBJbzL
192.168.1.112 | success | rc=0 >>
total 4
drwxr-xr-x 2 root root 4096 Apr 2 02:04 rsync
/先切換到/tmp目錄下,然後再執行指令!
2》creates 後邊可以直接指定一個檔案名(目錄名),或者是以正則模式比對一系列檔案名。如果所指定的檔案存在的話,則不執行指定的指令。
[root@master ansible]# ansible 192.168.1.112 -m command -a "ls -l /tmp creates=/tmp/test"
192.168.1.112 | success | rc=0 >>
total 4
drwxr-xr-x 2 root root 4096 Apr 2 02:04 rsync
/tmp/test不存在,是以執行指令
[root@master ansible]# ansible 192.168.1.112 -m command -a "ls -l /tmp creates=/tmp/rsync"
192.168.1.112 | success | rc=0 >>
skipped, since /tmp/rsync exists
/tmp/rsync檔案存在,是以不執行指令
3》removes 後邊可以直接指定一個檔案名(或目錄名),或者是以正則模式比對一系列檔案名。如果所指定的檔案不存在的話,則不運作指令。(注意與creates的對比)
[root@master ~]# ansible 192.168.1.112 -m command -a "ls -l /tmp removes=/tmp/test"
192.168.1.112 | success | rc=0 >>
skipped, since /tmp/test does not exist
[root@master ~]# ansible 192.168.1.112 -m command -a "ls -l /tmp removes=/tmp/rsync"
192.168.1.112 | success | rc=0 >>
total 4
drwxr-xr-x 2 root root 4096 Apr 2 02:04 rsync
[root@master ~]#
/注意與creates的對比
2.script子產品在遠端機器上運作本地腳本。
script子產品的-a選項直接跟一個本地腳本的絕對路徑,腳本的參數以空格隔開。該子產品首先将指定的腳本傳到遠端節點上,然後在遠端節點的shell環境下執行該腳本。
[root@master ~]# ansible 192.168.1.112 -m script -a "/root/test.sh"
192.168.1.112 | success >> {
"changed": true,
"rc": 0,
"stderr": "",
"stdout": "ok\n"
}
/注意的是,這是本地的腳本在遠端執行,這樣執行的時候,腳本檔案必須得有可執行權限/
3.shell子產品,在遠端節點執行指令
shell子產品的參數為指令名稱,指令本身的參數以空格隔開。像command子產品那樣在遠端節點執行指令,但shell子產品再遠端節點是通過shell環境(/bin/bash)執行指令的,該子產品也可以執行一個shell腳本,但該腳本必須在遠端節點上存在。
chdir、creates、removes參數與command子產品的參數一樣。
[root@master ~]# ansible 192.168.1.112 -m shell -a " echo $HOME"
192.168.1.112 | success | rc=0 >>
/root
shell執行腳本檔案
[root@master ~]# ansible 192.168.1.112 -m command -a " echo $HOME"
192.168.1.112 | success | rc=0 >>
/root
[root@master ~]# ansible 192.168.1.112 -m shell -a "/bin/bash /root/test1.sh"
192.168.1.112 | FAILED | rc=127 >>
/bin/bash: /root/test1.sh: No such file or directory
[root@client ~]# vim test1.sh /在遠端節點上建立腳本檔案
[root@client ~]# cat test1.sh
#!/bin/bash
echo oK
[root@client ~]#
[root@master ~]# ansible 192.168.1.112 -m shell -a "/bin/bash /root/test1.sh"
192.168.1.112 | success | rc=0 >>
oK
/sehll執行腳本檔案,腳本檔案必須在遠端節點上存在
看一個簡單的playbook檔案:
[root@master ansible]# vim test.yml
---
- hosts: 192.168.1.112
remote_user: root
tasks:
- name: lianxi module 1
#file on the remote
shell: /bin/bash /root/test1.sh
/playbook腳本是以.yml為字尾的!下面為執行結果
[root@master ~]# cd /etc/ansible
[root@master ansible]# ansible-playbook test.yml
PLAY [192.168.1.112] **********************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.1.112]
TASK: [lianxi module 1] *******************************************************
changed: [192.168.1.112]
PLAY RECAP ********************************************************************
192.168.1.112 : ok=2 changed=1 unreachable=0 failed=0
4.檔案相關子產品:
copy 複制本地檔案到遠端路徑下
copy子產品将本地檔案複制到遠端路徑下。fetch子產品将遠端檔案複制到本地。
copy的選項:
dest 必選參數,為目标檔案指定遠端節點上的一個絕對路徑。如果src是一個目錄,那麼該參數也必須是個目錄。
src 本地檔案的絕對路徑,或者相對路徑。如果是個路徑則會遞歸複制,路徑是以/結尾的話,隻複制目錄裡面的内容,如果不以/幾位的話會複制目
錄本身和裡面的内容。類似Rsync那樣。
backup 可選參數,為源檔案建立一個備份檔案,被給備份檔案添加一個時間戳資訊。值為:yes/no,預設為no。
[root@master ~]# ansible 192.168.1.112 -m copy -a "src=/root/test dest=/root/ backup=yes"
192.168.1.112 | success >> {
"changed": true,
"checksum": "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83",
"dest": "/root/test",
"gid": 0,
"group": "root",
"md5sum": "d8e8fca2dc0f896fd7cb4cb0031ba249",
"mode": "0644",
"owner": "root",
"size": 5,
"src": "/root/.ansible/tmp/ansible-tmp-1459869115.84-179543827108657/source",
"state": "file",
"uid": 0
}
content 可選參數,當使用該參數來代替src的時候,會将内容直接寫入到目标檔案中。
[root@master ~]# ansible 192.168.1.112 -m copy -a "content='test test' dest=/root/test"
192.168.1.112 | success >> {
"changed": true,
"checksum": "abedc47a5ede3fab13390898c5160ec9afbb6ec3",
"dest": "/root/test",
"gid": 0,
"group": "root",
"md5sum": "4f4acc5d8c71f5fbf04dace00b5360c8",
"mode": "0644",
"owner": "root",
"size": 9,
"src": "/root/.ansible/tmp/ansible-tmp-1459869735.21-149102614709149/source",
"state": "file",
"uid": 0
}
檢視一下:
[root@client ~]# cat test
test test[root@client ~]#
directory_mode 可選參數,當遞歸複制的時候,為所建立的目錄設定權限,如果沒有指定則使用系統的預設權限。該參數隻影響新建立的目錄,不會影響已經存在的目錄。
[root@web1 ~]# ansible webservers -m copy -a "src=/root/test/ dest=/root/ directory_mode=0777"
force ,該參數預設值為yes,當遠端檔案與本地檔案内容不一緻的時候會替換遠端檔案。隻有當遠端目标檔案不存在的時候才會傳輸檔案。
group 檔案或目錄的所屬組.
[root@web1 ~]# ansible webservers -m copy -a "src=/root/test/testgroup dest=/root/test1/testgroup group=liuzhenwei"
mode 檔案或目錄的權限,如0644。
[root@web1 ~]# ansible webservers -m copy -a "src=/root/test/testgroup dest=/root/test1/testgroup mode=0755"
看一下copy子產品的傳回值,各代表的意思:
src 要複制到遠端節點的源檔案路徑
backup_file 遠端節點上的備份檔案路徑,backup=yes的時候才有
uid 檔案的所有者ID
dest 目标檔案在遠端節點上的絕對路徑,/root/file.txt
checksum 校驗值
md5sum md5校驗值
state 狀态,如 file
gid 檔案的所屬組ID
mode 檔案的權限