当我们完成了apache基本的配置之后,我们可能就需要增加虚拟主机了,其实我们最核心的就是这一段代码;
1 2 3 4 5 6 7 |
<VirtualHost *:80> ServerAdmin maple_m@hotmail.com DocumentRoot /abc/dec/test ServerName test.503error.com ErrorLog logs/503error.com-error_log CustomLog logs/503error.com-access_log common </VirtualHost> |
虚拟主机我们一般认为是端口80,所以上边域名我们可以翻译成:
1 2 3 4 5 |
<VirtualHost *:80> ServerAdmin {{ item.adminEmail }} DocumentRoot /abc/dec/{{ item.folername }} ServerName {{ item.domainname }} </VirtualHost> |
日志先不添加了
然后我们在调用的时候是这么调用的,我们增加了一个新的main.yml文件在roles/webservers/vars/ 下,这里边存放这当前这个role的变量
1 2 3 4 5 6 7 8 9 10 11 12 |
--- apache_vhosts_enabled: - domainname: "www.503error.com" adminEmail: "maple_m@homtail.com" folername: "503" configname: "503config" - domainname: "www.504error.com" adminEmail: "maple_m@homtail.com" folername: "504" configname: "504config" |
下面是调用,我们新增了一个yml文件在 roles/webservers/tasks,叫作apache_vhost_conf.yml
内容如下:
1 2 3 4 5 6 7 8 9 |
--- - debug: var="{{item.domainname}}" with_items: "{{ apache_vhosts_enabled }}" - name: create config for every site template: src=apache_vhost.conf.j2 dest=/etc/httpd/conf.d/{{item.domainname}}.conf with_items: "{{ apache_vhosts_enabled }}" |
然后在tasks/mian.yml中include
1 |
- include: apache_vhost_conf.yml |
测试,没有问题,我们现在结构是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
ansible_home/ ├── main.yml └── roles ├── common │ ├── defaults │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ ├── templates │ └── vars ├── dbservers │ ├── defaults │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ ├── templates │ └── vars └── webservers ├── defaults ├── files ├── handlers ├── meta ├── tasks │ ├── apache_conf.yml │ ├── apache_vhost_conf.yml │ └── main.yml ├── templates │ ├── apache_conf.j2 │ └── apache_vhost.conf.j2 └── vars └── main.yml |
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021