測試環境 CentOS 9 Stream (虛擬機)
架構需求如下系統可以分為 Control node ( Ansible 控制端) 與 Managed nodes (被 Ansible 控制端)
- Control node ( Ansible 控制端) IP: 192.168.31.131
- Managed nodes (被 Ansible 控制端) IP: 192.168.31.133 與 192.168.31.178
在執行 Ansible 時 會有一段是 Gathering Facts ,這是做什麼的,我們來看一下要如何應用這一塊,參考資料 – https://chusiang.gitbooks.io/automate-with-ansible/content/13.how-to-get-the-managed-node-facts-with-setup.html
TASK [Gathering Facts] ************************************************************************************************* ok: [node1] ok: [node2]
- 建立 Ansible 專案目錄
[root@localhost ~]# mkdir -p ansible_project/inventory [root@localhost ~]# cd ansible_project/ [root@localhost ansible_project]#
- 創建 Inventory 文件
在 inventory/hosts 文件中,定義你的節點:[root@localhost ansible_project]# vi inventory/hosts [nodes] node1 ansible_host=192.168.31.133 node2 ansible_host=192.168.31.178
- 使用 Ad-Hoc Commands 呼叫 setup
[root@localhost ansible_project]# ansible nodes -m setup -i inventory/hosts node2 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.31.178" ], "ansible_all_ipv6_addresses": [ "fe80::20c:29ff:fe6f:ce28" ], "ansible_apparmor": { "status": "disabled" }, "ansible_architecture": "x86_64", "ansible_bios_date": "11/12/2020", "ansible_bios_vendor": "Phoenix Technologies LTD", "ansible_bios_version": "6.00", "ansible_board_asset_tag": "NA", "ansible_board_name": "440BX Desktop Reference Platform", "ansible_board_serial": "None", "ansible_board_vendor": "Intel Corporation", "ansible_board_version": "None", "ansible_chassis_asset_tag": "No Asset Tag", "ansible_chassis_serial": "None", "ansible_chassis_vendor": "No Enclosure", "ansible_chassis_version": "N/A", "ansible_cmdline": { "BOOT_IMAGE": "(hd0,msdos1)/vmlinuz-5.14.0-452.el9.x86_64", "crashkernel": "1G-4G:192M,4G-64G:256M,64G-:512M", "quiet": true, "rd.lvm.lv": "cs/swap", "resume": "/dev/mapper/cs-swap", "rhgb": true, "ro": true, "root": "/dev/mapper/cs-root" }, ....
因為資訊太多沒抓出所有的資料.
- 參數 -a filter=
可以透過參數 -a filter= 來指定要看哪一個參數值.[root@localhost ansible_project]# ansible nodes -m setup -i inventory/hosts -a "filter=ansible_distribution*" node1 | SUCCESS => { "ansible_facts": { "ansible_distribution": "CentOS", "ansible_distribution_file_parsed": true, "ansible_distribution_file_path": "/etc/centos-release", "ansible_distribution_file_variety": "CentOS", "ansible_distribution_major_version": "9", "ansible_distribution_release": "Stream", "ansible_distribution_version": "9", "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false } node2 | SUCCESS => { "ansible_facts": { "ansible_distribution": "CentOS", "ansible_distribution_file_parsed": true, "ansible_distribution_file_path": "/etc/centos-release", "ansible_distribution_file_variety": "CentOS", "ansible_distribution_major_version": "9", "ansible_distribution_release": "Stream", "ansible_distribution_version": "9", "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false }
這些資料可以怎麼應用呢?
我們可以利用 ansible_distribution 來判斷系統使用哪一個系統,進而判斷安裝程式需要使用 apt – Ubuntu 或是 dnf – CentOS / RHEL / Fedora
[root@localhost ansible_project]# ansible nodes -m setup -i inventory/hosts -a "filter=ansible_pkg_mgr" node2 | SUCCESS => { "ansible_facts": { "ansible_pkg_mgr": "dnf", "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false } node1 | SUCCESS => { "ansible_facts": { "ansible_pkg_mgr": "dnf", "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false }
實際範例 (建議可以用 nano 的編輯器, copy Paste 以下資料就不會出問題)
[root@localhost ansible_project]# vim setup_vim.yml --- - name: Setup the vim hosts: all become: true tasks: # Debian, Ubuntu. - name: install apt packages apt: name=vim state=present when: ansible_pkg_mgr == "apt" # CentOS. - name: install dnf packages dnf: name=vim-minimal state=present when: ansible_pkg_mgr == "dnf"
執行
[root@localhost ansible_project]# ansible-playbook -i inventory/hosts setup_vim.yml PLAY [Setup the vim] *************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************* ok: [node2] ok: [node1] TASK [install apt packages] ******************************************************************************************** skipping: [node1] skipping: [node2] TASK [install dnf packages] ******************************************************************************************** ok: [node2] ok: [node1] PLAY RECAP ************************************************************************************************************* node1 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 node2 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
因為 2 台 Managed nodes (被 Ansible 控制端) 都是 CentOS 所以都是使用 dnf 來安裝套件.
沒有解決問題,試試搜尋本站其他內容