国产在线观看精品免费,亚洲日本VA久久一区二区,香蕉久久99综合一区二区三区,久久精品99国产精品蜜桃小说

Ansible 介紹

2021-11-03 15:19:05 shuai.chang

睿智創(chuàng  )新RAIZ,一體化IT服務(wù)提供商

什么是 Ansible

Ansible 是一個(gè)簡(jiǎn)單,強大且無(wú)代理的自動(dòng)化語(yǔ)言。

Ansible 的好處:

簡(jiǎn)單易讀:基于 YAML 文本編寫(xiě),易于閱讀,非專(zhuān)業(yè)的開(kāi)發(fā)人員也可以編寫(xiě)。

功能強大:它可以同于管理配置,軟件安裝,流程自動(dòng)化

無(wú)代理:不需要在客戶(hù)端安裝額外的 agent

跨平臺支持:支持 linux,Windows,Unix 和網(wǎng)絡(luò )設備

Ansible 是如何工作的

Ansible 典型的工作方式是通過(guò)一個(gè)腳本文件(基于 YAML 格式構建的)去控制遠端操作系統按照特定的順序執行相關(guān)任務(wù),我們稱(chēng)這個(gè)文件為 playbook;

架構

**節點(diǎn):**Ansible 架構中擁有兩種計算機類(lèi)型,即控制節點(diǎn)和受控節點(diǎn)。Ansible 運行在控制節點(diǎn)上,并且只能運行在 linux 操作系統上,對于被控節點(diǎn),可以是主機設備,也可以是網(wǎng)絡(luò )設備,主機設備的操作系統,可以是 Windows,也可以是 linux。

睿智創(chuàng  )新RAIZ,一體化IT服務(wù)提供商

清單(inventory): 受控節點(diǎn)設備的列表。在這個(gè)列表中,你可以根據某些標準(如,作用,服務(wù)等)將擁有相同屬性的計算機組織到一個(gè)組中。Ansible 清單,支持靜態(tài)清單(一旦定義好,除非你修改配置文件,不然不會(huì )發(fā)生改變。),也支持動(dòng)態(tài)清單(通過(guò)腳本從外部源獲取清單,該清單可以隨著(zhù)環(huán)境的改變而改變。)。

Playbook: 需要在被控節點(diǎn)主機上運行的任務(wù)列表,從而讓他們達到我們預期的狀態(tài)。Playbook 中包含一個(gè)或多個(gè) play,每個(gè) play 會(huì )在一組或多組計算機上按順序執行已經(jīng)定義好的一系列 task,每個(gè) task 都是一個(gè)執行模塊。

模塊(Module): 用于確保主機處于某一個(gè)特定的狀態(tài),例如可以使用 yum(對于不同發(fā)行版本的 Linux,模塊名可能有所不同個(gè),如,在 Ubuntu 中與之對應的是 apt 模塊。) 模塊確保主機已經(jīng)安裝了某個(gè)軟件,如果主機狀態(tài)已經(jīng)是預期的了(已經(jīng)安裝了該軟件),那么就不會(huì )執行任何操作,執行下一個(gè)模塊(task)。

Plugin  添加到 Ansible 中的代碼段,用于擴展 Ansible 平臺

安裝 Ansible

在 Ubuntu 上安裝 ansible

it@workstation:~$ sudo apt install -y ansible

安裝完成后,你可以通過(guò) ansible --version 查看 ansible 的版本信息

it@workstation:~$ ansible --version
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/it/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0]

配置 Ansible

Ansible 提供的默認主機配置文件:

it@workstation:~$ ls -l /etc/ansible/ansible.cfg 
-rw-r--r-- 1 root root 19985 3月  5  2020 /etc/ansible/ansible.cfg

* 只有 root 用戶(hù)才有權限編輯。

創(chuàng )建新的主機配置文件:

一般情況,我們直接復制默認配置文件,然后根據需要修改復制過(guò)來(lái)的配置文件。

it@workstation:~$ mkdir ansible
it@workstation:~$ cp /etc/ansible/ansible.cfg ansible/
it@workstation:~$ ls -l ansible/
total 24
-rw-r--r-- 1 it it 19985 12月 29 15:03 ansible.cfg

*  在創(chuàng )建新的配置文件之前,我們首先需要創(chuàng )建一個(gè)用于測試 Ansible 的工作目錄,然后再創(chuàng )建配置文件。

當我們擁有多個(gè)配置文件時(shí),配置文件生效的順序為:

  • 當前目錄下:./ansible.cfg

  • home 目錄下:~/ansible.cfg

  • 默認配置文件:/etc/ansible/ansible.cfg

復制完成后,我們可以通過(guò) ansible --version 查看當前生效的配置文件

it@workstation:~$ cd ansible/
it@workstation:~/ansible$ ansible --version
ansible 2.9.6
  config file = /home/it/ansible/ansible.cfg
  configured module search path = ['/home/it/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0]

*  你需要切換到 Ansible 的工作目錄,不然 Ansible 工作目錄下的配置文件是無(wú)效的。

對于默認配置文件,我們當前需要了解的有兩個(gè)模塊:defaultsprivilege_escalation。

defaults 模塊:

inventory: 指定清單文件路徑

host_key_checking : 是否檢查主機 key 是否可信

remote_user: 遠程連接時(shí)使用的用戶(hù),默認使用當前用戶(hù)

ask_pass: 連接時(shí)是否詢(xún)問(wèn)輸入密碼(如果沒(méi)有配置密鑰登錄,需要配置該選項為 true。)

privilege_escalation 模塊:sudo 提權相關(guān)的配置

become: 是否開(kāi)啟切換用戶(hù)

become_method: 如何切換用戶(hù)

become_user: 切換到那個(gè)用戶(hù)

become_ask_pass: 是否提示輸入密碼

更改配置文件

it@workstation:~/ansible$ vim ansible.cfg 
it@workstation:~/ansible$ grep -vE '^$|#' ansible.cfg
[defaults]
inventory      = /home/it/ansible/hosts
host_key_checking = False
remote_user = it
ask_pass      = True
[inventory]
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=True
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]

清單(Inventory)

Ansible 提供了一個(gè)示例清單文件,并給我們提供了一些常規的示例:

EX 1: 將未分組的主機放在所有組的前面的;

EX 2: 通過(guò) [ ] 指定主機組的名稱(chēng),如,webservers,下面直到下一個(gè)組名(dbservers)前結束的都是屬于該組的主機,即使主機與主機之間存在空行,但如沒(méi)有到下一個(gè)組名,他們依然屬于同一個(gè)主機組;如果某些主機之間有某些順序關(guān)系,你可以通過(guò)簡(jiǎn)寫(xiě),將他們放到同一行,如示例中的 “www[001:006].example.com”,分別表示 www001.example.com、www002.example.com、www003.example.com、www004.example.com、www005.example.com 和 www006.example.com。

EX 3: 提供了另一種主機范圍的示例

it@workstation:~$ 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

it@workstation:~$

創(chuàng )建自己的主機清單

it@workstation:~$ vim ansible/hosts
it@workstation:~$ cat ansible/hosts
serverb

[web]
servera

[prod:children]
web

在該清單中,我們使用組嵌套,這個(gè)是前面示例中沒(méi)有的,web 組是 prod 組的子組。

我們可以通過(guò) ansible 命令查看主機清單內容:

it@workstation:~/ansible$ ansible web --list-host
SSH password:
BECOME password[defaults to SSH password]:
  hosts (1):
    servera
it@workstation:~/ansible$ ansible prod --list-host
SSH password:
BECOME password[defaults to SSH password]:
  hosts (1):
    servera

我們可以通過(guò) ansible 命令來(lái)驗證我們的主機清單文件

同時(shí) Ansible 還有兩個(gè)默認組:

all: 表示所有組件

ungrouped: 表示所有未分組的主機

it@workstation:~/ansible$ ansible all --list-host
SSH password:
BECOME password[defaults to SSH password]:
  hosts (2):
    serverb
    servera
it@workstation:~/ansible$ ansible ungrouped --list-host
SSH password:
BECOME password[defaults to SSH password]:
  hosts (1):
    serverb

*  在 Ansible 中,主機可以同時(shí)屬于多個(gè)組。

Ansible 中存在一個(gè)隱藏的主機 localhost,即 ansible 本身,當主機指定為 localhost 時(shí),ansible 會(huì )自動(dòng)忽略掉 remote_user 配置;

運行 ansible 臨時(shí)命令

Ansible 臨時(shí)命令可以快速執行單個(gè) ansible 任務(wù),而不需編寫(xiě) playbook,這對測試和排錯很有幫助。如,你可以使用臨時(shí)命令去檢查,某個(gè)軟件包在主機上的狀態(tài)是什么,是否可用。

通過(guò)臨時(shí)命令查看連接到遠程主機的用戶(hù)信息

it@workstation:~/ansible$ ansible servera -m shell -a "id"
SSH password:
BECOME password[defaults to SSH password]:
servera | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root)

*  由于我們沒(méi)有配置免密(密鑰),所以這里需要我們輸入輸入兩次密碼,一次時(shí) ssh 連接的密碼,一次是 sudo 提權的密碼;

*  如果使用 ssh 密碼方式運行 ansible,你還需要安裝 sshpass,不然會(huì )有報錯;

it@workstation:~/ansible$ ansible servera -m shell -a "id"
SSH password:
BECOME password[defaults to SSH password]:
servera | FAILED | rc=-1 >>
to use the 'ssh' connection type with passwords, you must install the sshpass program

安裝 sshpass

it@workstation:~/ansible$ sudo apt install sshpass


我要咨詢(xún)