1. 程式人生 > 實用技巧 >Ansible-入門使用方法

Ansible-入門使用方法

Ansible介紹


自動化運維工具,統一配置管理工具。自動化主要體現在Ansible集成了豐富模組以及功能元件,可以通過一個命令完成一系列的操作,進而能減少重複性的工作和維護成本,可以提高工作效率。

統一配置管理工具:

  • Ansible python 基於ssh通訊,不用安裝客戶端
  • SaltStack python 不基於ssh,使用自己的客戶端和服務端通訊,跨平臺,跨系統操作。
  • puppet Raby

Ansible的功能及優點


1.遠端執行
批量執行遠端命令,可以對多臺主機進行遠端操作

2.配置管理
批量配置軟體服務,可以進行自動化方式配置,服務的統一配置管理,和啟停

3.事件驅動
通過Ansible的模組,對服務進行不同的事件驅動
比如:
1)修改配置後重啟
2)只修改配置檔案,不重啟
3)修改配置檔案後,重新載入
4)遠端啟停服務管理

4.管理公有云
通過API介面的方式管理公有云,不過這方面做的不如saltstack.
saltstack本身可以通過saltcloud管理各大雲廠商的雲平臺。

5.二次開發
因為語法是Python,所以便於運維進行二次開發。

6.任務編排
可以通過playbook的方式來統一管理服務,並且可以使用一條命令,實現一套架構的部署

7.跨平臺,跨系統
幾乎不受到平臺和系統的限制,比如安裝apache和啟動服務

在Ubuntu上安裝apache服務名字叫apache2
在CentOS上安裝apache服務名字叫httpd

在CentOS6上啟動伺服器使用命令:/etc/init.d/nginx start
在CentOS7上啟動伺服器使用命令:systemctl start nginx

Saltstack基礎使用

# 1.安裝服務端和客戶端
[root@m01 ~]# yum install -y salt-master salt-minion

# 2.客戶端
[root@m01 ~]# yum install -y  salt-minion

# 3.修改客戶端的配置檔案
[root@m01 ~]# vim /etc/salt/minion
master: 10.0.0.61

# 4.一定先啟動服務端,再啟動客戶端
[root@m01 ~]# systemctl start salt-master salt-minion
[root@m01 ~]# systemctl start salt-minion
[root@oldgirl ~]# /etc/init.d/salt-minion start

# 檢視認證key
[root@m01 ~]# salt-key 
Accepted Keys:
Denied Keys:
Unaccepted Keys:
m01
oldgirl
web01
Rejected Keys:

### 認證1個key
[root@m01 ~]# salt-key -a web01
### 全部認證
[root@m01 ~]# salt-key -A


[root@m01 ~]# salt '*' test.ping
[root@m01 ~]# salt '*' cmd.run 'touch /root/zls123_456.txt'

環境

主機名 wanIP lanIP 角色
m01 10.0.0.61 172.16.1.61 Ansible控制端
web01 10.0.0.7 172.16.1.7 Ansible被控端
web02 10.0.0.8 172.16.1.8 Ansible被控端
web03 10.0.0.9 172.16.1.9 Ansible被控端
lb01 10.0.0.5 172.16.1.5 Ansible被控端
lb02 10.0.0.6 172.16.1.6 Ansible被控端
db01 10.0.0.51 172.16.1.51 Ansible被控端
backup 10.0.0.41 172.16.1.41 Ansible被控端
nfs 10.0.0.31 172.16.1.31 Ansible被控端

安裝

[root@m01 ~]# yum -y install ansible

ansible配置檔案


/etc/ansible/ansible.cfg

/etc/ansible/hosts 主機清單配置檔案

[root@m01 ~]# cat /etc/ansible/ansible.cfg 
#inventory      = /etc/ansible/hosts      #主機列表配置檔案
#library        = /usr/share/my_modules/  #庫檔案存放目錄
#remote_tmp     = ~/.ansible/tmp          #臨時py檔案存放在遠端主機目錄
#local_tmp      = ~/.ansible/tmp          #本機的臨時執行目錄
#forks          = 5                       #預設併發數
#sudo_user      = root                    #預設sudo使用者
#ask_sudo_pass = True                     #每次執行是否詢問sudo的ssh密碼
#ask_pass      = True                     #每次執行是否詢問ssh密碼
#remote_port    = 22                      #遠端主機埠
host_key_checking = False                 #跳過檢查主機指紋,在密碼驗證的時候忽略yes/和no的那一步。
log_path = /var/log/ansible.log           #ansible日誌

主機清單


讓ansible知道哪些機器需要維護,還有hosts的作用。


連線方式:

ip + port+user+pass

#方式一、IP+埠+使用者+密碼
[root@m01 ~]# vi /etc/ansible/hosts 
[webs]
10.0.0.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'
10.0.0.8 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'


hostname+pass

# 寫法一
[root@m01 ~]# vi /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7 ansible_ssh_pass='1'
web02 ansible_ssh_host=10.0.0.8 ansible_ssh_pass='1'

[root@m01 ~]# ansible web_group -m ping 
web02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
web01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}



# 寫法二(需要編輯hosts)
[root@m01 ~]# vi /etc/ansible/hosts
[web_group]
web01 ansible_ssh_pass='1'
web02 ansible_ssh_pass='1'

[root@m01 ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.8    web02
10.0.0.7    web01
10.0.0.9    web03
10.0.0.31   nfs01


[root@m01 ~]# ansible web_group -m ping 
web02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
web01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
nfs01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}


# 寫法三
[web_group]
web0[1:3] ansible_ssh_pass='1'
# 表示的是web01到web03;需要編輯hosts

# 寫法四,定義變數
[web_group]
web0[1:3]

[web_group:vars]
ansible_ssh_pass='1'
# 需要編輯hosts,做主機解析

[root@m01 ~]# ansible web_group -m ping 
web01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
web02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
web03 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}


主機金鑰

# 先分發金鑰
[root@m01 ~]# ssh-keygen
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

[root@m01 ~]# vi /etc/ansible/hosts 
[web_group]
10.0.0.7:22
10.0.0.8
10.0.0.9

[root@m01 ~]# ansible web_group -m ping 
10.0.0.8 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.0.0.9 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.0.0.31 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.0.0.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.0.0.51 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"


推薦使用的方式

不同用處的主機分成不同的組。

# 需要分發主機金鑰
[root@m01 ~]# vi /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7 ansible_ssh_port=22 # 後面ssh埠不是22的,可以使用該引數,更改埠
web02 ansible_ssh_host=10.0.0.8

[backup_group]
backup ansible_ssh_host=10.0.0.41

[nfs_group]
nfs ansible_ssh_host=10.0.0.31

[db_group]
db01 ansible_ssh_host=10.0.0.51
db02 ansible_ssh_host=10.0.0.52

# =======================================
# 分組書寫的方式之一
[root@m01 ~]# vi /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7 ansible_ssh_port=22
web02 ansible_ssh_host=10.0.0.8

[backup_group]
backup ansible_ssh_host=10.0.0.41

[nfs_group]
nfs ansible_ssh_host=10.0.0.31

[db_group]
db01 ansible_ssh_host=10.0.0.51
db02 ansible_ssh_host=10.0.0.52

# 可以引用上面的分組,!(和分組裡面的主機名),比如安裝rsync在特定的機器上,用此操作。
[rsync:children]
nfs_group
backup_group

[root@m01 ~]# ansible rsync -m ping 
nfs | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
backup | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

[root@m01 ~]# ansible rsync -m shell -a 'hostname' 
nfs | CHANGED | rc=0 >>
nfs01
backup | CHANGED | rc=0 >>
backup


[root@m01 ~]# ansible rsync -m shell -a 'yum -y install rsync' 
# 會提示ansible有專門的yum模組
[WARNING]: Consider using the yum module rather than running 'yum'.  If you need to use command because yum is insufficient you can
add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
# 後面會有返回安裝過後的值

檢視主機

# 檢視分組中有哪些主機
[root@m01 ~]# ansible rsync --list-host

# 檢視所有的主機
[root@m01 ~]# ansible all --list-host

主機的指定:
1.組名字
2.[]標籤名
3.IP(金鑰連線的時候)
4.主機名
# 語法
ansible 指定主機(IP、主機清單名字、all) -m 指定模組
ansible webs -m ping 

-m 		# 指定模組 


FBI WARNING

QQ:1402122292 認準原創sheldon 別人叫我曉東