操bb国片四区三区,亚洲一片内射无码,91短视频福利导航,蜜桃臀成人免费在线视频观看

Ansible 介紹

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

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

什么是 Ansible

Ansible 是一個簡單,強大且無代理的自動化語言。

Ansible 的好處:

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

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

無代理:不需要在客戶端安裝額外的 agent

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

Ansible 是如何工作的

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

架構(gòu)

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

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

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

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

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

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

安裝 Ansible

在 Ubuntu 上安裝 ansible

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

安裝完成后,你可以通過 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 提供的默認(rèn)主機配置文件:

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

* 只有 root 用戶才有權(quán)限編輯。

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

一般情況,我們直接復(fù)制默認(rèn)配置文件,然后根據(jù)需要修改復(fù)制過來的配置文件。

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)建一個用于測試 Ansible 的工作目錄,然后再創(chuàng)建配置文件。

當(dāng)我們擁有多個配置文件時,配置文件生效的順序為:

  • 當(dāng)前目錄下:./ansible.cfg

  • home 目錄下:~/ansible.cfg

  • 默認(rèn)配置文件:/etc/ansible/ansible.cfg

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

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 工作目錄下的配置文件是無效的。

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

defaults 模塊:

inventory: 指定清單文件路徑

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

remote_user: 遠(yuǎn)程連接時使用的用戶,默認(rèn)使用當(dāng)前用戶

ask_pass: 連接時是否詢問輸入密碼(如果沒有配置密鑰登錄,需要配置該選項為 true。)

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

become: 是否開啟切換用戶

become_method: 如何切換用戶

become_user: 切換到那個用戶

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 提供了一個示例清單文件,并給我們提供了一些常規(guī)的示例:

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

EX 2: 通過 [ ] 指定主機組的名稱,如,webservers,下面直到下一個組名(dbservers)前結(jié)束的都是屬于該組的主機,即使主機與主機之間存在空行,但如沒有到下一個組名,他們依然屬于同一個主機組;如果某些主機之間有某些順序關(guān)系,你可以通過簡寫,將他們放到同一行,如示例中的 “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

在該清單中,我們使用組嵌套,這個是前面示例中沒有的,web 組是 prod 組的子組。

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

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

我們可以通過 ansible 命令來驗證我們的主機清單文件

同時 Ansible 還有兩個默認(rèn)組:

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 中,主機可以同時屬于多個組。

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

運行 ansible 臨時命令

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

通過臨時命令查看連接到遠(yuǎn)程主機的用戶信息

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)

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

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

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


我要咨詢