ansible_hello_world_logo

更新紀錄

  • 2022.12
    1. 刪改原有內容、新增 全域/區域 變數設定方式
    2. 新增 playbook

簡介

Ansible 是一種開源配置工具,Linux 系統管理員使用的自動化工具。
Ansible 能讓系統管理員從一個控制節點(即 Ansible 服務器)管理數百台服務器。
Ansible 不需要任何代理,並且可以在 SSH 和 python 上工作。

註: 不需要代理的意思是目標主機並不需要安裝 Agent。
簡介出自朝陽資工系-王德譽老師#Ansible 前言
朝陽資訊學院兩尊神都提供 「終生保固」 讚讚 XD

此篇會記錄我的第一支 Ansible 腳本配置~

Ansible command shell completion

Adding Ansible command shell completion

官方建議安裝 argcomplete 來為 bash, zsh, tcsh 新增 [tab][tab] 自動補齊的功能。

(WSL Ubuntu 22.04 並沒有自帶 pip,透過以下指令安裝)

1
2
3
4
sudo apt update
sudo apt install python3-pip

pip3 --version
1
2
3
4
5
6
python3 -m pip install --user argcomplete

## Global completion requires bash 4.2
activate-global-python-argcomplete3 --user

> Installing bash completion script /home/eric_xzk/.bash_completion.d/python-argcomplete.sh

Iventory File (hostfile)

這個檔案內主要記錄主機 列表/群組。範例可以參考:

1
less /etc/ansible/hosts

Inventory 基本設定

我的 Inventory

1
2
3
4
5
[netbox]
Eric_NetBox_207 ansible_ssh_host=192.168.207.77

[ALL_207]
192.168.207.[1:253]

其中 netboxALL_207群組名稱 Group Name

netbox 群組用了一個主機別名 (Host alias)
ALL_207 群組用了 range of hosts Adding ranges of hosts

我使用的 inventory file format 屬於 INI。 Ansible 亦支援 YAML format
YAML format example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:

查看 hosts list

查看全部

1
ansible all -i inventory --list-hosts

以我的 iventory file 來說,輸出結果就會是:

1
2
3
4
5
6
7
  hosts (254):
    Eric_NetBox_207
    192.168.207.1
    192.168.207.2
    192.168.207.3
    ...
    192.168.207.253

常用變數

[Dywang] Inventory 變數說明

ansible_ssh_host: 主機 IP / Domain name
ansible_ssh_port: 如果不是預設 SSH Port 22 的話,在這邊指定
ansible_ssh_user: 使用 SSH 登入使用者,預設 root
ansible_ssh_pass: SSH 明文密碼。 不推薦使用 註1
ansible_sudo_pass: SSH sudo 明文密碼。 不推薦使用 註2

註1. 推薦使用 --ask-pass
註2. 推薦使用 --ask-sudo-pass

全域/區域 變數設定方式

若有多重 inventory file 來源,請參考 Managing inventory variable load order

1
2
3
4
5
6
7
[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[all:vars]
ansible_ssh_user=root

[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

我的 ansible.cfg

[Dywang] ansible.cfg 範例

Ansible Configuration Settings

Ansible 支援多種來源設定組態 (Configuration),包含名為 ansible.cfg 的 ini 、環境變數、命令選項、playbook 關鍵字、變數等。
ansible.cfg 讓使用者看到所有可用的組態設定、其預設值、如何設定等。
因有多種組態設定來源,可能不一致,Ansible 以下列順序搜尋,先找到後其他就忽略。\

  1. 環境變數 ANSIBLE_CONFIG
  2. 現在工作目錄下的 ansible.cfg
  3. 使用家目錄下的 ~/.ansible.cfg
  4. 系統 /etc 目錄下的 /etc/ansible/ansible.cfg

ansible.cfg Example

類別

1
cat /etc/ansible/ansible.cfg | egrep -v '^(#|$)'

Tips: -v 代表反轉

查看類別下細項

1
cat /etc/ansible/ansible.cfg | grep '^\[defaults' -A60 | egrep '^(\[|#[a-z])'

我的 ansible.cfg

1
2
[defaults]
host_key_checking = false

加上 host_key_checking = false 略過檢查 SSH fingerprint
Ansible provisioning ERROR! Using a SSH password instead of a key is not possible

Demo

這次 Demo 示範的是屬於 Ansible “Ad-hoc” command.

Ad hoc 是一個拉丁文常用短語。這個短語的意思是「特設的、特定目的的、即席的、臨時的、將就的、專案的」。

整個 command 結構大概會長這樣:

1
$ ansible [pattern] -m [module] -a "[module options]"

【包含了許多範例】Introduction to ad-hoc commands

1
ansible ALL_207 -i inventory -m shell -a "ls -al /root" --ask-pass

嫌太慢嗎?

1
ansible ALL_207 -i inventory -m shell -a "ls -al /root" --ask-pass -f 100

-f --forks 代表 Ansible 會同時使用 N 個 processes 去執行。預設 5 個


Playbook

推薦閱讀

▲ 執行 Playbook 前透過內建 options 來幫助再次確認 task,debug 時也很好用。

▲ 介紹利用 debug, register, fail module 來幫助除錯與檢查。

Bare-Metal ping test Playbook

有新 server 上架,完成所有設定後都必須測試所有網段連線正常。過去都是建一台 VM 之後一個一個切換 Port-Group => nmcli 設定 IP => 使用 ping 測試,這次透過 Ansible Playbook 幫我們自動執行 ping 的步驟,節省一些時間。(切換 Port-Group 變成新增好幾張 NIC,並且綁定 IP)

inventory

1
2
3
4
5
6
7
8
[all:vars]
ansible_ssh_user='root'

[local]
My_Local_CentOS ansible_ssh_host=192.168.60.155

[dc2-vlan]
VLAN-Baremetal-1 ansible_ssh_host=x.x.x.x

ansible.cfg

1
2
[defaults]
host_key_checking = false

ping-playbook.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
---
# - name: Ping Test for local VM
#   hosts: local
#   gather_facts: false
#   tasks:
#   - name: Ping DNS
#     shell: "ping -4 -i 0.01 -c 30 1.1.1.1 | tail -3"
#     register: result

#   - debug:
#       msg: "{{ result.stdout_lines }}"

- name: Ping Test 172 Prefix for bare-metal
  hosts: dc2-vlan
  gather_facts: false
  tasks:
  - name: DC2-M-Web_RD3 x.x.x.0/24
    shell: "ping -4 -i 0.01 -c 30 x.x.x.254 | tail -3"
    register: result
  - debug:
      msg: "{{ result.stdout_lines }}"

debug_module_syntax_error

debug 模組必須獨立使用,不能併在同一個 task 裡面! 不然會報 ERROR! conflicting action statements (--syntax-check 會報在 - name 這邊 Umm… 好喔) 謝謝小飛機~

ping_playbook_0

v0.1 版本即使 ping 沒通也會列在 OK 當中,下一版改進項目 ++