Centos8 安裝並使用Ansible(二)

夢共裡醉發表於2022-10-16
導讀 Ansible 是一個開源的配置管理工具,我們用於自動化任務、部署應用程式。使用 Ansible,您可以自動執行日常任務,例如更新系統、安裝軟體、和配置服務。
配置/etc/hosts檔案

覺得用ip地址麻煩,可以在Master控制節點中的 /etc/hosts檔案中新增主機名對應著節點名稱:

[root@Master ~]# cat /etc/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.43.131 Master
192.168.43.165 node1
192.168.43.166 node2
192.168.43.167 node3

Centos8 安裝並使用Ansible(二)Centos8 安裝並使用Ansible(二)
在上一篇文章中,建立了一個名為 Master 的控制節點和四個受控節點(node1、node2、node3 和 node4)。現在,為了讓 Ansible 與受控節點通訊,需要為Master節點的ansible使用者配置免密登入四個受控節點。

ansible使用者配置ssh免密登入

在Master和node1-node4中都需要建立 ansible使用者:

# 在Master節點建立使用者,新增到wheel組,並設定ansible使用者密碼
[root@Master ~]# useradd ansible
[root@Master ~]# usermod -aG wheel ansible
[root@Master ~]# echo '123'|passwd --stdin ansible
Changing password for user ansible.
passwd: all authentication tokens updated successfully.
# 在node1節點建立使用者,新增到wheel組,並設定ansible使用者密碼
[root@node1 ~]# useradd ansible
[root@node1 ~]# usermod -aG wheel ansible
[root@node1 ~]# echo '123'|passwd --stdin ansible
Changing password for user ansible.
passwd: all authentication tokens updated successfully.
# 在node2節點建立使用者,新增到wheel組,並設定ansible使用者密碼
[root@node2 ~]# useradd ansible
[root@node2 ~]# usermod -aG wheel ansible
[root@node2 ~]# echo '123'|passwd --stdin ansible
Changing password for user ansible.
passwd: all authentication tokens updated successfully.
# 在node3節點建立使用者,新增到wheel組,並設定ansible使用者密碼
[root@node3 ~]# useradd ansible
[root@node3 ~]# usermod -aG wheel ansible
[root@node3 ~]# echo '123'|passwd --stdin ansible
Changing password for user ansible.
passwd: all authentication tokens updated successfully.

在Master和node1-node4中執行 visudo,將 %wheel ALL=(ALL) NOPASSWD: ALL前面的註釋去掉,這樣ansible使用者使用sudo時不需要輸入密碼了:

[root@Master ~]# visudo

Centos8 安裝並使用Ansible(二)Centos8 安裝並使用Ansible(二)
在控制節點 Master上切換到ansible使用者,生成 SSH 金鑰,然後將 SSH 公鑰複製到所有受控節點。

[root@Master ~]# su - ansible
[ansible@Master ~]$ ssh-keygen

Centos8 安裝並使用Ansible(二)Centos8 安裝並使用Ansible(二)
現在,將 SSH 公鑰複製到所有受控節點,這讓 ansible 使用者無需輸入密碼即可登入所有節點主機了:

[ansible@Master ~]$ ssh-copy-id ansible@node1
[ansible@Master ~]$ ssh-copy-id ansible@node2
[ansible@Master ~]$ ssh-copy-id ansible@node3

Centos8 安裝並使用Ansible(二)Centos8 安裝並使用Ansible(二)

配置檔案

預設的 Ansible 配置檔案位於  /etc/ansible/ansible.cfg 下。Ansible 的大部分設定都可以使用此配置檔案進行修改以滿足環境需求,下面瞭解一下 Ansible 在哪裡搜尋配置檔案,Ansible 按以下順序搜尋配置檔案,ansible找到的第一個配置檔案,然後忽略其餘檔案:

  • $ANSIBLE_CONFIG如果設定了此變數
  • ansible.cfg如果在當前目錄中
  • ~/.ansible.cfg如果它在使用者的主目錄中。
  • /etc/ansible/ansible.cfg預設的配置檔案

預設清單檔案位於 /etc/ansible/hosts 中,但可以在配置檔案中更改此位置。您還可以透過 -i選項指定要使用的清單檔案。

下面在ansible的家目錄建立一個 ~/.ansible.cfg配置檔案,然後建立一個 inventory清單檔案:

[ansible@Master ~]$ touch ~/.ansible.cfg
[ansible@Master ~]$ touch inventory

~/.ansible.cfg中制定inventory檔案的位置:

[ansible@Master ~]$ cat ~/.ansible.cfg 
[defaults]
inventory = /home/ansible/inventory

Centos8 安裝並使用Ansible(二)Centos8 安裝並使用Ansible(二)
下面將主機節點寫入到inventory檔案中,內容中建立了三個組,nodes、test、prod:

[ansible@Master ~]$ vim inventory 
[nodes]
node1
node2
node3
[test]
node1
[prod]
node2
node3

Centos8 安裝並使用Ansible(二)Centos8 安裝並使用Ansible(二)

Ansible ad-hoc

ad-hoc可以在命令列快速執行命令,不需要編寫playbook。

使用ad-hoc檢查節點的連通性

使用 ping 模組檢查與節點主機的連線。

[ansible@Master ~]$ ansible all -m ping

Centos8 安裝並使用Ansible(二)Centos8 安裝並使用Ansible(二)
在上面的命令中,all 表示讓 Ansible 在所有主機上執行此命令。

使用ad-hoc管理包

使用 Ansible 的ad-hoc命令,還可以將軟體包安裝到節點主機。下面例項是將httpd安裝在 [test]組中:

[ansible@Master ~]$ ansible test -b -m yum -a "name=httpd state=present"
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Nothing to do",
    "rc": 0,
    "results": []
}

Centos8 安裝並使用Ansible(二)Centos8 安裝並使用Ansible(二)

使用ad-hoc管理服務

由於在上一步中成功安裝了 httpd 伺服器,下面使用 Ansible 的 ad-hoc 命令啟動和啟用 httpd 服務,以便 Web 伺服器啟動並執行。

[ansible@Master ~]$ ansible test -b -m service -a "name=httpd enabled=yes state=started"

Centos8 安裝並使用Ansible(二)Centos8 安裝並使用Ansible(二)

Playbook

與 Ansible ad-hoc命令不同,Ansible 劇本可以儲存和重複使用。每個playbook 由一個或多個playbook組成。下面是一個簡單的 Ansible 劇本,在不同的節點安裝不同的服務。下面例項檔名為httpd.yaml,用於在 prod 組安裝 httpd 服務,啟動服務,防火牆開放80埠。然後再所有節點安裝git安裝包:

# 在vim編輯器中新增這條資訊,讓縮排更方便一些。
[ansible@Master ~]$ echo 'autocmd FileType yaml setlocal ai ts=2 sw=2 et' > .vimrc
[ansible@Master ~]$ vim httpd.yml 
---
- name: Install httpd on prod group.
  hosts: prod
  become: yes
  tasks:
  - name: Install httpd
    yum:
      name: httpd
      state: latest
  - name: enable httpd service
    service:
      name: httpd
      enabled: yes
    notify: restart httpd
  handlers:
  - name: restart httpd
    service:
      name: httpd
      state: restarted
      
- name: Install git on all hosts
  hosts: all
  become: yes
  tasks:
  - name: Install Git
    yum:
      name: git
      state: latest

編寫完成playbook時,可以試執行一下,然後再真正的執行:

[ansible@Master ~]$ ansible-playbook httpd.yml -C
[ansible@Master ~]$ ansible-playbook httpd.yml

Centos8 安裝並使用Ansible(二)Centos8 安裝並使用Ansible(二)
Centos8 安裝並使用Ansible(二)Centos8 安裝並使用Ansible(二)

總結

Ansible 簡單、易於設定且功能強大。Ansible 是無代理的,這使系統管理員可以輕鬆開始自動化並花費更少的時間進行配置。

本文原創地址:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69955379/viewspace-2850389/,如需轉載,請註明出處,否則將追究法律責任。

相關文章