Ansible-role自动化部署httpd
1.安装ansible-core
dnf install ansible-core
[root@ansible ~]# rpm -qa | grep ansible
ansible-core-2.14.18-1.el9.x86_64
2.设置SSH免密登录节点
Ansible 会使用被控节点有的连接方式来管理被控节点,如通过 SSH 管理 Linux,因此 Ansible 是无代理的,不需要在被控节点安装 Agent。
1.
[root@ansible ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:2/Guo4MBlWKZUQmNVWnhwo45O1UqDF/yryEAV9v83OU root@ansible
The key's randomart image is:
+---[RSA 3072]----+
| +Xo+oo |
| .*==.o |
|. o.oo*.o . |
| o +.B * . o |
| . B.= S o E |
| . =.. o o |
| + .oo . . |
| o.o. .. |
| . .o.o. |
+----[SHA256]-----+
[root@ansible ~]#
[root@ansible ~]#
[root@ansible ~]#
2.
[root@ansible ~]# ssh-copy-id root@192.168.64.139
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.64.139 (192.168.64.139)' can't be established.
ED25519 key fingerprint is SHA256:TX8OewmGDTiadSgmuApvxQo6bp741hgqAgmF9jsQiQE.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.64.139's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.64.139'"
and check to make sure that only the key(s) you wanted were added.
3.
[root@ansible ~]# ssh-copy-id root@192.168.64.140
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.64.140 (192.168.64.140)' can't be established.
ED25519 key fingerprint is SHA256:TX8OewmGDTiadSgmuApvxQo6bp741hgqAgmF9jsQiQE.
This host key is known by the following other names/addresses:
~/.ssh/known_hosts:1: 192.168.64.139
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.64.140's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.64.140'"
and check to make sure that only the key(s) you wanted were added.
4.节点主机名也设置
[root@ansible ~]# ssh-copy-id node1
[root@ansible ~]# ssh-copy-id node2
5.测试
[root@ansible ~]# ssh root@192.168.64.139
Register this system with Red Hat Insights: rhc connect
Example:
# rhc connect --activation-key <key> --organization <org>
The rhc client and Red Hat Insights will enable analytics and additional
management capabilities on your system.
View your connected systems at https://console.redhat.com/insights
You can learn more about how to register your system
using rhc at https://red.ht/registration
Last login: Wed Mar 11 16:57:46 2026 from 192.168.64.100
[root@node1 ~]#
[root@ansible ~]# ssh node2
Register this system with Red Hat Insights: rhc connect
Example:
# rhc connect --activation-key <key> --organization <org>
The rhc client and Red Hat Insights will enable analytics and additional
management capabilities on your system.
View your connected systems at https://console.redhat.com/insights
You can learn more about how to register your system
using rhc at https://red.ht/registration
Last login: Wed Mar 11 18:31:30 2026 from 192.168.64.134
[root@node2 ~]# exit
logout
Connection to node2 closed.
3.初始化role 目录结构
[root@ansible ~]# mkdir roles
[root@ansible ~]# cd roles/
[root@ansible roles]# ansible-galaxy role init httpd
- Role httpd was created successfully
[root@ansible roles]#
[root@ansible roles]# ls
httpd
[root@ansible roles]# tree httpd/
httpd/
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
4.编写主机清单&剧本
/inventory/hosts和site.yml
[root@ansible roles]# mkdir inventory
[root@ansible roles]# cat > inventory/hosts <<EOF
> [server]
> node1
> node2
> EOF
[root@ansible roles]# vim site.yml
[root@ansible roles]# cat site.yml
- hosts: server
roles:
- role: httpd
5.配置补充目录结构
最终目的是部署httpd,则需要:
1.安装httpd
2.配置httpd
用到/etc/httpd/conf/httpd.conf,将这个文件放到ansible目录下=配置成功
2.1先安装httpd复制httpd.conf & 卸载httpd
2.2再将httpd.conf复制到ansible(task做)
3.编写首页
4.开启服务
5.检测
5.1获取httpd.conf
httpd.conf是 Apache HTTP Server(httpd)的主配置文件,相当于 Web 服务器的"控制中心"或"操作手册"。它告诉 httpd 程序如何运行、监听哪个端口、网站文件存放在哪里、允许谁访问等所有行为。
[root@ansible roles]# dnf install httpd -y
[root@ansible ~]# cp -r /etc/httpd/conf/httpd.conf ./
[root@ansible ~]# dnf remove httpd -y
[root@ansible ~]# mv httpd.conf roles/httpd/files/
5.2编写触发器(/handlers)
触发器(Handler)是Ansible中一种特殊的任务,它只有在被其他任务"通知"(notify)时才会执行,而且在所有普通任务执行完毕后才会运行。
[root@ansible roles]# vim httpd/handlers/main.yml
[root@ansible roles]# cat httpd/handlers/main.yml
- name: restart
systemd:
name: httpd
state: restart
5.3编写任务(/task)
分为4个:install,config,index,service
1.#main.yml
[root@ansible httpd]# vim tasks/main.yml
[root@ansible httpd]# cat tasks/main.yml
- include: install_httpd.yml
- include: config_httpd.yml
- include: index_httpd.yml
- include: service_httpd.yml
2.#install_httpd.yml
[root@ansible httpd]# cat tasks/install_httpd.yml
- name: install httpd
dnf:
name: httpd
state: installed
3.#config_httpd.yml
#其实就是copy httpd.conf,需要预先拿到httpd.conf
[root@ansible httpd]# cat tasks/config_httpd.yml
- name: config httpd
copy:
src: httpd.conf #相对路径,默认从files里面
dest: /etc/httpd/conf/httpd.conf
notify: restart
#notify: restart实现了"配置即代码"的核心理念——只有当配置真正变化时,才采取相应的行动(重启服务),既保证了配置的正确性,又避免了不必要的服务中断。
4.#index_httpd.yml
[root@ansible httpd]# cat tasks/index_httpd.yml
- name: index httpd
template:
src: index.html
dest: /var/www/html/index.html
5.#service_httpd.yml
[root@ansible httpd]# cat tasks/service_httpd.yml
- name: service httpd
systemd:
name: httpd
state: started
关于template模板
index_httpd.yml中:
使用
template模块。这里比copy更强大,因为它会渲染变量。它将templates/index.html(这是一个包含Jinja2语法的模板文件)复制到被控端的/var/www/html/index.html。
5.4编写网页模块(/templates)
[root@ansible httpd]# cat templates/index.html
<html>
<head>
<meta charset="UTF-8">
<title>index.html< title>
< head>
<body>
<h1>{{ index_content }}< h1> #这里是变量,要在var下面定义
< body>
< html>
5.5定义角色变量(/var)
[root@ansible httpd]# cat vars/main.yml
index_content: "首页"
5.6编写ansible.cfg
运行ansible-playbook时,不用指定节点
[root@ansible roles]# cat ansible.cfg
[defaults]
inventory = inventory/hosts
5.7目录结构
[root@ansible roles]# tree .
.
├── ansible.cfg
├── httpd
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ │ └── httpd.conf
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── README.md
│ ├── tasks
│ │ ├── config_httpd.yml
│ │ ├── index_httpd.yml
│ │ ├── install_httpd.yml
│ │ ├── main.yml
│ │ └── service_httpd.yml
│ ├── templates
│ │ └── index.html
│ ├── tests
│ │ ├── inventory
│ │ └── test.yml
│ └── vars
│ └── main.yml
├── inventory
│ └── hosts
└── site.yml
6.执行playbook
[root@ansible roles]# ansible-playbook site.yml
[DEPRECATION WARNING]: "include" is deprecated, use include_tasks/import_tasks instead. See
https://docs.ansible.com/ansible-core/2.14/user_guide/playbooks_reuse_includes.html for
details. This feature will be removed in version 2.16. Deprecation warnings can be disabled
by setting deprecation_warnings=False in ansible.cfg.
PLAY [server] ******************************************************************************
TASK [Gathering Facts] *********************************************************************
ok: [node1]
ok: [node2]
TASK [httpd : install httpd] ***************************************************************
changed: [node1]
changed: [node2]
TASK [httpd : config httpd] ****************************************************************
ok: [node2]
ok: [node1]
TASK [httpd : index httpd] *****************************************************************
changed: [node1]
changed: [node2]
TASK [httpd : service httpd] ***************************************************************
changed: [node1]
changed: [node2]
PLAY RECAP *********************************************************************************
node1 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
7.测试(被控主机上)
#node1
[root@node1 ~]# curl 127.0.0.1
<html>
<head>
<meta charset="UTF-8">
<title>index.html< title>
< head>
<body>
<h1>首页< h1>
< body>
< html>
#node2
[root@node2 ~]# curl localhost
<html>
<head>
<meta charset="UTF-8">
<title>index.html< title>
< head>
<body>
<h1>首页< h1>
< body>
< html>
8.流程解释
阶段一:Ansible部署时
按照playbook的执行顺序:
- 先安装软件(
install_httpd.yml)- httpd程序放到系统里 - 再复制配置文件(
config_httpd.yml)- 配置放到/etc/httpd/conf/ - 再生成网页文件(
index_httpd.yml)- index.html放到/var/www/html/ - 最后启动服务(
service_httpd.yml)- systemd启动httpd
阶段二:服务运行时
当你在部署完成后执行 curl 127.0.0.1 时,发生的真实过程是:
-
服务已经在监听(80端口)
#为啥是80?因为httpd.conf里面的内容 Listen 80 -
请求到达时读取文件
-
客户端(curl)发起HTTP请求到127.0.0.1
-
操作系统将请求交给监听80端口的httpd进程
[root@node1 ~]# ss -lntup | grep 80 tcp LISTEN 0 511 *:80 *:* users:(("httpd",pid=13060,fd=4),("httpd",pid=13058,fd=4),("httpd",pid=13057,fd=4),("httpd",pid=9135,fd=4)) -
httpd进程根据请求路径(/)查找对应的文件
-
根据配置文件(httpd.conf)中的DocumentRoot设置,找到/var/www/html/index.html
#httpd.conf里面的内容 DocumentRoot "/var/www/html" -
此时才真正读取这个文件的内容
-
将文件内容作为HTTP响应返回给客户端
-
更多推荐

所有评论(0)