博客 / 詳情

返回

ansible介紹、按照及配置

Ansible是什麼?

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

Ansible 是一款開源的自動化運維工具,由 RedHat 公司開發維護,核心目標是實現:

  • 批量服務器操作(命令執行、軟件安裝、配置修改)
  • 應用部署與生命週期管理
  • 配置自動化(替代手動修改配置文件)
  • 任務編排(按順序執行復雜的運維流程)

它最大的特點是無客户端(Agentless) —— 不需要在被管理的服務器上安裝任何代理程序,僅通過 SSH 協議(默認)或 WinRM(Windows 主機)與目標機器通信,部署和使用門檻極低。

Ansible 核心特性

  • 無代理架構:僅需控制節點(安裝 Ansible 的機器)能 SSH 連接被管理節點,無需在目標機裝軟件,降低維護成本;
  • 模塊化設計:內置上千個模塊(如 yum/apt 安裝軟件、copy 傳輸文件、service 管理服務),覆蓋絕大多數運維場景;
  • 聲明式語法:用 YAML 編寫 Playbook(劇本),只需描述 “最終要達到什麼狀態”,無需寫 “如何達到這個狀態”,Ansible 會自動處理執行邏輯;
  • 冪等性:多次執行同一個操作,結果始終一致(比如 “確保 nginx 服務啓動”,無論執行多少次,最終都是啓動狀態);
  • 跨平台:支持管理 Linux、Windows、網絡設備(華為 / 思科 / 華三)、雲平台(AWS / 阿里雲 / 騰訊雲)等。

Ansible架構

image

Ansible安裝與配置

安裝Ansible

# 首先安裝python3
root@master:~# apt install -y python3
root@master:~# apt install -y python3-pip

# 安裝ansible 
# -i指定安裝源,可以不加-i參數,直接pip3 install ansible
root@master:~# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple   ansible

檢查是否安裝成功

root@master:~# ansible --version
ansible [core 2.14.18]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] (/usr/bin/python3)
  jinja version = 3.1.5
  libyaml = True

修改Ansible的配置文件

Ansible的配置文件默認沒有配置上,需要使用命令進行初始化

# 創建目錄
root@master:~# mkdir -p /etc/ansible
# 初始化配置文件
root@master:~# ansible-config init --disabled > /etc/ansible/ansible.cfg

修改配置文件,修改下面兩個地方就好

root@master:~# vim /etc/ansible/ansible.cfg
# #類似於 ssh -oStrictHostKeyChecking=no
host_key_checking=False
# 日誌
log_path=/var/log/ansible.log

配置Ansible的hosts(被管理的主機清單)配置文件

Ansible的hosts文件作用

  • 定義被管理節點:把所有需要 Ansible 操作的服務器(IP / 主機名 / 域名)列在這個文件裏,Ansible 只能識別並操作這裏面的節點;
  • 分組管理節點:按業務場景(如 web 服務器、數據庫服務器)給節點分組,方便批量操作指定分組(比如只給 web 組裝 nginx);
  • 配置節點屬性:給單個節點 / 分組設置專屬變量(如 SSH 端口、登錄用户、自定義參數),讓 Ansible 適配不同節點的差異;
  • 簡化操作指令:不用每次執行命令都手動寫一堆 IP,直接用分組名(如 webservers)就能指代一組節點。

配置hosts文件

Ansible 默認讀取 /etc/ansible/hosts,你也可以通過 -i 參數指定自定義的 hosts 文件(比如 ansible -i /opt/my_hosts all -m ping)。

root@master:~# cat /etc/ansible/hosts
[web]
10.37.99.63
10.37.120.9
[db]
10.37.99.63
[es]
10.37.120.9

配置機器之間免密登錄

可以查看這篇文章:Linux機器之間配置免密登錄
當然也可以不配置免密登錄,這個後續文章會更新,這裏可以照着這裏做就好

測試使用

  • 測試所有被管理主機執行ping命令
# -m指定ansible的模塊
root@master:~# ansible all -m ping
[WARNING]: Platform linux on host 10.37.99.63 is using the discovered Python interpreter at /usr/bin/python3.11, but future installation of another Python interpreter could
change the meaning of that path. See https://docs.ansible.com/ansible-core/2.14/reference_appendices/interpreter_discovery.html for more information.
10.37.99.63 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3.11"
    },
    "changed": false,
    "ping": "pong"
}
[WARNING]: Platform linux on host 10.37.120.9 is using the discovered Python interpreter at /usr/bin/python3.11, but future installation of another Python interpreter could
change the meaning of that path. See https://docs.ansible.com/ansible-core/2.14/reference_appendices/interpreter_discovery.html for more information.
10.37.120.9 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3.11"
    },
    "changed": false,
    "ping": "pong"
}

測試在db主機執行shell命令

# -m指定ansible的模塊,-a指定模塊命令
root@master:~# ansible db -m command -a 'hostname -I'
[WARNING]: Platform linux on host 10.37.99.63 is using the discovered Python interpreter at /usr/bin/python3.11, but future installation of another Python interpreter could
change the meaning of that path. See https://docs.ansible.com/ansible-core/2.14/reference_appendices/interpreter_discovery.html for more information.
10.37.99.63 | CHANGED | rc=0 >>
10.37.99.63 172.17.0.1 100.112.111.195 fdbd:dc01:ff:318:71d3:f428:1bc5:b474 

去除ansible返回的warning警告

root@master:~# vim /etc/ansible/ansible.cfg 
# 找到下面這一行
interpreter_python=auto_silent

測試

root@master:~# ansible db -m command -a 'hostname -I'
# 發現warning警告已經沒有了
10.37.99.63 | CHANGED | rc=0 >>
10.37.99.63 172.17.0.1 100.112.111.195 fdbd:dc01:ff:318:71d3:f428:1bc5:b474
user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.