通过ansible自动化部署apache服务 转载

目录

  • 1 安装ansible

  • 2 ansible配置

  • 3 创建角色目录

  • 4 创建角色内的目录结构

  • 5 编写 tasks/main.yml

  • 6 编写 templates/index.html.j2

  • 7 创建 playbook

  • 8 运行 playbook

  • 9 验证结果

  • 10 验证后卸载apache

 


使用Ansible来部署Apache服务是一个很好的选择,因为它可以自动化部署过程,确保所有的服务器上都有相同的配置。以下是一个简单的步骤指南,展示如何使用Ansible来部署Apache服务:

回到顶部

1 安装ansible

在基于Debian的系统中,你可以使用以下命令来安装Ansible:

sudo apt update
sudo apt install ansible

在基于RPM的系统中,你可以使用以下命令:

sudo yum install ansible
# 或者在较新的系统中
sudo dnf install ansible

回到顶部

2 ansible配置

Inventory清单,编辑/etc/ansible/hosts文件,列出要由Ansible管理的受控主机的IP地址或主机名。可以将主机分组以便在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

你可以根据需要修改此文件来更改Ansible的默认设置。例如,你可以设置日志路径、默认模块、禁用SSH密钥检查等。

inventory = /path/to/your/inventory/file # 修改inventory(清单)文件路径
forks = 5	# 定义Ansible 在执行任务时可以在多少个目标主机上并行运行。增加此值可以提高执行速度
remote_user = your_username # 指定 Ansible 用于连接到远程主机的默认用户
private_key_file = /path/to/your/private_key # 指定私钥文件的路径

host_key_checking = False # 设置为 False 可以避免首次连接时的密钥确认提示

sudo_user = your_sudo_username  
# sudo_pass = your_sudo_password  # 不建议直接在配置文件中设置密码

timeout = 10 # 设置 SSH 连接和命令执行的超时时间

log_path = /var/log/ansible.log # 指定 Ansible 日志文件的存储路径

回到顶部

创建角色目录

首先,在 /etc/ansible/roles 下创建 apache 目录:

mkdir -p /etc/ansible/roles/apache

回到顶部

创建角色内的目录结构

在 apache 角色目录下,你需要创建几个子目录:taskstemplatesfileshandlersvarsmeta, 和 defaults(尽管不是所有的都是必要的,但通常 tasks 和 templates 是必须的)。

cd /etc/ansible/roles/apache
mkdir tasks templates

回到顶部

编写 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'

回到顶部

编写 templates/index.html.j2

在 templates/index.html.j2 中,你将使用 Jinja2 模板语法来插入主机名和 IP 地址。

Welcome to {{ ansible_fqdn }} On {{ ansible_default_ipv4.address }}

要使用你在 /etc/ansible/roles 目录下创建的 apache 角色,你需要编写一个 Ansible playbook。以下是如何编写并使用该角色的步骤:

回到顶部

创建 playbook

在 /etc/ansible/ 目录下(或者任何你希望存放 playbook 的地方),创建一个新的 playbook 文件,例如 apache.yml

cd /etc/ansible/
touch apache.yml

然后使用你喜欢的文本编辑器(如 nanovimemacs 等)打开 apache.yml 并输入以下内容:

---
- name: Deploy Apache
  hosts: your_target_group  # 替换为你的目标主机组名,例如 'webservers'
  become: yes  # 使用 sudo 或其他方法提升权限(如果需要)
  roles:
    - apache  # 调用你创建的 apache 角色

请注意,your_target_group 需要替换为你的 Ansible 主机清单中定义的一个主机组名。

回到顶部

运行 playbook

使用 ansible-playbook 命令运行 playbook:

ansible-playbook apache.yml

如果你定义了密码提升(即 become: yes),Ansible 可能会提示你输入 sudo 密码(除非你在 ansible.cfg 中配置了 become_method: sudo 和 become_pass)。
图片

回到顶部

验证结果

一旦 playbook 运行完成,你可以登录到目标机器上检查 Apache 是否已正确安装、启动,并且 /site/index.html 文件是否已正确创建。

你可以使用以下命令来检查 Apache 的状态:

sudo systemctl status httpd

图片

并使用 curl 或 wget 来检查 /site/index.html 文件的内容:

curl http://localhost/site/index.html

图片

或者

wget -qO- http://localhost/site/index.html

注意:如果你是在本地测试,并且 Apache 监听在默认的 80 端口上,那么 http://localhost 应该是正确的。但如果你是在远程机器上运行,你需要将 localhost 替换为远程机器的实际 IP 地址或域名。

回到顶部

10 验证后卸载apache

编写Ansible playbook,该playbook包含必要的步骤来在目标主机上卸载Apache。

--
- hosts: tests // 指定此playbook将在哪些主机上运行
  tasks:
    - name: stop httpd server // 停止httpd服务
      service: name=httpd state=stopped
      notify:
        - remove httpd
  handlers:
    - name: remove httpd
      yum: name=httpd state=removed

运行此playbook,您可以使用以下命令(假设playbook文件名为remove_httpd.yml):

ansible-playbook remove_httpd.yml

图片

链接:https://www.cnblogs.com/ydswin/p/18195602

posted on   我和你并没有不同  阅读(9)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
历史上的今天:
2020-05-22 Kubernetes 学习笔记 权威指南第一章
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示