Skip to content

Commit 5c256ab

Browse files
committed
Merge pull request #1 from m-r-c/vhosts
Vhosts
2 parents e40dd86 + a64f090 commit 5c256ab

File tree

13 files changed

+288
-399
lines changed

13 files changed

+288
-399
lines changed

ansible/library/tomcat_connector

+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# This file is part of ALA ansible scripts
5+
#
6+
# TODO Licence
7+
8+
DOCUMENTATION = '''
9+
---
10+
module: tomcat_connector
11+
short_description: Add or remove a Tomcat connector
12+
description:
13+
- Add or remove a Tomcat connector.
14+
version_added: "1.8.2"
15+
options:
16+
name:
17+
description:
18+
- the name of the connector
19+
required: true
20+
choices: [ "HTTP/1.1", "AJP/1.3" ]
21+
service:
22+
description:
23+
- the name of the service to put the connector in
24+
required: false
25+
default: Catalina
26+
bind_addr:
27+
description:
28+
- The ip address to bind the server socket to
29+
required: false
30+
default: 0.0.0.0
31+
port:
32+
description:
33+
- the port to use for connector
34+
- will use default port for protocol (8080 for HTTP or 8009 for AJP) if omitted
35+
required: false
36+
connection_timeout:
37+
description:
38+
- the amount of time in ms to wait for the client before timing out
39+
required: false
40+
default: 20000
41+
uri_encoding:
42+
description:
43+
- the charset for uri encoding
44+
required: false
45+
default: UTF-8
46+
redirect_port:
47+
description:
48+
- the port to automatically redirect to for requests that require servlet security-constraints
49+
required: false
50+
default: 443
51+
state:
52+
description:
53+
- The connector state
54+
required: false
55+
default: present
56+
choices: [ "present", "absent" ]
57+
tomcat_conf_dir:
58+
description:
59+
- The tomcat conf directory
60+
required: false
61+
default: /etc/tomcat7 on debian, /etc/tomcat6 on enterprise linux
62+
notes:
63+
- This module uses I(python-augeas), a Python Augeas binding library. You must ensure that python-agueas is installed
64+
on the host before using this module.
65+
- This module uses I(python-libxml2), a Python binding for the libxml2 library. You must ensure that python-libxml2
66+
is installed on the host before using this module.
67+
requirements: [ python-augeas, python-libxml2 ]
68+
author: Simon Bear
69+
'''
70+
71+
EXAMPLES = '''
72+
'''
73+
74+
try:
75+
import augeas
76+
except ImportError:
77+
augeas_found = False
78+
else:
79+
augeas_found = True
80+
81+
import logging
82+
logging.basicConfig(level=logging.DEBUG)
83+
log = logging.getLogger('tomcat_connector')
84+
85+
class NotSupportedError(Exception):
86+
pass
87+
88+
89+
# ===========================================
90+
# Tomcat Service module specific support methods.
91+
#
92+
93+
def load_augeas_for_file(root="/etc/tomcat7", lens="Xml.lns", filename="server.xml", check_mode=False):
94+
aug = augeas.Augeas(root, None, augeas.Augeas.NO_LOAD | augeas.Augeas.NO_MODL_AUTOLOAD | (
95+
augeas.Augeas.SAVE_NOOP if check_mode else augeas.Augeas.SAVE_BACKUP))
96+
aug.add_transform(lens, filename)
97+
aug.load()
98+
return aug
99+
100+
101+
def tomcat_dir_for_distrib(distribution):
102+
if distribution.lower() in ["redhat", "centos"]:
103+
return '/etc/tomcat6'
104+
else:
105+
return '/etc/tomcat7'
106+
107+
108+
def xml_lint(filename):
109+
try:
110+
import libxml2
111+
libxml2.thrDefIndentTreeOutput(1)
112+
libxml2.keepBlanksDefault(0)
113+
x = libxml2.parseFile(filename)
114+
x.saveFormatFile(filename, 1)
115+
except ImportError:
116+
pass
117+
118+
119+
def b2xs(string):
120+
return str(string).lower()
121+
122+
123+
def default_port(name):
124+
return 8080 if name == "HTTP/1.1" else 8009
125+
126+
127+
def add_connector(aug, protocol, service, bind_addr, port, connection_timeout=20000, uri_encoding="UTF-8", redirect_port=443):
128+
129+
aug.defvar("service", "/files/server.xml/Server/Service[#attribute/name=\"%(service)s\"]" % locals())
130+
131+
log.debug("add_connector")
132+
log.debug(locals())
133+
134+
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/port" % locals(), str(port))
135+
136+
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/protocol" % locals(), protocol)
137+
138+
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/address" % locals(), bind_addr)
139+
140+
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/URIEncoding" % locals(), uri_encoding)
141+
142+
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/redirectPort" % locals(), str(redirect_port))
143+
144+
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/connectionTimeout" % locals(),
145+
str(connection_timeout))
146+
147+
148+
def remove_connector(aug, protocol, port):
149+
aug.remove("$service/Connector[#attribute/port=\"%(port)s\"]" % locals())
150+
151+
152+
# ===========================================
153+
# Module execution.
154+
#
155+
156+
157+
def main():
158+
module = AnsibleModule(
159+
argument_spec=dict(
160+
name=dict(required=True, choices=["HTTP/1.1", "AJP/1.3"]),
161+
service=dict(default="Catalina"),
162+
bind_addr=dict(default="0.0.0.0"),
163+
port=dict(type="int"),
164+
connection_timeout=dict(default=20000, type="int"),
165+
uri_encoding=dict(default="UTF-8"),
166+
redirect_port=dict(default=443, type="int"),
167+
state=dict(default="present", choices=["absent", "present"]),
168+
tomcat_conf_dir=dict(default=None)
169+
),
170+
supports_check_mode=True
171+
)
172+
173+
if not augeas_found:
174+
module.fail_json(msg="the python augeas module is required")
175+
return
176+
177+
name = module.params["name"]
178+
service = module.params["service"]
179+
bind_addr = module.params["bind_addr"]
180+
port = module.params["port"]
181+
connection_timeout = module.params["connection_timeout"]
182+
uri_encoding = module.params["uri_encoding"]
183+
redirect_port = module.params["redirect_port"]
184+
state = module.params["state"]
185+
186+
tomcat_conf_dir = module.params["tomcat_conf_dir"]
187+
188+
if not port:
189+
port = default_port(name)
190+
191+
if not tomcat_conf_dir:
192+
distribution = get_distribution()
193+
tomcat_conf_dir = tomcat_dir_for_distrib(distribution)
194+
195+
try:
196+
aug = load_augeas_for_file(root=tomcat_conf_dir, check_mode=module.check_mode)
197+
except Exception, e:
198+
module.fail_json(msg="unable to connect to Augeas: %s" % e)
199+
return
200+
201+
try:
202+
if state == "absent":
203+
remove_connector(aug, name, port)
204+
else:
205+
add_connector(aug, name, service, bind_addr, port, connection_timeout, uri_encoding, redirect_port)
206+
207+
try:
208+
aug.save()
209+
except Exception, e:
210+
log.exception("Couldn't save server.xml")
211+
saved = aug.get("/augeas/events/saved")
212+
errors = aug.match("//errors/*")
213+
aug.close()
214+
if saved and not module.check_mode:
215+
xml_lint(tomcat_conf_dir + "/server.xml")
216+
module.exit_json(changed=True if saved else False, name=name, saved=saved, errors=errors)
217+
218+
except Exception, e:
219+
log.exception("exception while performing augeas operations")
220+
module.fail_json(msg="exception while performing augeas operations: %s" % e)
221+
222+
# import module snippets
223+
from ansible.module_utils.basic import *
224+
225+
main()

ansible/library/tomcat_service

+2-90
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,6 @@ options:
1818
- the name of the Service
1919
required: false
2020
default: Catalina
21-
bind_addr:
22-
description:
23-
- The ip address to bind the ajp and http server sockets to
24-
required: false
25-
default: 0.0.0.0
26-
http:
27-
description:
28-
- whether to add an http connector
29-
required: false
30-
default: true
31-
http_port:
32-
description:
33-
- the port to use for the http connector
34-
required: false
35-
default: 8080
36-
ajp:
37-
description:
38-
- whether to add an ajp connector
39-
required: false
40-
default: false
41-
ajp_port:
42-
description:
43-
- the port to use for the ajp connector
44-
required: false
45-
default: 8009
4621
state:
4722
description:
4823
- The database state
@@ -115,63 +90,10 @@ def b2xs(string):
11590
return str(string).lower()
11691

11792

118-
def add_connector(aug, protocol, bind_addr, port, connectionTimeout=None):
119-
120-
log.debug("add_connector")
121-
log.debug(locals())
122-
123-
#log.debug(aug.match("$service/*"))
124-
125-
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/port" % locals(), str(port))
126-
127-
#connectors = aug.match("$service/Connector[#attribute/protocol=\"%(protocol)s\"][#attribute/port=\"%(port)s\"]")
128-
129-
#if not connectors:
130-
#aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/port", str(port))
131-
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/protocol" % locals(), protocol)
132-
133-
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/address" % locals(), bind_addr)
134-
135-
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/URIEncoding" % locals(), "UTF-8")
136-
137-
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/redirectPort" % locals(), "443")
138-
139-
if connectionTimeout:
140-
aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/connectionTimeout" % locals(), str(connectionTimeout))
141-
else:
142-
aug.remove("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/connectionTimeout" % locals())
143-
144-
145-
def remove_connector(aug, protocol, port):
146-
aug.remove("$service/Connector[#attribute/port=\"%(port)s\"]" % locals())
147-
148-
149-
def create_service(aug, name, bind_addr, http, http_port, ajp, ajp_port):
150-
#<Connector port="8080" protocol="HTTP/1.1"
151-
# connectionTimeout="20000"
152-
# URIEncoding="UTF-8"
153-
# redirectPort="8443" />
154-
#<Connector port="{{ tomcat_ajp_port | default('8009') }}" protocol="AJP/1.3"
155-
# address="{{ tomcat_bind_addr | default('0.0.0.0') }}"
156-
# redirectPort="443"
157-
# URIEncoding="UTF-8" />
158-
159-
93+
def create_service(aug, name):
16094

16195
aug.set("/files/server.xml/Server/Service[#attribute/name=\"%(name)s\"]/#attribute/name" % locals(), name)
16296

163-
aug.defvar("service", "/files/server.xml/Server/Service[#attribute/name=\"%(name)s\"]" % locals())
164-
165-
if http:
166-
add_connector(aug, "HTTP/1.1", bind_addr, http_port, 20000)
167-
else:
168-
remove_connector(aug, "HTTP/1.1", http_port)
169-
170-
if ajp:
171-
add_connector(aug, "AJP/1.3", bind_addr, ajp_port)
172-
else:
173-
remove_connector(aug, "AJP/1.3", ajp_port)
174-
17597

17698
def remove_service(aug, name):
17799
aug.remove("/files/server.xml/Server/Service[name=\"%(name)s\"]")
@@ -186,11 +108,6 @@ def main():
186108
module = AnsibleModule(
187109
argument_spec=dict(
188110
name=dict(default="Catalina"),
189-
bind_addr=dict(default="0.0.0.0"),
190-
http=dict(default=True, type="bool"),
191-
http_port=dict(default=8080, type="int"),
192-
ajp=dict(default=False, type="bool"),
193-
ajp_port=dict(default=8009, type="int"),
194111
state=dict(default="present", choices=["absent", "present"]),
195112
tomcat_conf_dir=dict(default=None)
196113
),
@@ -202,11 +119,6 @@ def main():
202119
return
203120

204121
name = module.params["name"]
205-
bind_addr = module.params["bind_addr"]
206-
http = module.params["http"]
207-
http_port = module.params["http_port"]
208-
ajp = module.params["ajp"]
209-
ajp_port = module.params["ajp_port"]
210122
state = module.params["state"]
211123

212124
tomcat_conf_dir = module.params["tomcat_conf_dir"]
@@ -227,7 +139,7 @@ def main():
227139
if state == "absent":
228140
remove_service(aug, name)
229141
else:
230-
create_service(aug, name, bind_addr, http, http_port, ajp, ajp_port)
142+
create_service(aug, name)
231143

232144
try:
233145
aug.save()

ansible/roles/apache/tasks/main.yml

+2-10
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,11 @@
1414
apache2_module: state=present name=proxy_http
1515
when: ansible_os_family == "Debian"
1616

17-
- name: Enable mod_proxy_ajp (enables mod_proxy transitively)
17+
- name: Enable mod_proxy_ajp
1818
apache2_module: state=present name=proxy_ajp
1919
when: ansible_os_family == "Debian"
20-
21-
- name: Create Apache virtual host config file
22-
template: src=apache-default-vhost.j2 dest=/etc/apache2/sites-available/000-default.conf owner=root group=root
2320
notify:
24-
- reload apache
25-
26-
#- name: Enable Apache virtual host
27-
# command: "a2ensite default-host.conf"
28-
# notify:
29-
# - reload apache
21+
- restart apache
3022

3123
- name: Start Apache2 (Debian)
3224
service: name=apache2 state=restarted

0 commit comments

Comments
 (0)