Ansible


                                               臨淵羡魚不如退而結網 

 
         

 


我在這里只介紹下怎么去使用,具體想要深入了解可以去ansible中文權威指南,很詳細http://www.ansible.com.cn/docs/intro_configuration.html


[root@master .ssh]# cat /etc/issue CentOS release 6.8 (Final) 首先在新的機器上安裝epel源 1.epel簡介: https://fedoraproject.org/wiki/EPEL/zh-cn rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm 以上URL請按實際情況修改 2.查看是否安裝成功 rpm -q epel-release 3.導入key: rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 4.修改/etc/yum.repos.d/epel.repo文件 在[epel]最后添加一條屬性 priority=11 vi /etc/yum.repos.d/epel.repo 意思是yum先去官方源查,官方沒有再去epel的源找 5.重建緩存 yum makecache

這里我們使用yum安裝Ansible

yum install ansible 

與批量管理機器配置公鑰私鑰實現無密碼登陸,這里我使用自己的一台測試機做示范

在A機器創建公鑰\私鑰對

[root@master .ssh]# ssh-keygen -t rsa
敲三下回車
把A機器上的id_rsa.pub復制到B機下,在B機的authorized_keys里
cat id_rsa.pub >> .ssh/authorized_keys
ssh-copy-id -i ~/.ssh/id_rsa.pub 172.30.1.208 就可以了

測試下,連接B機器

成功,不需要輸入密碼
[root@master .ssh]# ssh 172.30.1.208
Last login: Tue Feb 21 10:11:39 2017 from master.1.cn
[root@client ~]# 

 現在開始修改Ansible配置

[root@master .ssh]# cat /etc/ansible/hosts 
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
## 
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Here's another example of host ranges, this time there are no
# leading 0s:

## db-[99:101]-node.example.com
[test] #添加一個test ip為172.30.1.208 , 我要操作的客戶端 172.30.1.208 #

示例 Ansible有一個非常有用的模塊是 command,相信很多人都需要它!Command模塊就是“遠程執行命令”

 相比Ansible的內置模塊而言,Command模塊無法通過返回值來判斷命令是否執行成功。

 但Ansible添加了creates 和 removes 的屬性,以此檢查命令是否成功執行或者是否應該被執行。

 如果你定義了 creates 屬性,當文件已存在時,它不會再執行。相反如果定義了 removes 屬性,

 則只有文件存在的情況下命令才會被執行

測試執行

ansible test -m command -a 'date' //command和shell模塊可以在被控端執行命令

          (失敗了)

172.30.1.208 | FAILED | rc=0 >>
failed to resolve remote temporary directory from ansible-tmp-1487642084.32-198317429406356: `( umask 77 && mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1487642084.32-198317429406356 `" && echo ansible-tmp-1487642084.32-198317429406356="` echo ~/.ansible/tmp/ansible-tmp-1487642084.32-198317429406356 `" )` returned empty string

 

查找失敗原因

為了提升效率,ansible使用了SSH的ControlPersist、ControlMaster特性。而該特征,應該需要在5.6或以上版本才能實現,Centos6.x上默認版本為5.3,在實際測試過程中,會經常報上面的錯誤

解決方案

1.提高SSH版本,在測試后期,我們換用了Centos7,當然也可以不換操作系統,只升級openssh。(openssh升級腳本:

http://www.cnblogs.com/shenjianyu/p/6397150.html

)

2.禁用該功能特性。

在配置文件修改

# vim /etc/ansible/ansible.cfg
[ssh_connection]
ssh_args = -o ControlMaster=no -o ControlPersist=no

再次測試 成功

[root@master .ssh]# ansible test -m command -a 'date' 
172.30.1.208 | SUCCESS | rc=0 >>
Tue Feb 21 10:04:53 CST 2017

[root@master .ssh]#ansible test -m command -a 'mkdir /ssj'
172.30.1.208 | SUCCESS | rc=0 >>

編寫一個簡單的playbook

下面有一個安裝tree命令的腳本

#!/bin/bash
# 安裝tree
yum install tree -y

將其轉換為一個完整的playbook后:

---
- hosts: all

  tasks:
   - name: "安裝tree"
     command: yum install -y tree

將以上內容放在一個名為playbook.yml的文件中,直接調用ansible-playbook命令,即可運行,運行結果和腳本運行結果一致:

# ansible-playbook ./playbook.yml
[root@master ansible]# ansible-playbook ./playbook.yml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [172.30.1.208]

TASK [安裝Apache] ****************************************************************
changed: [172.30.1.208]
 [WARNING]: Consider using yum module rather than running yum


PLAY RECAP *********************************************************************
172.30.1.208               : ok=2    changed=1    unreachable=0    failed=0  

再舉個例子,拷貝腳本到客戶端,執行腳本

[root@master ansible]# vim /home/touch.sh
#!/bin/bash
for i in `seq 10`
do
`touch /home/user$i`
done

執行playbook報錯(失敗,貌似是說對應機器沒有這個文件)

 1 [root@master ansible]# ansible-playbook ./playbook.yml
 2 
 3 PLAY [all] *********************************************************************
 4 
 5 TASK [setup] *******************************************************************
 6 ok: [172.30.1.208]
 7 
 8 TASK [安裝Apache] ****************************************************************
 9 changed: [172.30.1.208]
10  [WARNING]: Consider using yum module rather than running yum
11 
12 
13 TASK [執行腳本] ********************************************************************
14 fatal: [172.30.1.208]: FAILED! => {"changed": true, "cmd": ["sh", "/home/touch.sh"], "delta": "0:00:00.003191", "end": "2017-02-22 09:03:14.381231", "failed": true, "rc": 127, "start": "2017-02-22 09:03:14.378040", "stderr": "sh: /home/touch.sh: No such file or directory", "stdout": "", "stdout_lines": [], "warnings": []}
15         to retry, use: --limit @/etc/ansible/playbook.retry
16 
17 PLAY RECAP *********************************************************************
18 172.30.1.208               : ok=2    changed=1    unreachable=0    failed=1   
 1 [root@master ansible]# ansible test -m copy -a 'src=/home/touch.sh dest=/home'   #-m 指令  -a路徑  dest目的路徑
 2 172.30.1.208 | SUCCESS => {
 3     "changed": true, 
 4     "checksum": "6678dda6e6661840b5cb135f66053a3d254bd739", 
 5     "dest": "/home/touch.sh", 
 6     "gid": 0, 
 7     "group": "root", 
 8     "md5sum": "f3603ea1b89bcafa6c43809c45a5fa11", 
 9     "mode": "0644", 
10     "owner": "root", 
11     "secontext": "unconfined_u:object_r:user_home_dir_t:s0", 
12     "size": 59, 
13     "src": "/root/.ansible/tmp/ansible-tmp-1487725606.17-235469749218236/source", 
14     "state": "file", 
15     "uid": 0
16 }
 1 [root@master ansible]# ansible-playbook ./playbook.yml            成功  2 
 3 PLAY [all] *********************************************************************
 4 
 5 TASK [setup] *******************************************************************
 6 ok: [172.30.1.208]
 7 
 8 TASK [安裝Apache] ****************************************************************
 9 changed: [172.30.1.208]
10  [WARNING]: Consider using yum module rather than running yum
11 
12 
13 TASK [執行腳本] ********************************************************************
14 changed: [172.30.1.208]
15 
16 PLAY RECAP *********************************************************************
17 172.30.1.208               : ok=3    changed=2    unreachable=0    failed=0   

在客戶端查看下

[root@client home]# ls
ansi  ansible  rzrk  sjy  touch.sh  user1  user10  user2  user3  user4  user5  user6  user7  user8  user9

 

 

 Ansible 下各模塊命令介紹

copy模塊:

    目的:把主控端/root目錄下的a.sh文件拷貝到到指定節點上

    命令:ansible 10.1.1.113 -m copy -a 'src=/root/a.sh dest=/tmp/'

 

file模塊:

    目的:更改指定節點上/tmp/t.sh的權限為755,屬主和屬組為root

    命令:ansible all -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root"

 

cron模塊:

    目的:在指定節點上定義一個計划任務,每隔3分鍾到主控端更新一次時間

    命令:ansible all -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'

 

group模塊:

    目的:在所有節點上創建一個組名為nolinux,gid為2014的組 

    命令:ansible all -m group -a 'gid=2014 name=nolinux'

 

user模塊:

    目的:在指定節點上創建一個用戶名為nolinux,組為nolinux的用戶

    命令:ansible 10.1.1.113 -m user -a 'name=nolinux groups=nolinux state=present'

 

yum模塊:

    目的:在指定節點上安裝 lrzsz 服務

    命令:ansible all -m yum -a "state=present name=httpd"

 

service模塊:

    目的:啟動指定節點上的 puppet 服務,並讓其開機自啟動

    命令:ansible 10.1.1.113 -m service -a 'name=puppet state=restarted enabled=yes'

 

script模塊:

    目的:在指定節點上執行/root/a.sh腳本(該腳本是在ansible控制節點上的)

    命令:ansible 10.1.1.113 -m script -a '/root/a.sh'

 

ping模塊:

    目的:檢查指定節點機器是否還能連通

    命令:ansible 10.1.1.113 -m ping

 

command模塊:

    目的:在指定節點上運行hostname命令

    命令:ansible 10.1.1.113 -m command -a 'hostname'

 

raw模塊:

    目的:在10.1.1.113節點上運行hostname命令

    命令:ansible 10.1.1.113 -m raw-a 'hostname|tee'

 

get_url模塊:

    目的:將http://10.1.1.116/favicon.ico文件下載到指定節點的/tmp目錄下

    命令:ansible 10.1.1.113 -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'

 

 

 

synchronize模塊:

    目的:將主控方/root/a目錄推送到指定節點的/tmp目錄下

    命令:ansible 10.1.1.113 -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM