使用playbook部署httpd
1. 环境
版本 | ip | 备注 | 域名 |
---|
CentOS Stream release 8 | 192.168.232.129 | ansible主控 | 129a |
CentOS Stream release 8 | 192.168.232.130 | 部署httpd | 130h |
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 ~]#