使用playbook部署httpd

使用playbook部署httpd

1. 环境

版本ip备注域名
CentOS Stream release 8192.168.232.129ansible主控129a
CentOS Stream release 8192.168.232.130部署httpd130h

2. 准备工作

2.1 安装yum源和epel源
2.2 安装ansible
2.3 修改ansible主控机的配置文件,使其能通信
将下载的网站移动到httpd目录下
[root@129a ~]# mv web/ /opt/httpd/


修改ansible主机默认清单的位置
[root@129a httpd]# ls
ansible.cfg  inventory  wz
[root@129a httpd]# vim inventory 
[root@129a httpd]# cat inventory 
[http]
130h ansible_user=root ansible_password=run123456
[root@129a httpd]# vim /etc/hosts
[root@129a httpd]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.232.130 130h
[root@129a httpd]# 

测试ansible主机和受管主机之间是否互通
[root@129a httpd]# ansible all -m ping
130h | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
[root@129a httpd]# 
2.4 编写安装httpd的playbook
[root@129a httpd]# cat httpd_install.yml 
---
- name: install httpd
  hosts: all
  tasks:
    - name: install httpd useing dnf       
      dnf:
        name: httpd
        state: present
    - name: service is enabled
      service:
        name: httpd
        state: started
        enabled: yes
    - name: firewalld
      service:
        name: firewalld
        state: stopped
        enabled: no
    - name: restart
      service:
        name: httpd
        state: restarted

- name: Copy web files
  hosts: all
  tasks:
    - name: Copy web files      
      copy:
        src: /opt/httpd/web/
        dest: /var/www/html/index.html
        owner: root
        group: root
        mode: 0644
[root@129a httpd]#
2.5 执行空运行
[root@129a httpd]# ansible-playbook -C httpd_install.yml

PLAY [install httpd] ***************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************
ok: [130h]

TASK [install httpd useing dnf] ****************************************************************************************************
ok: [130h]

TASK [service is enabled] **********************************************************************************************************
ok: [130h]

PLAY [Copy web files] **************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************
ok: [130h]

TASK [Copy web files] **************************************************************************************************************
changed: [130h]

PLAY RECAP *************************************************************************************************************************
130h                       : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@129a httpd]#
2.6 运行playbook
[root@129a httpd]# ansible-playbook httpd_install.yml

PLAY [install httpd] ***************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************
ok: [130h]

TASK [install httpd useing dnf] ****************************************************************************************************
ok: [130h]
[root@129a httpd]# ansible-playbook httpd_install.yml

PLAY [install httpd] ***************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************
ok: [130h]

TASK [install httpd useing dnf] ****************************************************************************************************
[root@129a httpd]# ansible-playbook httpd_install.yml

PLAY [install httpd] ***************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************
ok: [130h]

TASK [install httpd useing dnf] ****************************************************************************************************
changed: [130h]

TASK [service is enabled] **********************************************************************************************************
changed: [130h]

TASK [firewalld] *******************************************************************************************************************
ok: [130h]

TASK [restart] *********************************************************************************************************************
changed: [130h]

PLAY [Copy web files] **************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************
ok: [130h]

TASK [Copy web files] **************************************************************************************************************
ok: [130h]

PLAY RECAP *************************************************************************************************************************
130h                       : ok=7    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@129a httpd]# 
2.7 访问
[root@130h ~]# ss -antl
State          Recv-Q         Send-Q                 Local Address:Port                   Peer Address:Port         Process         
LISTEN         0              128                          0.0.0.0:22                          0.0.0.0:*                            
LISTEN         0              128                             [::]:22                             [::]:*                            
LISTEN         0              128                                *:80                                *:*                            
[root@130h ~]# 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值