ansible的部署httpd与虚拟主机

本文档详细介绍了如何使用Ansible自动化部署HTTPD服务,并配置虚拟主机。包括关闭firewalld和SELINUX,控制节点主机清单的管理,连接测试,复制和配置yum源,安装与配置httpd服务,添加站点以及启动服务的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关闭firewalld和SELINUX
[root@ansible ~]# systemctl stop firewalld
[root@ansible ~]# systemctl disable firewalld

在控制节点的主机清单中添加受控主机
[root@ansible ~]# vim /etc/ansible/ansible.cfg 
#inventory      = /etc/ansible/hosts               //去掉注释并修改路径,这里我修改的路径为/etc/ansible/inventory,修改完毕后要手动创建清单文件

[root@ansible ~]# vim /etc/ansible/ansible.cfg 
192.168.50.140 ansible_password=redhat                        //在清单中添加受管主机的IP地址以及远程登陆的密码,可以手动修改远登录的用户,如果不修改默认使用当前ansible主机的用户登录
控制节点的主机清单中添加受控主机
[root@ansible ~]# vi /etc/ansible/inventory 
192.168.194.132 ansible_password=redhat
192.168.194.133 ansible_password=redhat
192.168.194.134 ansible_password=redhat

[root@localhost ~]# echo 'redhat' |passwd --stdin root
更改ansible配置文件
[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# vim ansible.cfg
// 在ansible.cfg里添加一行
inventory      = /etc/ansible/inventory

测试连通性
[root@ansible ~]# ansible 192.168.194.139 -m ping
192.168.194.139 | SUCCESS => {
   "ansible_facts": {
       "discovered_interpreter_python": "/usr/bin/python"
   },
   "changed": false,
   "ping": "pong"
}

创建项目目录,方便管理

[root@localhost ~]# tree httpd
httpd
├── files
│   └── httpd-vhosts.conf
├── group-vars
├── host-vars
├── playbook.yml
└── vars
   ├── httpd_port1
   └── httpd_port2
5 directories, 3 files
复制并配置yum源
---
- hosts: all
  name: download CentOS 7 yum
  tasks:
    - name: copy
      copy:
        src:  /tmp/yum/yum.repos.d/
        dest: /etc/yum.repos.d/



- name: 修改yum源的配置文件
  hosts: all
  tasks:
    - name: 修改
      command: sed -i 's/8/7/g' /etc/yum.repos.d/CentOS-Base.repo

安装httpd服务
[root@ansible ~]# vim gg.yml 

--- 
   -name: install httpd
     hosts: 192.168.194.139
     tasks:
         -name: install http
         yum:
            name: httpd
            state: present
[root@localhost ~]# ansible-playbook  gg.yml 

PLAY [192.168.194.139] *********************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.194.139]

TASK [install httpd] ***********************************************************
changed: [192.168.194.139]

PLAY RECAP *********************************************************************
192.168.194.139            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
[root@ansible ~]# 

修改httpd的配置文件
---
    hosts: 192.168.194.132
    tasks:
         -name: change configuration file
          command: sed -i 's/#ServerName www.example.com:80/ServerName www.example.com:80/g' /etc/httpd/conf/httpd.conf
添加站点
[root@localhost ~]# vim  httpd.yml 
[root@localhost ~]# cat httpd.yml 
---
- name: change configuration file
  hosts: 192.168.194.139
  tasks:
    - name: change configuration file
      command: sed -i 's/#ServerName www.example.com:80/ServerName www.example.com:80/g' /etc/httpd/conf/httpd.conf

- name: add VirtualHost
  hosts: 192.168.194.139
  tasks: 
    - name: add VirtualHost 
      shell: cd /var/www/html/ && mkdir test xxx && cd test && echo "hello tom" > index.html && cd ../xxx && echo "helo cwt" > index.html

这一步在localhost上手动编写虚拟主机配置文件

[root@localhost ~]# vim httpd-vhosts.j2 

<VirtualHost *:{{ httpd_port1 }}>
DocumentRoot "/var/www/html/{{ web1 }}"
ServerName {{ web1 }}.example.com
ErrorLog "/var/log/httpd/{{ web1 }}.example.com-error_log"
CustomLog “/var/log/httpd/{{ web1 }}.example.com-access_log" common
</VirtualHost>
Listen {{ httpd_port2 }}
<VirtualHost *:{{ httpd_port2 }}>
DocumentRoot "/var/www/html/{{ web2 }}"
ServerName {{ web2 }}.example.com
ErrorLog "/var/log/httpd/{{ web2 }}.example.com-error_log"
CustomLog "/var/log/httpd/{{ web2 }}.example.com-access_log" common
</VirtualHost>

启动服务
- name: start httpd
  hosts: 192.168.194.139
  tasks:
    - name: start httpd
      service:
        name: httpd
        state: started
        enabled: yes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值