1. 程式人生 > >自動化工具ansible的安裝和使用

自動化工具ansible的安裝和使用

ansible 自動化工具 ansible自動化工具

自動化工具ansible的安裝和使用

操作系統:centos6.8

服務端ip192.168.137.142

客戶端ip: 192.168.137.34 192.168.137.33

安裝ansible

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

yum clean all

yum repolist

yum install -y ansible

生成秘鑰讓服務端和客戶端能進行免密鑰

ssh-keygen -t dsa #一路按回車鍵即可

cat /root/.ssh/id_dsa.pub >> /root/.ssh/authorized_keys

chmod 600 /root/.ssh/authorized_keys

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

#按回車鍵,然後輸入yes,最後輸入客戶端的服務器密碼

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

#按回車鍵,然後輸入yes,最後輸入客戶端的服務器密碼

配置ansible

# vim /etc/ansible/ansible.cfg

[defaults]

hostfile =/etc/ansible/hosts

library =/usr/share/ansible

remote_tmp =$HOME/.ansible/tmp

pattern = *

forks = 5

poll_interval = 15

sudo_user = root

transport = smart

remote_port = 22

timeout = 10

添加主機

# vim /etc/ansible/hosts

[test] #自定義主機組名

192.168.137.34 #添加客戶端的免密鑰登錄ip

192.168.137.33

3.常用模塊使用

(1).setup

#用來查看遠程主機的一些基本信息

ansible test -m setup #有綠色內容顯示為執行成功

(主機組) (模塊)

(2).ping

#用來測試遠程主機的運行狀態

ansible test -m ping

192.168.137.34 | SUCCESS => {

"changed": false,

"ping": "pong"

}

192.168.137.33 | SUCCESS => {

"changed": false,

"ping": "pong"

(3).file

#設置文件的屬性

相關選項如下:

force:需要在兩種情況下強制創建軟鏈接,一種是源文件不存在,但之後會建立的情況下;另一種是目標軟鏈接已存在,需要先取消之前的軟鏈,然後創建新的軟鏈,有兩個選項:yes|no

group: 定義文件/目錄的屬組

mode: 定義文件/目錄的權限

owner: 定義文件/目錄的屬主

path: 必選項,定義文件/目錄的路徑

recurse:遞歸設置文件的屬性,只對目錄有效

src: 被鏈接的源文件路徑,只應用於state=link的情況

dest: 被鏈接到的路徑,只應用於state=link的情況

state:

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

file:即使文件不存在,也不會被創建

link:創建軟鏈接

hard:創建硬鏈接

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

absent:刪除目錄、文件或者取消鏈接文件

例子:

# 遠程文件符號鏈接創建當客戶端在/home目錄存在文件test_a,並且其他用戶擁有執行權限,顯示綠色為執行成功

ansible test -m file -a "src=/home/test_a dest=/tmp/test_a state=link"

192.168.137.33 | SUCCESS => {

"changed": false,

"dest": "/tmp/test_a",

"gid": 0,

"group": "root",

"mode": "0777",

"owner": "root",

"secontext": "unconfined_u:object_r:user_tmp_t:s0",

"size": 12,

"src": "/home/test_a",

"state": "link",

"uid": 0

}

192.168.137.34 | SUCCESS => {

"changed": false,

"dest": "/tmp/test_a",

"gid": 0,

"group": "root",

"mode": "0777",

"owner": "root",

"secontext": "unconfined_u:object_r:user_tmp_t:s0",

"size": 12,

"src": "/home/test_a",

"state": "link",

"uid": 0

}

#遠程文件信息查看

ansible test -m command -a "ls -l /home/test_a"

192.168.137.33 | SUCCESS | rc=0 >>

-rwxr-xr-x. 1 root root 14209 Sep 13 05:11 /home/test_a

192.168.137.34 | SUCCESS | rc=0 >>

-rwxr-xr-x. 1 root root 12 Sep 13 10:34 /home/test_a

## 將本地文件“/etc/ansible/ansible.cfg”復制到遠程服務器

ansible test -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"

192.168.137.34 | SUCCESS => {

"changed": true,

"checksum": "1b4bbebae90acabf3b33da80ddcb2b4564a8d323",

"dest": "/tmp/ansible.cfg",

"gid": 0,

"group": "root",

"md5sum": "56d60e05c5f0e6c503b82403281dbbcf",

"mode": "0644",

"owner": "root",

"secontext": "unconfined_u:object_r:admin_home_t:s0",

"size": 18307,

"src": "/root/.ansible/tmp/ansible-tmp-1505325737.38-134417600986364/source",

"state": "file",

"uid": 0

}

192.168.137.33 | SUCCESS => {

"changed": true,

"checksum": "1b4bbebae90acabf3b33da80ddcb2b4564a8d323",

"dest": "/tmp/ansible.cfg",

"gid": 0,

"group": "root",

"md5sum": "56d60e05c5f0e6c503b82403281dbbcf",

"mode": "0644",

"owner": "root",

"secontext": "unconfined_u:object_r:admin_home_t:s0",

"size": 18307,

"src": "/root/.ansible/tmp/ansible-tmp-1505325737.34-166041859775381/source",

"state": "file",

"uid": 0

}

dest:必選項。要將源文件復制到的遠程主機的絕對路徑,如果源文件是一個目錄,那麽該路徑也必須是個目錄

src:被復制到遠程主機的本地文件,可以是絕對路徑,也可以是相對路徑。如果路徑是一個目錄,它將遞歸復制。在這種情況下,如果路徑使用“/”來結尾,則只復制目錄裏的內容,如果沒有使用“/”來結尾,則包含目錄在內的整個內容全部復制,類似於rsync。

(4).shell

## 切換到某個shell執行指定的指令

例子:

# 先在本地創建一個SHELL腳本

# vim /tmp/a.sh

#!/bin/sh

echo "hello"

#chmod +x /tmp/a.sh

# 將創建的腳本文件分發到遠程

ansible test -m copy -a "src=/tmp/a.sh dest=/tmp/a.sh owner=root group=root mode=0755"

## 遠程執行

ansible test -m shell -a "/tmp/a.sh"

192.168.137.34 | SUCCESS | rc=0 >>

hello

192.168.137.33 | SUCCESS | rc=0 >>

hello

Ansinle的Playbooks

例子:

vim /etc/ansible/test.yml

- name: create_user

hosts: test

user: root

gather_facts: false

vars:

- user: "testyml"

tasks:

- name: create {{ user }}

sudo: yes

user: name="{{ user }}"

註意:

name參數對該playbook實現的功能做一個概述,後面執行過程中,會打印 name變量的值 ,可以省略;

gather_facts參數指定了在以下任務部分執行前,是否先執行setup模塊獲取主機相關信息,這在後面的task會使用到setup獲取的信息時用到;

vars參數指定了變量,這裏指字一個user變量,其值為test ,需要註意的是,變量值一定要用引號引住;

user提定了調用user模塊,name是user模塊裏的一個參數,而增加的用戶名字調用了上面user變量的值。

執行:

cd /etc/ansible/

ansible-playbook test.yml

[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is ‘sudo‘ (default).

This feature will be removed in a future release.

Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

PLAY [create_user] ***********************************************************************************************************************************************************

TASK [create testyml] ********************************************************************************************************************************************************

changed: [192.168.137.33]

changed: [192.168.137.34]

PLAY RECAP *******************************************************************************************************************************************************************

192.168.137.33 : ok=1 changed=1 unreachable=0 failed=0

192.168.137.34 : ok=1 changed=1 unreachable=0 failed=0

檢查:

ansible 192.168.137.33 -m shell -a "id testyml"

[[email protected] ansible]# ansible 192.168.137.33 -m shell -a "id testyml"

192.168.137.33 | SUCCESS | rc=0 >>

uid=501(testyml) gid=501(testyml) groups=501(testyml)

[[email protected] ansible]# ansible 192.168.137.34 -m shell -a "id testyml"

192.168.137.34 | SUCCESS | rc=0 >>

uid=501(testyml) gid=501(testyml) groups=501(testyml)


本文出自 “Linux修仙之路” 博客,請務必保留此出處http://8999a.blog.51cto.com/11385098/1965139

自動化工具ansible的安裝和使用