栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

playbook部署httpd

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

playbook部署httpd

playbook、yum部署httpd
  • 1. 准备环境
  • 2. 部署Ansible
  • 3. 设置免密登录
  • 4. 配置Ansible
    • 4.1 在真机上做域名映射
  • 5. 测试环境是否已弄好

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:  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 

    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


#往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:WindowsSystem32driversetchosts

5. 测试环境是否已弄好

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/964864.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号