我开始研究ansible,将其作为一种可行的office配置管理工具,设置测试环境等。我已经设法涵盖了大多数基本的使用场景,使用剧本概述任务和协调角色。
我已经为文档vhost和mod_proxy vhost等特定场景创建了一个非常简单的apache2角色。我被卡住了,我不知道如何编写剧本来在相同的主机上执行这些不同的vhosts配置,即指向/var/www/vhosts/node1.localhost的域node1.localhost和为tomcat ajp port上的另一个机器做反向代理的node2.localhost。
下面是我的角色:
#roles/apache/tasks/main.yaml
---
- name: Update apt cache
apt: update_cache=yes
- name: Install Apache2
apt:
name=apache2
state=present
- name: Creating a Document virtualhost file
template:
src=http_vhost.conf.j2
dest=/etc/apache2/site-available/{{ apache_domain_name }}.conf
mode=0644
when: document_vhost == True
- name: Creating a Reverse Proxy virtualhost file
template:
src=http_vhost_reverse_proxy.conf.j2
dest=/etc/apache2/site-available/{{ apache_domain_name }}.conf
mode=0644
when: proxy_vhost == True
- name: Activate Vhosts
command: a2ensite {{ apache_domain_name }}.conf
when: activate_vhost|bool
notify: Reload Apache
- name: Deactivate Vhosts
command: a2dissite {{ apache_domain_name }}.conf
when: deactivate_vhost|bool
notify: Reload-Apache文档vhost模板文件
#roles/templates/http_vhost.conf.j2
<VirtualHost *:{{ apache_port }}>
ServerAdmin {{ apache_admin_email_address }}
ServerName {{ apache_domain_name }}
{% if apache_domain_aliases is defined %}
{% for serveralias in apache_domain_aliases %}
ServerAlias {{ serveralias }}
{% endfor %}
{% endif %}
DocumentRoot /var/www/vhosts{{ apache_domain_name}}/htdocs
<Directory /var/www/vhosts{{ apache_domain_name}}/htdocs>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/{{ apache_domain_name }}_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel {{ apache_log_level|default('warn') }}
CustomLog ${APACHE_LOG_DIR}/{{ apache_domain_name }}_access.log combined
</VirtualHost>反向代理vhost模板文件
#roles/apache/templates/http_vhost_reverse_proxy.conf.j2
<VirtualHost *:{{ apache_port }}>
ProxyRequests off
ProxyPreserveHost On
ServerAdmin {{ apache_admin_email_address }}
ServerName {{ apache_domain_name }}
{% if apache_domain_aliases if defined %}
{% for serveralias in apache_domain_aliases %}
ServerAlias {{ serveralias }}
{% endfor %}
{% endif %}
{% if apache_vhosts if defined %}
{% for vhost in apache_vhosts %}
ProxyPass {{ vhost.apache_proxy_context }} {{ vhost.reversed_server_mod }}://{{ vhost.reversed_server_ip }}:{{ vhost.reversed_server_port }}{{ vhost.reversed_server_context }}
ProxyPassReverse {{ vhost.apache_proxy_context }} {{ vhost.reversed_server_mod }}://{{ vhost.reversed_server_ip }}:{{ vhost.reversed_server_port }}{{ vhost.reversed_server_context }}
{% endfor %}
{% endif %}
ErrorLog ${APACHE_LOG_DIR}/{{ apache_domain_name }}_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel {{ apache_log_level|default('warn') }}
CustomLog ${APACHE_LOG_DIR}/{{ apache_domain_name }}_access.log combined
</VirtualHost>假设我的节点IP为192.168.56.106,应该同时处理node1.localhost常规vhost和node2.localhost代理vhost。
感谢你的帮助
发布于 2015-05-05 07:39:00
设置变量的方式(对常规vhost和代理vhost使用相同的变量名)将不起作用。这不是ansible的问题(ansible不关心),Apache不会对生成的文件感到满意。您不能为同一FQDN配置两个不同的配置。此外,您对两个配置文件使用相同的文件名,因此代理配置将破坏任何具有相同名称的现有vhost配置。
名称模式看起来在只激活vhost或proxy配置的情况下可以很好地工作,但如果您希望在一台主机上同时激活这两种配置,则必须重新考虑变量名称或如何使用它们。也许就像下面这样,所以你只需要定义异常?
{{ proxy_apache_domain_name | default(apache_domain_name) }}至于控制哪个主机上的哪个配置应该是活动的,我在角色的defaults/main.yml中将document_vhost和proxy_vhost的默认值设置为false,然后在主机的host_vars中有选择地打开每个主机。
这应该允许您在主机上打开/关闭特定的配置。
roles/apache/defaults/main.yaml
document_vhost: False
proxy_vhost: Falsehost_vars/192.168.56.106
document_vhost: True
proxy_vhost: True发布于 2015-09-04 06:36:27
我知道这太晚了,可能没有回答你直接的问题,但是有一些角色可以完成大部分任务https://github.com/geerlingguy/ansible-role-apache
https://stackoverflow.com/questions/30014323
复制相似问题