[Linux]使用Ansible自動化管理
Ansible可實現自動化管理遠端系統,主要組成環境有:
- Control node - A system on which Ansible is installed.
- Managed node - A remote system, or host.
- Inventory - A list of managed nodes that are logically organized.
Control Node
根據Ansible(官方文件)進行安裝,這邊Control Node用的OS是Ubuntu。$ sudo apt install ansible
檢查安裝結果
Managed node
Managed node準備建立虛擬機並安裝CentOS/7。
Inventory
接著可以開始撰寫腳本,假設要安裝指定packages,腳本架構大致如下:
aio.yaml
inventory.ini
roles
|_install_packages
|_tasks
|_main.yml
編輯aio.yaml:
- hosts - 決定要控制哪些host主機
- roles - 引入相關檔案、變數、任務等等,並提升可用性
- hosts: all
roles:
- role: install_packages
編輯/roles/install_packages/tasks/main.yml
---
# tasks file for install_packages
- name: Install epel-release
package:
name:
- epel-release
- name: Install packages
package:
name:
- net-tools
- vim
- git
- lsof
- python3
- python3-libs
- python3-devel
編輯inventory.ini:
- ansible_host - 編輯Managed host IP address
- ansible_port - 編輯Managed host port
- ansible_user - 編輯Managed host帳號
- ansible_password - 編輯Managed host密碼
- ansible_sudo_pass - 編輯Managed host sudo 密碼
[web]
web ansible_host=192.168.122.1 ansible_port=22 ansible_user='shock' ansible_password="shock123" ansible_sudo_pass="shock123"
執行Playbook
ansible-playbook -i inventory.ini aio.yaml
留言
張貼留言