连接
[root@control httpd]# ssh web01.example.com
root@web01.example.com's password:
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Fri May 28 18:43:57 2022
[root@localhost ~]# exit
logout
Connection to web01.example.com closed.
设置免密登录
[root@control httpd]# ssh-copy-id web01.example.com
/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@web01.example.com's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'web01.example.com'"
and check to make sure that only the key(s) you wanted were added.
//现在可以ping通了
[root@control httpd]# ansible all -m ping
web01.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
playbook
[root@control httpd]# vim install.yml
---
- hosts: webservers
tasks:
- name: provides repo file
copy:
src: files/CentOS-Base.repo
dest: /etc/yum.repos.d/
- name: install apache
yum:
name: httpd
state: latest
- name: porvides web site
copy:
src: files/mm
dest: /var/www/html
- name: config apache
copy:
src: files/httpd-vhosts.conf
dest: /etc/httpd/conf.d/
- name: run httpd
service:
name: httpd
state: started
enabled: yes
- name: close firewalld
service:
name: firewalld
state: stopped
enabled: no
[root@control httpd]# ansible-playbook -C install.yml //测试能不能跑
[root@control httpd]# ansible-playbook install.yml //运行
详细步骤
//先装apache
[root@control httpd]# vim install.yml
---
- hosts: webservers
tasks:
- name: provides repo file
copy:
src: files/CentOS-Base.repo
dest: /etc/yum.repos.d/
- name: install apache
yum:
name: httpd
state: latest
[root@control httpd]# ansible-playbook install.yml
//受控机
[root@localhost ~]# rpm -qa | grep httpd
centos-logos-httpd-85.8-2.el8.noarch
httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch
[root@localhost ~]#
//在受控机上找到配置文件
[root@localhost ~]# cd /etc/httpd
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run state
[root@localhost httpd]# cd conf
[root@localhost conf]# ls
httpd.conf magic
[root@localhost conf]# cd ../conf.d/
[root@localhost conf.d]# ls
autoindex.conf README userdir.conf welcome.conf
[root@localhost conf.d]# find / -name *vhosts.conf
/usr/share/doc/httpd/httpd-vhosts.conf
[root@localhost conf.d]#
//修改配置文件
[root@control httpd]# cd files
[root@control files]# scp web01.example.com:/usr/share/doc/httpd/httpd-vhosts.conf .
httpd-vhosts.conf 100% 1477 1.3MB/s 00:00
[root@control files]# ls
CentOS-Base.repo httpd-vhosts.conf
[root@control files]# vim httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/ting"
ServerName ting.example.com
ErrorLog "/var/log/httpd/ting.example.com-error_log"
CustomLog "/var/log/httpd/ting.example.com-access_log" common
</VirtualHost>
//把配置文件弄到受控机上
[root@localhost conf.d]# ls //此时没有
autoindex.conf README userdir.conf welcome.conf
//playbook
[root@control files]# cd ..
[root@control httpd]# vim install.yml
---
- hosts: webservers
tasks:
- name: provides repo file
copy:
src: files/CentOS-Base.repo
dest: /etc/yum.repos.d/
- name: install apache
yum:
name: httpd
state: latest
- name: config apache
copy:
src: files/httpd-vhosts.conf
dest: /etc/httpd/conf.d/
[root@control httpd]# ansible-playbook install.yml
PLAY [webservers] ******************************************************************
TASK [Gathering Facts] *************************************************************
ok: [web01.example.com]
TASK [provides repo file] **********************************************************
ok: [web01.example.com]
TASK [install apache] **************************************************************
ok: [web01.example.com]
TASK [config apache] ***************************************************************
changed: [web01.example.com]
PLAY RECAP *************************************************************************
web01.example.com : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
//现在受控机可以查看到了/
[root@localhost conf.d]# ls
autoindex.conf httpd-vhosts.conf README userdir.conf welcome.conf
[root@localhost conf.d]# cat httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/ting"
ServerName ting.example.com
ErrorLog "/var/log/httpd/ting.example.com-error_log"
CustomLog "/var/log/httpd/ting.example.com-access_log" common
</VirtualHost>
//下载一个网站
[root@control files]# yum -y install unzip
[root@control files]# unzip ffcc.zip
[root@control files]# mv ffcc xi
[root@control files]# rm -rf ffcc.zip
//playbook
[root@control httpd]# vim install.yml
---
- hosts: webservers
tasks:
- name: provides repo file
copy:
src: files/CentOS-Base.repo
dest: /etc/yum.repos.d/
- name: install apache
yum:
name: httpd
state: latest
- name: porvides web site
copy:
src: files/ting
dest: /var/www/html
- name: config apache
copy:
src: files/httpd-vhosts.conf
dest: /etc/httpd/conf.d/
- name: run httpd
service:
name: httpd
state: started
enabled: yes
- name: close firewalld
service:
name: firewalld
state: stopped
enabled: no
[root@control httpd]# ansible-playbook install.yml
本文介绍如何使用Ansible实现Apache Web服务器的自动化部署过程,包括免密SSH连接配置、安装Apache及相关组件、配置虚拟主机、启动服务及关闭防火墙等步骤。
311

被折叠的 条评论
为什么被折叠?



