Merge lp:~michael.nelson/charm-helpers/no-hyphens-in-ansible-yaml-keys into lp:charm-helpers

Proposed by Michael Nelson
Status: Merged
Merged at revision: 100
Proposed branch: lp:~michael.nelson/charm-helpers/no-hyphens-in-ansible-yaml-keys
Merge into: lp:charm-helpers
Diff against target: 134 lines (+66/-3)
4 files modified
charmhelpers/contrib/ansible/__init__.py (+2/-1)
charmhelpers/contrib/templating/contexts.py (+10/-1)
tests/contrib/ansible/test_ansible.py (+3/-1)
tests/contrib/templating/test_contexts.py (+51/-0)
To merge this branch: bzr merge lp:~michael.nelson/charm-helpers/no-hyphens-in-ansible-yaml-keys
Reviewer Review Type Date Requested Status
James Page Approve
Review via email: mp+195931@code.launchpad.net

Commit message

No hyphens in ansible yaml keys.

Description of the change

No hyphens in ansible yaml keys.

As InformatiQ pointed out yesterday, ansible requires that keys don't have hyphens. YAML alows hyphens in key names, so this doesn't affect the saltstack helpers (where you pull the variables in explicitly in your template with private_address = grains["private-address"], but ansible brings the yaml variables into the templates directly, and jinja2 does not allow {{ private-address }} for obvious reasons.

[1] http://www.ansibleworks.com/docs/playbooks_variables.html#what-makes-a-valid-variable-name

To post a comment you must log in.
Revision history for this message
James Page (james-page) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/ansible/__init__.py'
2--- charmhelpers/contrib/ansible/__init__.py 2013-11-19 16:20:38 +0000
3+++ charmhelpers/contrib/ansible/__init__.py 2013-11-20 10:06:29 +0000
4@@ -98,7 +98,8 @@
5 tags = tags or []
6 tags = ",".join(tags)
7 charmhelpers.contrib.templating.contexts.juju_state_to_yaml(
8- ansible_vars_path, namespace_separator='__')
9+ ansible_vars_path, namespace_separator='__',
10+ allow_hyphens_in_keys=False)
11 call = [
12 'ansible-playbook',
13 '-c',
14
15=== modified file 'charmhelpers/contrib/templating/contexts.py'
16--- charmhelpers/contrib/templating/contexts.py 2013-11-07 14:38:35 +0000
17+++ charmhelpers/contrib/templating/contexts.py 2013-11-20 10:06:29 +0000
18@@ -12,7 +12,8 @@
19 charm_dir = os.environ.get('CHARM_DIR', '')
20
21
22-def juju_state_to_yaml(yaml_path, namespace_separator=':'):
23+def juju_state_to_yaml(yaml_path, namespace_separator=':',
24+ allow_hyphens_in_keys=True):
25 """Update the juju config and state in a yaml file.
26
27 This includes any current relation-get data, and the charm
28@@ -23,6 +24,11 @@
29 context to templates, but it may be useful generally to
30 create and update an on-disk cache of all the config, including
31 previous relation data.
32+
33+ By default, hyphens are allowed in keys as this is supported
34+ by yaml, but for tools like ansible, hyphens are not valid [1].
35+
36+ [1] http://www.ansibleworks.com/docs/playbooks_variables.html#what-makes-a-valid-variable-name
37 """
38 config = charmhelpers.core.hookenv.config()
39
40@@ -59,6 +65,9 @@
41 else:
42 existing_vars = {}
43
44+ if not allow_hyphens_in_keys:
45+ config = dict(
46+ (key.replace('-', '_'), val) for key, val in config.items())
47 existing_vars.update(config)
48 with open(yaml_path, "w+") as fp:
49 fp.write(yaml.dump(existing_vars))
50
51=== modified file 'tests/contrib/ansible/test_ansible.py'
52--- tests/contrib/ansible/test_ansible.py 2013-11-19 17:03:40 +0000
53+++ tests/contrib/ansible/test_ansible.py 2013-11-20 10:06:29 +0000
54@@ -111,11 +111,12 @@
55 self.mock_config.return_value = charmhelpers.core.hookenv.Serializable({
56 'group_code_owner': 'webops_deploy',
57 'user_code_runner': 'ubunet',
58+ 'private-address': '10.10.10.10',
59 })
60 self.mock_relation_type.return_value = 'wsgi-file'
61 self.mock_relation_get.return_value = {
62 'relation_key1': 'relation_value1',
63- 'relation_key2': 'relation_value2',
64+ 'relation-key2': 'relation_value2',
65 }
66
67 charmhelpers.contrib.ansible.apply_playbook(
68@@ -127,6 +128,7 @@
69 self.assertEqual({
70 "group_code_owner": "webops_deploy",
71 "user_code_runner": "ubunet",
72+ "private_address": "10.10.10.10",
73 "charm_dir": "",
74 "local_unit": {},
75 "wsgi_file__relation_key1": "relation_value1",
76
77=== modified file 'tests/contrib/templating/test_contexts.py'
78--- tests/contrib/templating/test_contexts.py 2013-11-19 17:03:40 +0000
79+++ tests/contrib/templating/test_contexts.py 2013-11-20 10:06:29 +0000
80@@ -165,3 +165,54 @@
81 "local_unit": "click-index/3",
82 "solr:hostname": "example.com",
83 }, result)
84+
85+ def test_keys_with_hyphens(self):
86+ self.mock_config.return_value = {
87+ 'group_code_owner': 'webops_deploy',
88+ 'user_code_runner': 'ubunet',
89+ 'private-address': '10.1.1.10',
90+ }
91+ self.mock_local_unit.return_value = "click-index/3"
92+ self.mock_relation_get.return_value = None
93+
94+ charmhelpers.contrib.templating.contexts.juju_state_to_yaml(
95+ self.context_path)
96+
97+ with open(self.context_path, 'r') as context_file:
98+ result = yaml.load(context_file.read())
99+ self.assertEqual({
100+ "charm_dir": "/tmp/charm_dir",
101+ "group_code_owner": "webops_deploy",
102+ "user_code_runner": "ubunet",
103+ "local_unit": "click-index/3",
104+ "private-address": "10.1.1.10",
105+ }, result)
106+
107+ def test_keys_with_hypens_not_allowed_in_keys(self):
108+ self.mock_config.return_value = {
109+ 'group_code_owner': 'webops_deploy',
110+ 'user_code_runner': 'ubunet',
111+ 'private-address': '10.1.1.10',
112+ }
113+ self.mock_local_unit.return_value = "click-index/3"
114+ self.mock_relation_type.return_value = 'wsgi-file'
115+ self.mock_relation_get.return_value = {
116+ 'relation-key1': 'relation_value1',
117+ 'relation-key2': 'relation_value2',
118+ }
119+
120+ charmhelpers.contrib.templating.contexts.juju_state_to_yaml(
121+ self.context_path, allow_hyphens_in_keys=False,
122+ namespace_separator='__')
123+
124+ with open(self.context_path, 'r') as context_file:
125+ result = yaml.load(context_file.read())
126+ self.assertEqual({
127+ "charm_dir": "/tmp/charm_dir",
128+ "group_code_owner": "webops_deploy",
129+ "user_code_runner": "ubunet",
130+ "local_unit": "click-index/3",
131+ "private_address": "10.1.1.10",
132+ "wsgi_file__relation_key1": "relation_value1",
133+ "wsgi_file__relation_key2": "relation_value2",
134+ }, result)

Subscribers

People subscribed via source and target branches