playbook部署httpd

1. 准备环境

虚拟机:redhat8

主机:192.168.229.143

受控主机:192.168.229.130

注意:两台虚拟机已恢复快照

2. 部署Ansible

#配置阿里yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
#清除缓存
[root@localhost yum.repos.d]# dnf clean all
#建立缓存
[root@localhost yum.repos.d]# dnf makecache

#列出ansible
[root@localhost ~]# dnf list all|grep ansible
....省略N
ansible-pcp.noarch                 2.2.1-1.el8  AppStream 
centos-release-ansible-29.noarch   1-2.el8      extras #需要这个安装Ansible源
....省略N

#安装ansible源
[root@localhost ~]# dnf -y install centos-release-ansible-29
#清除缓存
[root@localhost ~]# dnf clean all
#建立缓存
[root@localhost ~]# dnf makecache

# 安装ansible
[root@localhost ~]# dnf -y install ansible

#查看版本号
[root@localhost ~]# ansible --version 
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Sep  9 2021, 07:49:02) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
[root@localhost ~]# 

3. 设置免密登录

#配置域名
[root@localhost ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.229.130 web.example.com

#生成密钥
[root@localhost ~]# 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:f0z4fZYxApm0w+pJgYfND2TKxTO18KEgRd7qxoErfQg root@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|     .o+.=.+     |
|      + %+* *    |
|       * OoX     |
|      . o =.o    |
|   E . oSo.... o |
|    o = +..+ .. +|
|   . + = o. + ..o|
|    . o    .   o |
|                 |
+----[SHA256]-----+

#使用 ssh-copy-id 将公钥复制到远程系统上的正确位置
[root@localhost ~]# ssh-copy-id root@web.example.com
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'web.example.com (192.168.229.130)' can't be established.
ECDSA key fingerprint is SHA256:mntQBTppC7e+5Uh8MyZHFW3FuKZzpoS46G0j2C+O8U4.
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@web.example.com's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@web.example.com'"
and check to make sure that only the key(s) you wanted were added.

#登录到受管主机并修改主机名
[root@localhost ~]# ssh web.example.com
[root@localhost ~]# ip addr show ens160
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:f6:e7:cf brd ff:ff:ff:ff:ff:ff
    inet 192.168.229.130/24 brd 192.168.229.255 scope global dynamic noprefixroute ens160
       valid_lft 1190sec preferred_lft 1190sec
    inet6 fe80::20c:29ff:fef6:e7cf/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@localhost ~]# hostnamectl set-hostname web.example.com
[root@localhost ~]# bash
[root@web ~]# 

4. 配置Ansible

#设置主机名
[root@localhost ~]# hostnamectl set-hostname ansible.example.com
[root@localhost ~]# bash
[root@ansible ~]# 

#将ansible下面的ansible.cfg复制一份到opt的httpd目录内
[root@ansible ~]# mkdir /opt/httpd
[root@ansible ~]# cp /etc/ansible/ansible.cfg /opt/httpd/
[root@ansible ~]# cd /opt/httpd/
[root@ansible httpd]# ls
ansible.cfg

#创建清单文件
[root@ansible httpd]# vi inventory 
[webservers]
web.example.com

#修改ansible.cfg文件
[root@ansible httpd]# vim ansible.cfg 
inventory      = inventory

[root@localhost httpd]# ansible --version
ansible 2.9.27
  config file = /opt/httpd/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Sep  9 2021, 07:49:02) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]

#创建目录files
[root@localhost httpd]# mkdir files
#将yum仓库文件复制到/opt/httpd/files/内
[root@ansible ~]# cp /etc/yum.repos.d/CentOS-Base.repo /opt/httpd/files/
[root@ansible ~]# ls /opt/httpd/files/
CentOS-Base.repo

#下载网页源码包 解压改名为web
[root@ansible ~]# ls /opt/httpd/files/
7-404.zip  CentOS-Base.repo
#下载解压工具
[root@ansible ~]# dnf -y install unzip
[root@localhost files]# unzip 7-404.zip 
[root@localhost files]# mv 7-404 web
[root@localhost files]# ls
CentOS-Base.repo  web

#使用临时命令删除受控主机yum源
[root@control httpd]# ansible all -m shell -a 'rm -rf /etc/yum.repos.d/*'
web.example.com | CHANGED | rc=0 >>


#创建剧本
[root@ansible httpd]# vim apache.yml
---
- hosts: webservers
  tasks:
    - name: provides repo file
      copy:
        src: files/CentOS-Base.repo
        dest: /etc/yum.repos.d/

    - name: install apache
      dnf:
       name: httpd
       state: latest

#运行、也可以加上-C测试
[root@ansible httpd]# ansible-playbook install.yml 

#将受管主机的httpd-vhosts.conf文件传到主机的files目录内 并修改配置
[root@localhost files]# scp web.example.com:/usr/share/doc/httpd/httpd-vhosts.conf .
httpd-vhosts.conf                         100% 1477   851.9KB/s   00:00    
[root@localhost files]# ls
7-404.zip  CentOS-Base.repo  httpd-vhosts.conf  web

[root@localhost files]# vim httpd-vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/var/www/html/web"
    ServerName web.example.com
    ErrorLog "/var/log/httpd/web.example.com-error_log"
    CustomLog "/var/log/httpd/web.example.com-access_log" common
</VirtualHost>

#往apache.yml添加以下内容
[root@ansible httpd]# cat apache.yml 
---
- hosts: webservers
  vars_files:
    - vars/httpd
  tasks:
    - name: provides repo file
      copy:
        src: files/CentOS-Base.repo
        dest: /etc/yum.repos.d/

    - name: install apache #下载httpd
      dnf:
        name: "{{ web }}"
        state: latest

    - name: provides web site #提供网站
      copy:
        src: files/web
        dest: /var/www/html/

    - name: config apache #
      copy:
        src: files/httpd-vhosts.conf
        dest: /etc/httpd/conf.d/

    - name: run {{ web }} #启动httpd 、并开机自启
      service:
       name: "{{ web }}"
       state: started
       enabled: yes

    - name: close firewalld #关闭防火墙、并开机不自启
      service:
        name: firewalld
        state: stopped
        enabled: no

#创建vars变量目录
[root@ansible httpd]# mkdir vars
[root@ansible httpd]# ls
ansible.cfg  files  inventory  vars
[root@ansible httpd]# vim vars/httpd 
web: httpd

#运行、可以加上-C测试
[root@ansible httpd]# ansible-playbook apache.yml

4.1 在真机上做域名映射

映射地址:C:\Windows\System32\drivers\etc\hosts
在这里插入图片描述

5. 测试环境是否已弄好

在这里插入图片描述

要使用Ansible部署MHA(MySQL高可用性解决方案),可以按照以下步骤进行操作: 1. 首先,创建一个playbook文件,比如playbook_mha.yml。在该playbook中,你需要指定你的目标主机和任务。 2. 在playbook_mha.yml中,使用ansible的shell模块来执行必要的命令。可以参考引用中的示例,使用ansible-playbook命令来执行playbook文件。 3. 在MHA节点上停止httpd服务,可以使用ansible的service模块。可以参考引用中的示例,使用ansible命令来停止httpd服务。 4. 在web服务器上安装httpd软件包,可以使用ansible的yum模块。可以参考引用中的示例,使用ansible命令来安装httpd软件包。 5. 在playbook_mha.yml中添加其他必要的任务,例如设置MHA配置文件、启动MHA等。 通过以上步骤,你可以使用ansible-playbook部署MHA。请根据你的具体需求进行相应的修改和配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ansible-playbook](https://blog.csdn.net/m0_61664359/article/details/120552327)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [第二十周 ansible playbook实现httpd批量部署和MySQL高可用方案MHA](https://blog.csdn.net/hfhshwj/article/details/115911358)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值