ansible testhost -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0755"
注意:源目錄會放到目標目錄下面去,如果
目標指定的目錄不存在,它會自動創建。如果拷貝的是文件,dest指定的名字和源如果不同,並且它不是已經存在的目錄,相當於拷貝過去后又重命名。但相反,如
果dest是目標機器上已經存在的目錄,則會直接把文件拷貝到該目錄下面。
ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/1.txt"
這里的/tmp/123和源機器上的/etc/passwd是一致的,但如果目標機器上已經有/tmp/123目錄,則會再/tmp/123目錄下面建立passwd文件
Ansible遠程執行腳本
首先創建一個shell腳本
vim /tmp/test.sh //加入內容
#!/bin/bash
echo `date` > /tmp/ansible_test.txt
然后把該
腳本分發到各個機器上
ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
最后是
批量執行該shell腳本
ansible testhost -m shell -a "/tmp/test.sh"
shell模塊,
還支持遠程執行命令並且帶管道
ansible testhost -m shell -a "cat /etc/passwd|wc -l "
Ansible
實現任務計划
ansible web10.aming.com -m cron -a "name=test_cron job='/bin/touch /tmp/1212.txt' weekday=6"
crontab -l 客戶端查看
若要刪除該cron 只需要加一個字段 state=absent
ansible testhost -m cron -a "name=test_cron state=absent"
其他的時間表示:分鍾 minute 小時 hour 日期 day 月份 month
Ansibl
e安裝rpm包/管理服務
ansible testhost -m yum -a "name=httpd "
在name后面還可以加上state=installed
ansible testhost -m service -a "name=httpd state=started enabled=yes"
這里的name是centos系統里的服務名,可以通過chkconfig --list查到。
Ansible文檔的使用
ansible-doc -l 列出所有的模塊
ansible-doc cron 查看指定模塊的文檔
Ansible playbook的使用
相當於把模塊寫入到配置文件里面,例:
vim /etc/ansible/test.yml
---
- hosts:
web10.aming.com
remote_user: root
tasks:
- name: test_playbook
shell: touch /tmp/lishiming.txt
說明: hosts參數指定了對哪些主機進行參作;
user參數指定了使用什么用戶登錄遠程主機操作;
tasks指定了一個任務,其下面的name參數同樣是對任務的描述,在執行過程中會打印出來。
執行:ansible-playbook test.yml
再來一個創建用戶的例子:
vim /etc/ansible/create_user.yml
---
- name: create_user
hosts: web10.aming.com
user: root
gather_facts: false
vars:
- user: "test"
tasks:
- name: create user
user: name="{{ user }}"
說明: name參數對該playbook實現的功能做一個概述,后面執行過程中,會打印 name變量的值 ,可以省略;gather_facts參數指定了在以下任務部分執行前,是否先執行setup模塊獲取主機相關信息,這在后面的task會使用到setup獲取的信息時用到;vars參數,指定了變量,這里指字一個user變量,其值為test ,需要注意的是,變量值一定要用引號引住;user提定了調用user模塊,name是user模塊里的一個參數,而增加的用戶名字調用了上面user變量的值。
客戶端查看 創建的用戶test grep test /etc/passwd
Ansible playbook中的循環
---
- hosts: testhost
user: root
tasks:
- name: change mode for files
file: path=/tmp/{{ item }} mode=600
with_items:
- 1.txt
- 2.txt
- 3.txt
需要
在web9, web10 機器上先創建文件 touch /tmp/{1.txt,2.txt,3.txt} 才能執行修改權限操作。
Copy模塊
copy模塊
copy模塊在ansible里的角色就是把ansible執行機器上的文件拷貝到遠程節點上。
與fetch模塊相反的操作。
常用模塊參數
參數名 | 是否必須 | 默認值 | 選項 | 說明 |
---|---|---|---|---|
src | no | 用於定位ansible執行的機器上的文件,需要絕對路徑。如果拷貝的是文件夾,那么文件夾會整體拷貝,如果結尾是”/”,那么只有文件夾內的東西被考過去。一切的感覺很像rsync | ||
content | no | 用來替代src,用於將指定文件的內容,拷貝到遠程文件內 | ||
dest | yes | 用於定位遠程節點上的文件,需要絕對路徑。如果src指向的是文件夾,這個參數也必須是指向文件夾 | ||
backup | no | no | yes/no | 備份遠程節點上的原始文件,在拷貝之前。如果發生什么意外,原始文件還能使用。 |
directory_mode | no | 這個參數只能用於拷貝文件夾時候,這個設定后,文件夾內新建的文件會被拷貝。而老舊的不會被拷貝 | ||
follow | no | no | yes/no | 當拷貝的文件夾內有link存在的時候,那么拷貝過去的也會有link |
force | no | yes | yes/no | 默認為yes,會覆蓋遠程的內容不一樣的文件(可能文件名一樣)。如果是no,就不會拷貝文件,如果遠程有這個文件 |
group | no | 設定一個群組擁有拷貝到遠程節點的文件權限 | ||
mode | no | 等同於chmod,參數可以為“u+rwx or u=rw,g=r,o=r” | ||
owner | no | 設定一個用戶擁有拷貝到遠程節點的文件權限 |
案例
# 把/srv/myfiles/foo.conf文件拷貝到遠程節點/etc/foo.conf,並且它的擁有者是foo,擁有它的群組是foo,權限是0644
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644 # 跟上面的案例一樣,不一樣的只是權限的寫法 - copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode="u=rw,g=r,o=r" # 另外一個權限的寫法 - copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode="u+rw,g-wx,o-rwx" # 拷貝/mine/ntp.conf到遠程節點/etc/ntp.conf,並且備份遠程節點的/etc/ntp.conf。 - copy: src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes
常用參數返回值
參數名 | 參數說明 | 返回值 | 返回值類型 | 樣例 |
---|---|---|---|---|
src | 位於ansible執行機上的位置 | changed | string | /home/httpd/.ansible/tmp/ansible-tmp-1423796390.97-147729857856000/source |
backup_file | 將原文件備份 | changed and if backup=yes | string | /path/to/file.txt.2015-02-12@22:09~ |
uid | 在執行后,擁有者的ID | success | int | 100 |
dest | 遠程節點的目標目錄或文件 | success | string | /path/to/file.txt |
checksum | 拷貝文件后的checksum值 | success | string | 6e642bb8dd5c2e027bf21dd923337cbb4214f827 |
md5sum | 拷貝文件后的md5 checksum值 | when supported | string | 2a5aeecc61dc98c4d780b14b330e3282 |
state | 執行后的狀態 | success | string | file |
gid | 執行后擁有文件夾、文件的群組ID | success | int | 100 |
mode | 執行后文件的權限 | success | string | 0644 |
owner | 執行后文件所有者的名字 | success | string | httpd |
group | 執行后文件所有群組的名字 | success | string | httpd |
size | 執行后文件大小 | success | int | 1220 |