祗疼妳一个 发表于 2024-5-20 17:35:04

在Linux中通过ansible自动化部署apache服务

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

在基于Debian的系统中,你可以使用以下下令来安装Ansible:
sudo apt update
sudo apt install ansible在基于RPM的系统中,你可以使用以下下令:
sudo yum install ansible
# 或者在较新的系统中
sudo dnf install ansible2 ansible设置

Inventory清单,编辑/etc/ansible/hosts文件,列出要由Ansible管理的受控主机的IP地址或主机名。可以将主机分组以便在Playbooks中引用它们

192.168.178.222


192.168.178.100
192.168.178.101


192.168.178.103
192.168.178.104Ansible主设置文件        /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 日志文件的存储路径3 创建脚色目录

首先,在 /etc/ansible/roles 下创建 apache 目录:
mkdir -p /etc/ansible/roles/apache4 创建脚色内的目录结构

在 apache 脚色目录下,你必要创建几个子目录:tasks, templates, files, handlers, vars, meta, 和 defaults(只管不是所有的都是须要的,但通常 tasks 和 templates 是必须的)。
cd /etc/ansible/roles/apache
mkdir tasks templates5 编写 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 模板语法来插入主机名和 IP 地址。
Welcome to {{ ansible_fqdn }} On {{ ansible_default_ipv4.address }}要使用你在 /etc/ansible/roles 目录下创建的 apache 脚色,你必要编写一个 Ansible playbook。以下是如何编写并使用该脚色的步调:
7 创建 playbook

在 /etc/ansible/ 目录下(或者任何你希望存放 playbook 的地方),创建一个新的 playbook 文件,例如 apache.yml:
cd /etc/ansible/
touch apache.yml然后使用你喜好的文本编辑器(如 nano, vim, emacs 等)打开 apache.yml 并输入以下内容:
---
- name: Deploy Apache
hosts: your_target_group# 替换为你的目标主机组名,例如 'webservers'
become: yes# 使用 sudo 或其他方法提升权限(如果需要)
roles:
    - apache# 调用你创建的 apache 角色请注意,your_target_group 必要替换为你的 Ansible 主机清单中界说的一个主机组名。
8 运行 playbook

使用 ansible-playbook 下令运行 playbook:
ansible-playbook apache.yml如果你界说了密码提升(即 become: yes),Ansible 可能会提示你输入 sudo 密码(除非你在 ansible.cfg 中设置了 become_method: sudo 和 become_pass)。
https://img2024.cnblogs.com/blog/1307305/202405/1307305-20240516113656056-974001332.png
9 验证结果

一旦 playbook 运行完成,你可以登录到目标呆板上检查 Apache 是否已正确安装、启动,并且 /site/index.html 文件是否已正确创建。
你可以使用以下下令来检查 Apache 的状态:
sudo systemctl status httpdhttps://img2024.cnblogs.com/blog/1307305/202405/1307305-20240516113744647-949060604.png
并使用 curl 或 wget 来检查 /site/index.html 文件的内容:
curl http://localhost/site/index.htmlhttps://img2024.cnblogs.com/blog/1307305/202405/1307305-20240516113806210-1479606968.png
或者
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.ymlhttps://img2024.cnblogs.com/blog/1307305/202405/1307305-20240516113453997-165273243.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 在Linux中通过ansible自动化部署apache服务