天天看點

[email protected]基礎詳解

Ansible

[email protected]基礎詳解

1、ansible簡述(自動化運維)

“運維的未來是,讓研發人員能夠借助工具、自動化和流程,并且讓他們能夠在運維幹預極少的情況下部署和營運服務,進而實作自助服務。每個角色都應該努力使工作實作自動化。” ——《運維的未來》

官網: https://www.ansible.com/

官方文檔: https://docs.ansible.com/

源代碼倉庫: https://github.com/ansible/ansible

Ansible是一個自動化統一配置管理工具,自動化主要展現在Ansible內建了豐富子產品以及功能元件,可以通過一個指令完成一系列的操作,進而能減少重複性的工作和維護成本,可以提高工作效率。

           

2、[企業實際應用]

Dev開發環境
Test測試環境
Pre預釋出環境
Master生成環境
           

3、[應用場景]

檔案傳輸

應用部署

配置管理

任務流編排

#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 ngin
           

4、常用自動化工具

1、Ansible:python,Agentless,中小型應用環境
2、Saltstack:python,一般需部署agent,執行效率更高
3、Puppet:ruby, 功能強大,配置複雜,重型,适合大型環境
4、Fabric:python,agentless
5、Chef:ruby,國内應用少
           

5、ansible發展史

#作者:Michael DeHaan( Cobbler 與 Func 作者)

ansible 的名稱來自科幻小說《安德的遊戲》中跨越時空的即時通信工具,使用它可以在相距數光年的距離,遠端實時控制前線的艦隊戰鬥。

2012-03-09,釋出0.0.1版,

2015-10-17,Red Hat宣布1.5億美元收購


#同類型軟體對比
1.puppet 學習難,安裝ruby環境難,沒有遠端執行功能
2.ansible 輕量級,大規模環境下隻通過ssh會很慢,串行的
3.saltstack 一般選擇salt會使用C/S結構的模式,salt-master和salt-minion,并行的,大規模批量操作的情況下,會比Ansible速度快一些,底層使用的是zero-MQ消協隊列
           

6、[Ansible的特性]

子產品化:調用特定的子產品完成特定任務,支援自定義子產品,可使用任何程式設計語言寫子產品
Paramiko(python對ssh的實作),PyYAML,Jinja2(模闆語言)三個關鍵子產品
基于Python語言實作
部署簡單,基于python和SSH(預設已安裝),agentless,無需代理不依賴PKI(無需ssl)
安全,基于OpenSSH
幂等性:一個任務執行1遍和執行n遍效果一樣,不因重複執行帶來意外情況,此特性非絕對
支援playbook編排任務,YAML格式,編排任務,支援豐富的資料結構
較強大的多層解決方案 role
           

7、[Ansible架構]

1、連接配接插件

connection plugins

用于連接配接主機 用來連接配接被管理端

2、核心子產品

core modules

連接配接主機實作操作, 它依賴于具體的子產品來做具體的事情

3、自定義子產品

custom modules

根據自己的需求編寫具體的子產品

4、插件

plugins

完成子產品功能的補充

5、劇本

playbook

ansible的配置檔案,将多個任務定義在劇本中,由ansible自動執行

6、主機清單

inventor

定義ansible需要操作主機的範圍
[email protected]基礎詳解
#Ansible的執行流程

1.Ansible讀取playbook劇本,劇本中會記錄對哪些主機執行哪些任務。
2.首先Ansible通過主機清單找到要執行的主機,然後調用具體的子產品。
3.其次Ansible會通過連接配接插件連接配接對應的主機并推送對應的任務清單。
4.最後被管理的主機會将Ansible發送過來的任務解析為本地Shell指令執行。
           

8、安裝Ansible

#1.安裝epel源
[[email protected] ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

#2.安裝Ansible
[[email protected] ~]# yum install ansible -y


  
  
#ansible格式
ansible <host-pattern> [options]
#參數詳解
--version   #ansible版本資訊
-v          #顯示詳細資訊
-i          #主機清單檔案路徑,預設是在/etc/ansible/hosts
-m          #使用的子產品名稱,預設使用command子產品
-a          #使用的子產品參數,子產品的具體動作
-k          #提示輸入ssh密碼,而不使用基于ssh的密鑰認證
-C          #模拟執行測試,但不會真的執行
-T          #執行指令的逾時



#Ansible是否安裝成功檢視(版本)
[[email protected] ~]# ansible --version          #版本檢視
ansible 2.9.21
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
           

Ansible配置檔案讀取順

1、$ANSIBLE_CONFIG

2、./ansible.cfg

3、~/.ansible.cfg

4、/etc/ansible/ansible.cfg

[[email protected] ~]# rpm -ql ansible
[[email protected] ~]# zcat /usr/share/man/man1/ansible-config.1.gz

#要檢視完整清單,請通路https://docs.ansibe.com/或使用ansibe-config指令。
For a full list check \fI\%https://docs.ansible.com/\fP\&. or use the \fIansible\-config\fP command.

#/etc/ansible/ansible.cfg 配置檔案,如果存在則使用
/etc/ansible/ansible.cfg \-\- Config file, used if present

#~/.ansible.cfg 使用者配置檔案,覆寫預設配置(如果存在)
~/.ansible.cfg \-\- User config file, overrides the default config if present

#\&/ansible.cfg 本地配置檔案(在目前工作目錄中)假定為(aqproject-specific)(aq,如果存在,則重寫其餘檔案)。
\&./ansible.cfg \-\- Local config file (in current working directory) assumed to be \(aqproject specific\(aq and overrides the rest if present.

#如上所述,ANSIBLE_CONFIG環境變量将覆寫所有其他環境變量。
As mentioned above, the ANSIBLE_CONFIG environment variable will override all others.
           

9、[Ansible相關檔案]

#配置檔案
/etc/ansible/ansible.cfg 主配置檔案,配置ansible工作特性
/etc/ansible/hosts 主機清單
/etc/ansible/roles/ 存放角色的目錄


#ansible主配置檔案
Ansible 的配置檔案 /etc/ansible/ansible.cfg ,其中大部分的配置内容無需進行修改



#配置檔案注釋
[defaults]
#inventory           = /etc/ansible/hosts              # 主機清單配置檔案
#library             = /usr/share/my_modules/          # 庫檔案存放目錄
#remote_tmp          = $HOME/.ansible/tmp              # 臨時py指令檔案存放在遠端主機目錄
#local_tmp           = $HOME/.ansible/tmp              # 本機的臨時指令執行目錄
#forks               = 5                               # 預設并發數
#sudo_user           = root                            # 預設sudo 使用者
#ask_sudo_pass       = True                            # 每次執行ansible指令是否詢問ssh密碼
#ask_pass            = True   
#remote_port         = 22
#host_key_checking   = False                            # 檢查對應伺服器的host_key,建議取消注釋
#log_path            = /var/log/ansible.log             # 日志檔案,建議啟用
#module_name         = command                          # 預設子產品,可以修改為shell子產品

           

10、[inventory 主機清單]

ansible的主要功用在于批量主機操作,為了便捷地使用其中的部分主機,可以在inventory file中将其分組命名,預設的inventory file為 /etc/ansible/hosts,inventory file可以有多個,且也可以通過Dynamic Inventory來動态生成

官方文檔:https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

/etc/ansible/hosts是ansible預設主機資産清單檔案,用于定義被管理主機的認證資訊, 例如ssh登入使用者名、密碼以及key相關資訊。Inventory檔案中填寫需要被管理的主機與主機組資訊。還可以自定義Inventory主機清單的位置,使用-i指定檔案位置即可。
           

11、主機清單檔案格式

inventory檔案遵循INI檔案風格,中括号中的字元為組名。可以将同一個主機同時歸并到多個不同的組中

此外,當如若目标主機使用了非預設的SSH端口,還可以在主機名稱之後使用冒号加端口号來标明,如果主機名稱遵循相似的命名模式,還可以使用清單的方式辨別各主機。

1場景一:基于密碼連接配接方式一

#############################(IP+端口+使用者+密碼)#####################
[[email protected] ~]# cat /etc/ansible/hosts 
[web]
172.16.1.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1' 
172.16.1.8 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'
172.16.1.9 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'
[[email protected]@m01 ~]# ansible all -m ping 
172.16.1.8 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
......
...



#################################(主機名+密碼)##########################
[[email protected] ~]#vim /etc/ansible/hosts
[web]
web0[1:3] ansible_ssh_pass='1'


#添加配置檔案
[[email protected] ~]#vim /etc/ssh/ssh_config           #增加配置檔案
Host *
        GSSAPIAuthentication yes
.........
......
StrictHostKeyChecking no 



[[email protected] ~]# ansible all -m ping          #檢查測試ok
web02 | SUCCESS => {     
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
.....
...
           

場景二:基于密鑰連接配接 (需要先建立公鑰和私鑰)

秘鑰
#秘鑰對制作
[roo[email protected]ocalhost ansible]# ssh-keygen 
.....
..
#方式一秘鑰推送傳輸
[[email protected] ansible]# ssh-copy-id 172.16.1.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
......
...


#方式二推送公鑰
[[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
           
#方式一、主機+端口+密鑰
[webs]
10.0.0.7:22
10.0.0.8

[[email protected] ~]# ansible webs -m ping -i ./hosts
10.0.0.8 | 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"
}




#方式二、别名+主機+端口+密鑰
[webs]
web01 ansible_ssh_host=10.0.0.7 ansible_ssh_port=22
web02 ansible_ssh_host=10.0.0.8

[[email protected] ~]# ansible webs -m ping -i ./hosts
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"
}




#本機
[[email protected] ~]# vim /etc/ansible/hosts
172.16.1.61 ansible_connection=local   #指定本地連接配接,無需ssh配置
......
...
[[email protected] ~]# ansible all -m ping 
172.16.1.61 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}



# 注:密鑰連接配接的前提是需要做好免密
[[email protected] ~]# vim /etc/ansible/hosts
[web]
172.16.1.7
172.16.1.8
172.16.1.9
[[email protected] ~]# ansible all -m ping 
172.16.1.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}


#别名連接配接
[[email protected] ~]# vim /etc/ansible/hosts
[web]
oldboy ansible_ssh_host=172.16.1.7

[[email protected] ~]# ansible web -m ping   &&   ansible oldboy  -m ping 
172.16.1.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}


           

場景三:主機組

# 分組連接配接
[web]
172.16.1.7
172.16.1.8
172.16.1.9

[lb]
172.16.1.5
172.16.1.6

# 多組
[web]
172.16.1.7
172.16.1.8
172.16.1.9
[lb]
172.16.1.5
172.16.1.6
[db]
172.16.1.51
[servers:children]
lb
web


#指連接配接的組
[[email protected] ~]# ansible web -m ping 







#主機組使用方式
[[email protected] ~]# vim hosts
[db_group]
db01 ansible_ssh_host=10.0.0.51
db02 ansible_ssh_host=10.0.0.52

[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8



#檢視指定組内主機數量
[[email protected] ~]# ansible web_group -m ping -i ./hosts  --list-host
  hosts (2):
    web01
    web02

[[email protected] ~]# ansible db_group -m ping -i ./hosts  --list-host
  hosts (2):
    db01
    db02


#方式一、主機組變量+主機+密碼
[db_group]
db01 ansible_ssh_host=10.0.0.51
db02 ansible_ssh_host=10.0.0.52
[db_group:vars]
ansible_ssh_pass='1'

#方式二、主機組變量+主機+密鑰
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8

#定義多組,多組彙總整合
# lnmp組包括兩個子組[db,web]
[lnmp:children]
db_group
web_group

#最終配置檔案
[[email protected] ~]# cat hosts
[db_group]
db01 ansible_ssh_host=10.0.0.51
db02 ansible_ssh_host=10.0.0.52

[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8

[lnmp:children]
db_group
web_group

#檢視多組
[[email protected] ~]# ansible all -m ping -i ./hosts  --list-host
  hosts (4):
    db01
    db02
    web01
    web02
[[email protected] ~] ansible lnmp -m ping -i ./hosts  --list-host
  hosts (4):
    db01
    db02
    web01
    web02
           

12、檢視伺服器(統計)

#檢視目前有多少台主機
[[email protected] ~]# ansible all -m ping -i /etc/ansible/hosts --list-hosts
  hosts (7):
    172.16.1.7
    172.16.1.8
    172.16.1.9
    oldboy
    web01
    web02
    web03
[[email protected] ~]# ansible web -m ping -i /etc/ansible/hosts --list-hosts
  hosts (3):
    172.16.1.7
    172.16.1.8
    172.16.1.9
[[email protected] ~]# ansible server -m ping -i /etc/ansible/hosts --list-hosts
  hosts (3):
    web01
    web02
    web03
[[email protected] ~]# ansible webserver -m ping -i /etc/ansible/hosts --list-hosts
  hosts (1):
    oldboy
[[email protected] ~]# ansible oldboy -m ping -i /etc/ansible/hosts --list-hosts
  hosts (1):
    oldboy
[[email protected] ~]#