Playbook 变量管理 之 安装 Apache
前言案例步骤一、案例要求二、准备工作三、编辑 Playbook四、检查 Playbook 语法五、执行Playbook六、测试http服务
前言
通过在Playbook 中定义变量,使用变量去创建和完成任务,从而实现 http 服务的部署,达到 web 服务的正常运行,并对外提供服务
提示:本篇文章所使用的环境为centos-8.2基于ansible-2.8.0 搭建 具体环境搭建,请参考:ansible-2.8.0 搭建链接
案例步骤
一、案例要求
要求通过Playbook 定义变量的方式,安装Apache 软件 并添加防火墙规则,确保http 服务开机自启,正常对外提供服务的需求
二、准备工作
编辑Playbook 需要定义的变量
变量描述
web_pkg需要安装的 web 服务器的软件包firewall_pkg需要安装的防火墙软件包web_service需要管理的 web 服务firewall_service需要管理的防火墙服务python_pkguri 模块所需的软件包rule需要打开的服务
三、编辑 Playbook
定义变量,安装Aoache 服务,并启动http 服务、开机自启,添加防火墙
[root@ansible-server ansible
]
---
- name: deploy and start Apache HTTPD
service
hosts: webservers
vars:
web_pkg: httpd
firewalld_pkg: firewalld
web_service: httpd
firewalld_service: firewalld
python_pkg: python3-PyMySQL
rule: http
tasks:
- name: 1.install httpd firewalld python3-PyMySQL and confirm is latest
yum:
name:
-
'{{ web_pkg }}'
-
'{{ firewalld_pkg }}'
-
'{{ python_pkg }}'
state: latest
- name: 2.the
{{ firewalld_service
}} service is started and enabled
service:
name:
'{{ firewalld_service }}'
enabled:
true
state: started
- name: 3.the
{{ web_service
}} service is started and enabled
service:
name:
'{{ web_service }}'
enabled:
true
state: started
- name: web_file
copy:
content:
'This is Apache web_servers...'
dest: /var/www/html/index.html
- name: 4.the firewalld port
for {{ rule
}} is
open
firewalld:
service:
'{{ rule }}'
permanent:
true
immediate:
true
state: enabled
[root@ansible-server ansible
]
---
- hosts: localhost
name:
make sure Apache web_server is
enable
become:
false
tasks:
- name:
test web_server is useable
uri:
url: http://node02
status_code: 200
- name:
test web_server is useable
uri:
url: http://node02
status_code: 200
四、检查 Playbook 语法
[root@ansible-server ansible
]
playbook: Apache_service.yml
[root@ansible-server ansible
]
playbook: Apache_service_test.yml
五、执行Playbook
[root@ansible-server ansible
]
PLAY
[deploy and start Apache HTTPD service
] ***********************************************************************
TASK
[Gathering Facts
] *********************************************************************************************
ok:
[node02
]
ok:
[node03
]
TASK
[1.install httpd firewalld python3-PyMySQL and confirm is latest
] *********************************************
ok:
[node02
]
ok:
[node03
]
TASK
[2.the firewalld
service is started and enabled
] **************************************************************
ok:
[node03
]
ok:
[node02
]
TASK
[3.the httpd
service is started and enabled
] ******************************************************************
ok:
[node03
]
ok:
[node02
]
TASK
[web_file
] ****************************************************************************************************
ok:
[node03
]
ok:
[node02
]
TASK
[4.the firewalld port
for http is open
] ***********************************************************************
ok:
[node03
]
ok:
[node02
]
PLAY RECAP *********************************************************************************************************
node02
: ok
=6 changed
=0 unreachable
=0 failed
=0 skipped
=0 rescued
=0 ignored
=0
node03
: ok
=6 changed
=0 unreachable
=0 failed
=0 skipped
=0 rescued
=0 ignored
=0
[root@ansible-server ansible
]
PLAY
[make sure Apache web_server is enable
] ***********************************************************************
TASK
[Gathering Facts
] *********************************************************************************************
ok:
[localhost
]
TASK
[test web_server is useable
] **********************************************************************************
ok:
[localhost
]
TASK
[test web_server is useable
] **********************************************************************************
ok:
[localhost
]
PLAY RECAP *********************************************************************************************************
localhost
: ok
=3 changed
=0 unreachable
=0 failed
=0 skipped
=0 rescued
=0 ignored
=0
六、测试http服务
[root@ansible-server ansible
]
[WARNING
]: Consider using the get_url or uri module rather than running
'curl'. If you need to use
command because
get_url or uri is insufficient you can add
'warn: false' to this
command task or
set 'command_warnings=False' in
ansible.cfg to get rid of this message.
node03
| CHANGED
| rc
=0
>>
This is Apache web_servers
...This is Apache web_servers
... % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29 100 29 0 0 5800 0 --:--:-- --:--:-- --:--:-- 5800
100 29 100 29 0 0 5800 0 --:--:-- --:--:-- --:--:-- 5800
node02
| CHANGED
| rc
=0
>>
This is Apache web_servers
...This is Apache web_servers
... % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29 100 29 0 0 4833 0 --:--:-- --:--:-- --:--:-- 5800
100 29 100 29 0 0 4833 0 --:--:-- --:--:-- --:--:-- 4833