ansible-playbook批量安装httpd,按主机名提供不同的index.html

本文详细介绍了如何使用Ansible在Centos 7.9环境中创建httpd角色,包括组、用户、包安装、配置文件和启动服务的步骤,并通过Playbook进行自动化部署。最后验证了httpd服务的正常运行和网页内容的加载。
摘要由CSDN通过智能技术生成

实验环境

Ansible Server        Centos7.9

192.168.118.189

Http Server        Centos7.9

192.168.118.25

192.168.118.43

1.创建角色相关的目录

 mkdir -pv /data/ansible/roles/httpd/{tasks,handlers,files}

2.创建角色相关文件

cd /data/ansible/roles/httpd/
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/main.yml
- include: group.yml
- include: user.yml
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml

[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/group.yml
- name: create apache group
  group: name=apache system=yes gid=80
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/user.yml
- name: create apache user
  user: name=apache system=yes shell=/sbin/nologin home=/var/www uid=80 group=apache
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/install.yml
- name: install httpd package
  yum: name=httpd
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/config.yml
- name: config file
  copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
  notify: restart
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/index.yml
- name: index.html
  copy: src=index_master.html dest=/var/www/html/index.html
  when: ansible_hostname == "master"
- name: index.html
  copy: src=index_slave.html dest=/var/www/html/index.html
  when: ansible_hostname == "slave"
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/service.yml
- name: start service
  service: name=httpd state=started enabled=yes

PlayBook结构

[root@mycat files]# tree /data/ansible/roles/httpd/
/data/ansible/roles/httpd/
├── files
│   ├── httpd.conf
│   ├── index.html
│   ├── index_master.html
│   └── index_slave.html
├── handlers
│   └── main.yml
└── tasks
    ├── config.yml
    ├── group.yml
    ├── index.yml
    ├── install.yml
    ├── main.yml
    ├── service.yml
    └── user.yml

3.执行 ansible-playbook -C role_httpd.yml测试配置是否正确

[root@mycat ansible]# ansible-playbook -C role_httpd.yml

PLAY [websrvs] ****************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [192.168.118.43]
ok: [192.168.118.25]

TASK [httpd : create apache group] ********************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]

TASK [httpd : create apache user] *********************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]

TASK [install httpd package] **************************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]

TASK [httpd : config file] ****************************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]

TASK [httpd : index.html] *****************************************************************************************************************************************
skipping: [192.168.118.43]
changed: [192.168.118.25]

TASK [httpd : index.html] *****************************************************************************************************************************************
skipping: [192.168.118.25]
changed: [192.168.118.43]

TASK [httpd : start service] **************************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]

RUNNING HANDLER [httpd : restart] *********************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]

PLAY RECAP ********************************************************************************************************************************************************
192.168.118.25             : ok=8    changed=7    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
192.168.118.43             : ok=8    changed=7    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

4.执行ansible playbook安装配置httpd

[root@mycat ansible]# ansible-playbook  role_httpd.yml

PLAY [websrvs] ****************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [192.168.118.25]
ok: [192.168.118.43]

TASK [httpd : create apache group] ********************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]

TASK [httpd : create apache user] *********************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]

TASK [install httpd package] **************************************************************************************************************************************
changed: [192.168.118.25]
changed: [192.168.118.43]

TASK [httpd : config file] ****************************************************************************************************************************************
ok: [192.168.118.25]
ok: [192.168.118.43]

TASK [httpd : index.html] *****************************************************************************************************************************************
skipping: [192.168.118.43]
changed: [192.168.118.25]

TASK [httpd : index.html] *****************************************************************************************************************************************
skipping: [192.168.118.25]
changed: [192.168.118.43]

TASK [httpd : start service] **************************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]

PLAY RECAP ********************************************************************************************************************************************************
192.168.118.25             : ok=7    changed=5    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
192.168.118.43             : ok=7    changed=5    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

5.查看httpd服务是否正常启动

[root@master ~]# ss -tnl
State       Recv-Q Send-Q                                    Local Address:Port                                                   Peer Address:Port
LISTEN      0      128                                                   *:22                                                                *:*
LISTEN      0      100                                           127.0.0.1:25                                                                *:*
LISTEN      0      128                                                [::]:80                                                             [::]:*
LISTEN      0      128                                                [::]:22                                                             [::]:*
LISTEN      0      100                                               [::1]:25                                                             [::]:*

6.登录web页面查看是否符合预期

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值