练习
- 所有web主机安装Apache
- 修改配置文件的监听端口为8080
- 设置默认主页 hello world
- 启动服务
- 设置开机自启
服务器规划
MANAGER(管理机) : IP 192.168.1.10
WEB1: IP 192.168.1.21
WEB2: IP 192.168.1.22
安装前提: (需要进入ansible文件中运行"/etc/ansible")
- ansible的基本配置已经完成,如下图:
[root@manager ansible]# ansible web -m ping
web1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web2 | SUCCESS => {
"changed": false,
"ping": "pong"
2.所有机器的YUM源配置完成.
[root@web1 ~]# yum repolist
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
源标识 源名称 状态
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,097
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 323
local_repo CentOS-7 - Base 9,911
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 1,446
repolist: 21,777
开始写YAML文件
[root@ansible ansible]# vim http.yml
---
- hosts: web
remote_user: root
tasks:
- name: install one specific version of Apache
yum:
name: httpd //安装Apache
state: installed
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen '
line: 'Listen 8080' //修改端口为8080
- service:
name: httpd
enabled: yes //开机自启
state: restarted
- copy:
src: /root/index.html //修改主页,可以自己写个页面
dest: /var/www/html/index.html
运行时,需要将以"//"注释信息删除,以免报错!
[root@manager ansible]# echo 'hello world' > /root/index.html
[root@manager ansible]# ansible-playbook http.yml
PLAY [web] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [web1]
ok: [web2]
TASK [install one specific version of Apache] **********************************
changed: [web2]
changed: [web1]
TASK [lineinfile] **************************************************************
changed: [web1]
changed: [web2]
TASK [service] *****************************************************************
changed: [web1]
changed: [web2]
TASK [copy] ********************************************************************
changed: [web1]
changed: [web2]
PLAY RECAP *********************************************************************
web1 : ok=5 changed=4 unreachable=0 failed=0
web2 : ok=5 changed=4 unreachable=0 failed=0
检查是否安装启动成功
[root@manager ansible]# curl 192.168.1.21:8080
hello world