菏泽哪里有做网站的广州疫情今天最新消息
实验目的如下:
1. 环境准备: 使用命令lab inventory-variables start初始化环境
2. 进入/home/student/git-repos目录克隆下载http://git.lab.example.com:8081/git/inventory-variables.git
3. 将目录下yaml文件内容以group_vars形式修改
4. 部署并将修改后ansible-playbook代码上传git仓库
5. 环境结束使用lab inventory-variables finish清理环境
试验开始
cd /home/student/git-repos
git clone http://git.lab.example.com:8081/git/inventory-variables.git
cd inventory-variables/=====================
确认2个组名
grep hosts deploy_haproxy.yml deploy_webapp.yml
====得到以下内容:
deploy_haproxy.yml: hosts: lb_servers
deploy_webapp.yml: hosts: web_servers
创建目录
mkdir group_vars/{lb_servers,web_servers} -p
此时deploy_haproxy.yml 内容如下.
[student@workstation inventory-variables (master)]$cat deploy_haproxy.yml
- name: Ensure HAProxy is deployedhosts: lb_serversforce_handlers: Trueroles:# The "haproxy" role has a dependency on the "firewall" role.# The "firewall" role requires a "firewall_rules" variable be defined.- role: haproxyfirewall_rules:# Allow 80/tcp connections- port: 80/tcphaproxy_appservers:- name: serverb.lab.example.comip: 172.25.250.11backend_port: 80- name: serverc.lab.example.comip: 172.25.250.12backend_port: 80
将以下内容放入group_vars/lb_servers/firewall.yml中
firewall_rules:# Allow 80/tcp connections- port: 80/tcp
将以下内容放入group_vars/lb_servers/haproxy.yml中(其实这里路径对即可,文件名可以不严格安装这个,只是好识别)
haproxy_appservers:- name: serverb.lab.example.comip: 172.25.250.11backend_port: 80- name: serverc.lab.example.comip: 172.25.250.12backend_port: 80
然后删除deploy_haproxy.yml中的这部分内容,此时deploy_haproxy.yml如下:
- name: Ensure HAProxy is deployedhosts: lb_serversforce_handlers: Trueroles:# The "haproxy" role has a dependency on the "firewall" role.# The "firewall" role requires a "firewall_rules" variable be defined.- role: haproxy
deploy_apache内容如下:
- name: Ensure Apache is deployedhosts: web_serversforce_handlers: Trueroles:# The "apache" role has a dependency on the "firewall" role.# The "firewall" role requires a "firewall_rules" variable be defined.- role: apachefirewall_rules:# Allow http requests from the load_balancer.- zone: internalservice: httpsource: "172.25.250.10"
将以下内容放入group_vars/web_servers/firewall.yml中
firewall_rules:# Allow http requests from the load_balancer.- zone: internalservice: httpsource: "172.25.250.10"
再将源文件这部分内容删除,此时deploy_apache.yml内容如下
- name: Ensure Apache is deployedhosts: web_serversforce_handlers: Trueroles:# The "apache" role has a dependency on the "firewall" role.# The "firewall" role requires a "firewall_rules" variable be defined.- role: apache
执行命令ansible-playbook site.yml运行部署
[student@workstation inventory-variables (master *%)]$ansible-playbook site.ymlPLAY [Ensure HAProxy is deployed] ******************************************************TASK [Gathering Facts] *****************************************************************
ok: [servera.lab.example.com]TASK [firewall : Ensure Firewall Sources Configuration] ********************************
ok: [servera.lab.example.com] => (item={'port': '80/tcp'})TASK [haproxy : Ensure haproxy packages are present] ***********************************
changed: [servera.lab.example.com]TASK [haproxy : Ensure haproxy is started and enabled] *********************************
changed: [servera.lab.example.com]TASK [haproxy : Ensure haproxy configuration is set] ***********************************
changed: [servera.lab.example.com]RUNNING HANDLER [haproxy : reload haproxy] *********************************************
changed: [servera.lab.example.com]PLAY [Ensure Apache is deployed] *******************************************************TASK [Gathering Facts] *****************************************************************
ok: [serverc.lab.example.com]
ok: [serverb.lab.example.com]TASK [firewall : Ensure Firewall Sources Configuration] ********************************
ok: [serverb.lab.example.com] => (item={'zone': 'internal', 'service': 'http', 'source': '172.25.250.10'})
ok: [serverc.lab.example.com] => (item={'zone': 'internal', 'service': 'http', 'source': '172.25.250.10'})TASK [apache : Install http] ***********************************************************
changed: [serverb.lab.example.com]
changed: [serverc.lab.example.com]TASK [apache : Configure SELinux to allow httpd to connect to remote database] *********
ok: [serverb.lab.example.com]
ok: [serverc.lab.example.com]TASK [apache : http service state] *****************************************************
changed: [serverb.lab.example.com]
changed: [serverc.lab.example.com]PLAY [Ensure Web App is deployed] ******************************************************TASK [Gathering Facts] *****************************************************************
ok: [serverb.lab.example.com]
ok: [serverc.lab.example.com]TASK [webapp : Copy a stub file.] ******************************************************
ok: [serverb.lab.example.com]
ok: [serverc.lab.example.com]PLAY RECAP *****************************************************************************
servera.lab.example.com : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverb.lab.example.com : ok=7 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverc.lab.example.com : ok=7 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
访问测试,可以看到实现通过servera对serverb,serverc的轮询
[student@workstation inventory-variables (master *%)]$curl http://servera
This is serverb. (version v1.0)
[student@workstation inventory-variables (master *%)]$curl http://servera
This is serverc. (version v1.0)
[student@workstation inventory-variables (master *%)]$curl http://servera
This is serverb. (version v1.0)
[student@workstation inventory-variables (master *%)]$curl http://servera
This is serverc. (version v1.0)
因为需要对ansible-playbook的管理
git commit -a -m "Use Group Vars"
git push
至此试验完成,执行以下命令清理环境
lab inventory-variables finish
cd .. && rm -rf inventory-variables