通過(guò)ansible自動(dòng)化部署apache服務(wù)
共 4172字,需瀏覽 9分鐘
·
2024-05-20 21:41
目錄
1 安裝ansible
2 ansible配置
3 創(chuàng)建角色目錄
4 創(chuàng)建角色內(nèi)的目錄結(jié)構(gòu)
5 編寫 tasks/main.yml
6 編寫 templates/index.html.j2
7 創(chuàng)建 playbook
8 運(yùn)行 playbook
9 驗(yàn)證結(jié)果
10 驗(yàn)證后卸載apache
使用Ansible來(lái)部署Apache服務(wù)是一個(gè)很好的選擇,因?yàn)樗梢宰詣?dòng)化部署過(guò)程,確保所有的服務(wù)器上都有相同的配置。以下是一個(gè)簡(jiǎn)單的步驟指南,展示如何使用Ansible來(lái)部署Apache服務(wù):
回到頂部
1 安裝ansible
在基于Debian的系統(tǒng)中,你可以使用以下命令來(lái)安裝Ansible:
sudo apt update
sudo apt install ansible
在基于RPM的系統(tǒng)中,你可以使用以下命令:
sudo yum install ansible
# 或者在較新的系統(tǒng)中
sudo dnf install ansible
回到頂部
2 ansible配置
Inventory清單,編輯/etc/ansible/hosts文件,列出要由Ansible管理的受控主機(jī)的IP地址或主機(jī)名??梢詫⒅鳈C(jī)分組以便在Playbooks中引用它們
[tests]
192.168.178.222
[webservers]
192.168.178.100
192.168.178.101
[dbservers]
192.168.178.103
192.168.178.104
Ansible主配置文件 /etc/ansible/ansible.cfg
你可以根據(jù)需要修改此文件來(lái)更改Ansible的默認(rèn)設(shè)置。例如,你可以設(shè)置日志路徑、默認(rèn)模塊、禁用SSH密鑰檢查等。
inventory = /path/to/your/inventory/file # 修改inventory(清單)文件路徑
forks = 5 # 定義Ansible 在執(zhí)行任務(wù)時(shí)可以在多少個(gè)目標(biāo)主機(jī)上并行運(yùn)行。增加此值可以提高執(zhí)行速度
remote_user = your_username # 指定 Ansible 用于連接到遠(yuǎn)程主機(jī)的默認(rèn)用戶
private_key_file = /path/to/your/private_key # 指定私鑰文件的路徑
host_key_checking = False # 設(shè)置為 False 可以避免首次連接時(shí)的密鑰確認(rèn)提示
sudo_user = your_sudo_username
# sudo_pass = your_sudo_password # 不建議直接在配置文件中設(shè)置密碼
timeout = 10 # 設(shè)置 SSH 連接和命令執(zhí)行的超時(shí)時(shí)間
log_path = /var/log/ansible.log # 指定 Ansible 日志文件的存儲(chǔ)路徑
回到頂部
3 創(chuàng)建角色目錄
首先,在 /etc/ansible/roles 下創(chuàng)建 apache 目錄:
mkdir -p /etc/ansible/roles/apache
回到頂部
4 創(chuàng)建角色內(nèi)的目錄結(jié)構(gòu)
在 apache 角色目錄下,你需要?jiǎng)?chuàng)建幾個(gè)子目錄:tasks, templates, files, handlers, vars, meta, 和 defaults(盡管不是所有的都是必要的,但通常 tasks 和 templates 是必須的)。
cd /etc/ansible/roles/apache
mkdir tasks templates
回到頂部
5 編寫 tasks/main.yml
在 tasks/main.yml 中,你將定義安裝和配置 Apache 的步驟。
---
- name: Install httpd
yum:
name: httpd
state: present
- name: Start httpd service
service:
name: httpd
state: started
enabled: yes
- name: Stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: Create /site directory
file:
path: /var/www/html/site
state: directory
mode: '0755'
- name: Template index.html
template:
src: index.html.j2
dest: /var/www/html/site/index.html
mode: '0644'
回到頂部
6 編寫 templates/index.html.j2
在 templates/index.html.j2 中,你將使用 Jinja2 模板語(yǔ)法來(lái)插入主機(jī)名和 IP 地址。
Welcome to {{ ansible_fqdn }} On {{ ansible_default_ipv4.address }}
要使用你在 /etc/ansible/roles 目錄下創(chuàng)建的 apache 角色,你需要編寫一個(gè) Ansible playbook。以下是如何編寫并使用該角色的步驟:
回到頂部
7 創(chuàng)建 playbook
在 /etc/ansible/ 目錄下(或者任何你希望存放 playbook 的地方),創(chuàng)建一個(gè)新的 playbook 文件,例如 apache.yml:
cd /etc/ansible/
touch apache.yml
然后使用你喜歡的文本編輯器(如 nano, vim, emacs 等)打開(kāi) apache.yml 并輸入以下內(nèi)容:
---
- name: Deploy Apache
hosts: your_target_group # 替換為你的目標(biāo)主機(jī)組名,例如 'webservers'
become: yes # 使用 sudo 或其他方法提升權(quán)限(如果需要)
roles:
- apache # 調(diào)用你創(chuàng)建的 apache 角色
請(qǐng)注意,your_target_group 需要替換為你的 Ansible 主機(jī)清單中定義的一個(gè)主機(jī)組名。
回到頂部
8 運(yùn)行 playbook
使用 ansible-playbook 命令運(yùn)行 playbook:
ansible-playbook apache.yml
如果你定義了密碼提升(即 become: yes),Ansible 可能會(huì)提示你輸入 sudo 密碼(除非你在 ansible.cfg 中配置了 become_method: sudo 和 become_pass)。
回到頂部
9 驗(yàn)證結(jié)果
一旦 playbook 運(yùn)行完成,你可以登錄到目標(biāo)機(jī)器上檢查 Apache 是否已正確安裝、啟動(dòng),并且 /site/index.html 文件是否已正確創(chuàng)建。
你可以使用以下命令來(lái)檢查 Apache 的狀態(tài):
sudo systemctl status httpd
并使用 curl 或 wget 來(lái)檢查 /site/index.html 文件的內(nèi)容:
curl http://localhost/site/index.html
或者
wget -qO- http://localhost/site/index.html
注意:如果你是在本地測(cè)試,并且 Apache 監(jiān)聽(tīng)在默認(rèn)的 80 端口上,那么 http://localhost 應(yīng)該是正確的。但如果你是在遠(yuǎn)程機(jī)器上運(yùn)行,你需要將 localhost 替換為遠(yuǎn)程機(jī)器的實(shí)際 IP 地址或域名。
回到頂部
10 驗(yàn)證后卸載apache
編寫Ansible playbook,該playbook包含必要的步驟來(lái)在目標(biāo)主機(jī)上卸載Apache。
--
- hosts: tests // 指定此playbook將在哪些主機(jī)上運(yùn)行
tasks:
- name: stop httpd server // 停止httpd服務(wù)
service: name=httpd state=stopped
notify:
- remove httpd
handlers:
- name: remove httpd
yum: name=httpd state=removed
運(yùn)行此playbook,您可以使用以下命令(假設(shè)playbook文件名為remove_httpd.yml):
ansible-playbook remove_httpd.yml
鏈接:https://www.cnblogs.com/ydswin/p/18195602
(版權(quán)歸原作者所有,侵刪)
