天天看點

Ansible入門

Ansible簡介

ansible是新出現的自動化運維工具,基于Python開發,集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優點,實作了批量系統配置、批量程式部署、批量運作指令等功能。ansible是基于子產品工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運作的子產品,ansible隻是提供一種架構。主要包括:

(1)、連接配接插件connection plugins:負責和被監控端實作通信;

(2)、host inventory:指定操作的主機,是一個配置檔案裡面定義監控的主機;

(3)、各種子產品核心子產品、command子產品、自定義子產品;

(4)、借助于插件完成記錄日志郵件等功能;

(5)、playbook:劇本執行多個任務時,非必需可以讓節點一次性運作多個任務。

通過Apt (Ubuntu)安裝最新釋出版本

root@storage:~# apt-add-repository ppa:ansible/ansible

 Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.

http://ansible.com/

 More info: https://launchpad.net/~ansible/+archive/ubuntu/ansible

Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmpi636boen/secring.gpg' created

gpg: keyring `/tmp/tmpi636boen/pubring.gpg' created

gpg: requesting key 7BB9C367 from hkp server keyserver.ubuntu.com

gpg: /tmp/tmpi636boen/trustdb.gpg: trustdb created

gpg: key 7BB9C367: public key "Launchpad PPA for Ansible, Inc." imported

gpg: Total number processed: 1

gpg:               imported: 1  (RSA: 1)

OK

root@storage:~# apt-get update -y

root@storage:~# apt-get install ansible -y

配置 無密碼通路 

root@storage:/etc/ansible# ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:rSrNWrBMLzr7gUUqVZZUQU+bUYiVVoqev37/r3Xfmck root@storage

The key's randomart p_w_picpath is:

+---[RSA 2048]----+

|   .+++++=o      |

|   o. .++=       |

|  . . ..=        |

| . o . . .       |

|. . + o S .      |

| . = + . .       |

|  . =oo o       o|

|  ...+o. ..   ..B|

|  o+oo..o. ...oE+|

+----[SHA256]-----+

root@storage:/etc/ansible# ssh-copy-id 192.168.13.143

root@storage:/etc/ansible# ssh-copy-id 192.168.13.144

root@storage:~# vim /etc/ansible/hosts

192.168.13.143

192.168.13.144

[openstack]          #主機組:openstack

192.168.13.30

192.168.13.33

192.168.13.45

簡單測試1

root@storage:~# ansible all -m ping

192.168.13.143 | SUCCESS => {

    "changed": false,

    "ping": "pong"

}

192.168.13.144 | SUCCESS => {

簡單測試2

root@storage:~# ansible all -m command -a 'uptime'

192.168.13.144 | SUCCESS | rc=0 >>

 07:39:00 up  6:02,  2 users,  load average: 0.00, 0.00, 0.00

192.168.13.143 | SUCCESS | rc=0 >>

 07:39:00 up  5:58,  2 users,  load average: 0.06, 0.03, 0.01

root@storage:~# ansible openstack -m command -a 'uptime'

192.168.13.33 | SUCCESS | rc=0 >>

 15:43:18 up 4 days,  1:38,  1 user,  load average: 0.30, 0.26, 0.30

192.168.13.45 | SUCCESS | rc=0 >>

 15:43:27 up 13 days,  1:08,  1 user,  load average: 0.12, 0.08, 0.08

192.168.13.30 | SUCCESS | rc=0 >>

 15:43:27 up 26 days,  1:39,  2 users,  load average: 10.40, 10.49, 10.24

root@storage:~#

常用子產品介紹

1、setup:用來檢視遠端主機的一些基本資訊

root@storage:~# ansible openstack -m setup |more

Ansible入門
Ansible入門

2、ping: 用來測試遠端主機的運作狀态

root@storage:~# ansible openstack -m ping

Ansible入門
Ansible入門

3、file:設定檔案的屬性

     file選項如下:

force:需要在兩種情況下強制建立軟連結,一種是源檔案不存在,但之後會建立的情況下;另一種是目标軟連結已存在,需要先取消之前的軟鍊,然後建立新的軟鍊,有兩個選項:yes|no

group:定義檔案/目錄的屬組

mode:定義檔案/目錄的權限

owner:定義檔案/目錄的屬主

path:必選項,定義檔案/目錄的路徑

recurse:遞歸設定檔案的屬性,隻對目錄有效

src:被連結的源檔案路徑,隻應用于state=link的情況

dest:被連結到的路徑,隻應用于state=link的情況

state:

       directory:如果目錄不存在,就建立目錄

       file:即使檔案不存在,也不會被建立

       link:建立軟連結

       hard:建立硬連結

       touch:如果檔案不存在,則會建立一個新的檔案,如果檔案或目錄已存在,則更新其最後修改時間

       absent:删除目錄、檔案或者取消連結檔案

root@storage:# ansible openstack -m file  -a 'src=/etc/resolv.conf dest=/home/resolv.conf state=link'

192.168.13.33 | SUCCESS => {

    "changed": true,

    "dest": "/home/resolv.conf",

    "gid": 0,

    "group": "root",

    "mode": "0777",

    "owner": "root",

    "size": 16,

    "src": "/etc/resolv.conf",

    "state": "link",

    "uid": 0

  檢視執行結果:-m command 在遠端執行指令

root@storage:~# ansible openstack -m command -a 'ls /home'

andy

resolv.conf

root@storage:~# ansible openstack -m file -a 'path=/home/resolv.conf state=absent'

192.168.13.45 | SUCCESS => {

    "path": "/home/resolv.conf",

    "state": "absent"

4、copy:複制檔案到遠端主機

相關選項如下:

backup:在覆寫之前,将源檔案備份,備份檔案包含時間資訊。有兩個選項:yes|no

content:用于替代“src”,可以直接設定指定檔案的值

dest:必選項。要将源檔案複制到的遠端主機的絕對路徑,如果源檔案是一個目錄,那麼該路徑也必須是個目錄

directory_mode:遞歸設定目錄的權限,預設為系統預設權限

force:如果目标主機包含該檔案,但内容不同,如果設定為yes,則強制覆寫,如果為no,則隻有當目标主機的目标位置不存在該檔案時,才複制。預設為yes

others:所有的file子產品裡的選項都可以在這裡使用

src:被複制到遠端主機的本地檔案,可以是絕對路徑,也可以是相對路徑。如果路徑是一個目錄,它将遞歸複制。在這種情況下,如果路徑使用“/”來結尾,則隻複制目錄裡的内容,如果沒有使用“/”來結尾,則包含目錄在内的整個内容全部複制,類似于rsync。

root@storage:~# ansible openstack -m copy -a 'src=/root/aa.txt dest=/tmp'

Ansible入門
Ansible入門

5、command:在遠端主機上執行指令

creates:一個檔案名,當該檔案存在,則該指令不執行

free_form:要執行的linux指令

chdir:在執行指令之前,先切換到該目錄

removes:一個檔案名,當該檔案不存在,則該選項不執行

executable:切換shell來執行指令,該執行路徑必須是一個絕對路徑

#ansible openstack -m command -a 'uptime'

 16:11:43 up 27 days,  2:08,  3 users,  load average: 7.11, 7.81, 8.92

6、shell:切換到某個shell執行指定的指令,參數與command相同

與command不同的是,此子產品可以支援指令管道,同時還有另一個子產品也具備此功能:raw

root@storage:~# vim data.sh

root@storage:~# date +%F_%H:%M:%S

2016-08-24_08:16:50

root@storage:~# chmod +x data.sh

root@storage:~# ansible openstack -m copy -a 'src=/root/data.sh dest=/tmp owner=root group=root mode=0755'

192.168.13.30 | SUCCESS => {

    "checksum": "a0c5ebf2b8c213d26a864ff89b381afc2b1eb901",

    "dest": "/tmp/data.sh",

    "md5sum": "159569349be4db486945c106d187ea4a",

    "mode": "0755",

    "size": 30,

    "src": "/root/.ansible/tmp/ansible-tmp-1472026735.85-123460492539355/source",

    "state": "file",

root@storage:~# ansible openstack -m command -a 'ls -l /tmp/data.sh'

-rwxr-xr-x 1 root root 30 Aug 24 16:18 /tmp/data.sh

root@storage:~# ansible openstack -m shell -a '/tmp/data.sh'

2016-08-24_16:20:00

7、其它子產品

其他常用子產品,比如:service、cron、yum、synchronize就不一一例舉,可以結合自身的系統環境進行測試。

service:系統服務管理

cron:計劃任務管理

yum:yum軟體包安裝管理

synchronize:使用rsync同步檔案

user:系統使用者管理

group:系統使用者組管理

上一篇: ansible記錄