Skip to content

Commit 7f75241

Browse files
Merge pull request #12 from fabriziopandini/0.7.2
0.7.2
2 parents 1108121 + 22721f8 commit 7f75241

File tree

8 files changed

+170
-95
lines changed

8 files changed

+170
-95
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ Vagrantfile
1717

1818
# Test
1919
provisioning
20-
declarative.yaml
20+
*.yaml

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@ ansible_group_vars_path and ansible_host_vars_path are not supported anymore
3535
# 0.7.0 (November 02, 2016)
3636

3737
* introduced support for declarative cluster definition
38+
39+
# 0.7.1 (November 04, 2016)
40+
41+
* Minor fix
42+
43+
# 0.7.2 (November 16, 2016)
44+
45+
* issues #11 Allow management of Ansible vars for all hosts
46+
* issues #9 Create group_vars and host_vars directory only if necessary
47+
48+
* breacking change: custom group of groups all_groups:children removed. Insteal use all (automatically created by ansible)

doc/declarative.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ kubernetes:
295295
...
296296
...
297297
ansible_group_vars:
298-
etcd :
298+
etcd:
299299
var1: ...
300-
docker :
300+
docker:
301301
var2: ...
302302
var3: ...
303303
...
@@ -308,6 +308,17 @@ Group vars can be set to literal value or to Jinja2 value generators, that will
308308
- **context_vars** see below
309309
- **nodes**, list of nodes in the ansible_group to which the group_vars belong
310310

311+
Additionally it is possible to set variables for all groups/all hosts, by setting vars for the pre-defined `all` group of groups:
312+
313+
```yaml
314+
kubernetes:
315+
...
316+
ansible_group_vars:
317+
all:
318+
var1: ...
319+
...
320+
```
321+
311322
Ansible group vars will be stored into yaml files saved into `{cluster.ansible_playbook_path}\group_vars` folder.
312323

313324
The variable `cluster.ansible_playbook_path` defaults to the current directory (the directory of the Vagrantfile) + `/provisioning`; this value can be changed like any other cluster attributes (see Defining cluster & cluster attributes).
@@ -384,18 +395,42 @@ Context vars can be set to literal value or to Jinja2 value generators, that wil
384395

385396
- nodes, list of nodes in the ansible_group to which the group_vars belong
386397

387-
388398
> Context_vars generator are always executed before group_vars and host_vars generators; the resulting context, is given in input to group_vars and host_vars generators.
389399

400+
> In addition to context vars for groups, it is possible to create context_vars for all groups/all hosts, by setting vars for the pre-defined `all` group of groups; in this case, intuitively, the list of nodes whitin the context contains all the nodes.
401+
390402
Then, you can use the above context var when generating group_vars for host vars.
391403

404+
```yaml
405+
kubernetes:
406+
box: centos/7
407+
master:
408+
...
409+
ansible_groups:
410+
- kb8-master
411+
minions:
412+
...
413+
ansible_groups:
414+
- kb8-minions
415+
416+
ansible_context_vars:
417+
all:
418+
var0: "{{ nodes | count }}"
419+
kb8-master:
420+
var1: "{{ nodes | count }}"
421+
422+
ansible_group_vars:
423+
all:
424+
var0_from_context: "{{ context['var0'] }}"
425+
kb8-master:
426+
var1_from_context: "{{ context['var1'] }}"
427+
```
428+
392429
### Group of groups
393430

394431
A useful ansible inventory feature is [group of groups](http://docs.ansible.com/ansible/intro_inventory.html#hosts-and-groups).
395432

396-
By default vagrant-compose will generate a group named `[all_groups:children]` with all the ansible_groups defined in cluster configuration; however:
397-
- you cannot rename all_groups
398-
- you cannot exclude any ansible group from all_groups.
433+
By default ansible has a group named `[all]` with all nodes in the cluster.
399434

400435
If you need higher control on groups of groups you can simply add a new item to the variable `config.cluster.ansible_groups` before creating nodes.
401436

doc/programmatic.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ end
302302

303303
As you can see, `consul` and `docker` ansible_groups now include both nodes from `consul-agent` and `load-balancer` node set; vice versa, other groups like `registrator`, `consul-template`, `nginx` contain node only from one of the two nodes set.
304304

305-
Ansible playbook can leverage on groups for providing machines with the required software stacks.
305+
Ansible playbook can additionally leverage on groups for providing machines with the required software stacks.
306306

307-
NB. you can see resulting ansible_groups by using `debug` command with `verbose` equal to `true`.
307+
> NB. you can see resulting ansible_groups by using `debug` command with `verbose` equal to `true`.
308308
309309
### Defining group vars
310310

@@ -329,6 +329,18 @@ config.cluster.compose('test') do |c|
329329
end
330330
```
331331

332+
Additionally, it is possible to set variables for all groups/all hosts, by setting vars for the pre-defined `all` group of groups:
333+
334+
``` ruby
335+
config.cluster.compose('test') do |c|
336+
...
337+
c.ansible_group_vars['all'] = lambda { |context, nodes|
338+
return { 'var0' => nodes.length }
339+
}
340+
...
341+
end
342+
```
343+
332344
Ansible group vars will be stored into yaml files saved into `{cluster.ansible_playbook_path}\group_vars` folder.
333345

334346
The variable `cluster.ansible_playbook_path` defaults to the current directory (the directory of the Vagrantfile) + `/provisioning`; this value can be changed like any other cluster attributes (see Defining cluster attributes).
@@ -379,6 +391,8 @@ end
379391

380392
> Context_vars generator are always executed before group_vars and host_vars generators; the resulting context, is given in input to group_vars and host_vars generators.
381393
394+
> In addition to context vars for groups, it is possible to create context_vars for all groups/all hosts, by setting vars for the pre-defined `all` group of groups; in this case, intuitively, the list of nodes whitin the context contains all the nodes.
395+
382396
Then, you can use the above context var when generating group_vars for nodes in the `consul-agent` group.
383397

384398
``` ruby
@@ -397,9 +411,7 @@ end
397411
### Group of groups
398412
A useful ansible inventory feature is [group of groups](http://docs.ansible.com/ansible/intro_inventory.html#hosts-and-groups).
399413

400-
By default vagrant-compose will generate a group named `[all_groups:children]` with all the ansible_groups defined in cluster configuration; however:
401-
- you cannot rename all_groups
402-
- you cannot exclude any ansible group from all_groups.
414+
By default ansible has a group named `[all]` with all the nodes defined in cluster configuration.
403415

404416
If you need higher control on groups of groups you can simply add a new item to the variable `config.cluster.ansible_groups` before creating nodes.
405417

lib/vagrant/compose/config.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ def filterInventory(inventory)
6262
end
6363
end
6464

65-
ansible_groups['all_groups:children'] = ansible_groups.keys
66-
6765
return ansible_groups
6866
end
6967

lib/vagrant/compose/declarative/cluster.rb

Lines changed: 84 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@ class Cluster
2424
# It defaults to current directory/provisioning
2525
attr_accessor :ansible_playbook_path
2626

27-
# Costruttore di una istanza di cluster.
27+
# Costruttore di una istanza di cluster.
2828
def initialize()
29-
@multimachine_filter = ""
29+
@multimachine_filter = ""
3030
end
3131

3232
# Implements cluster creation from a playbook file
33-
def from (file)
34-
# calls vagrant-playbook utility for executing the playbook file.
35-
playbook = YAML.load(pycompose (file))
33+
def from (file)
34+
# calls vagrant-playbook utility for executing the playbook file.
35+
playbook = YAML.load(pycompose (file))
3636

37-
# extract cluster attributes
38-
@name = playbook.keys[0]
39-
@box = playbook[@name]['box']
40-
@domain = playbook[@name]['domain']
41-
@ansible_playbook_path = playbook[@name]['ansible_playbook_path']
37+
# extract cluster attributes
38+
@name = playbook.keys[0]
39+
@box = playbook[@name]['box']
40+
@domain = playbook[@name]['domain']
41+
@ansible_playbook_path = playbook[@name]['ansible_playbook_path']
4242

4343
# extract nodes
4444
nodes = []
4545
playbook[@name]['nodes'].each do |node|
4646

47-
boxname = node.keys[0]
47+
boxname = node.keys[0]
4848

4949
box = node[boxname]['box']
5050
hostname = node[boxname]['hostname']
@@ -64,74 +64,85 @@ def from (file)
6464
# extract ansible inventory, ansible_group_vars, ansible_host_vars
6565
ansible_groups = {}
6666
if playbook[@name].key?("ansible")
67-
ansible = playbook[@name]['ansible']
68-
69-
# extract ansible inventory
70-
ansible_groups = ansible['inventory']
71-
72-
# cleanup ansible_group_vars files
73-
# TODO: make safe
74-
ansible_group_vars_path = File.join(@ansible_playbook_path, 'group_vars')
75-
76-
FileUtils.mkdir_p(ansible_group_vars_path) unless File.exists?(ansible_group_vars_path)
77-
Dir.foreach(ansible_group_vars_path) {|f| fn = File.join(ansible_group_vars_path, f); File.delete(fn) if f.end_with?(".yml")}
78-
79-
#generazione ansible_group_vars file (NB. 1 group = 1 gruppo host ansible)
80-
ansible['group_vars'].each do |group, vars|
81-
# crea il file (se sono state generate delle variabili)
82-
unless vars.empty?
83-
# TODO: make safe
84-
File.open(File.join(ansible_group_vars_path,"#{group}.yml") , 'w+') do |file|
85-
file.puts YAML::dump(vars)
86-
end
87-
end
88-
end
89-
90-
# cleanup ansible_host_vars files (NB. 1 nodo = 1 host)
91-
# TODO: make safe
92-
ansible_host_vars_path = File.join(@ansible_playbook_path, 'host_vars')
93-
94-
FileUtils.mkdir_p(ansible_host_vars_path) unless File.exists?(ansible_host_vars_path)
95-
Dir.foreach(ansible_host_vars_path) {|f| fn = File.join(ansible_host_vars_path, f); File.delete(fn) if f.end_with?(".yml")}
96-
97-
#generazione ansible_host_vars file
98-
ansible['host_vars'].each do |host, vars|
99-
# crea il file (se sono state generate delle variabili)
100-
unless vars.empty?
101-
# TODO: make safe
102-
File.open(File.join(ansible_host_vars_path,"#{host}.yml") , 'w+') do |file|
103-
file.puts YAML::dump(vars)
104-
end
105-
end
106-
end
107-
end
10867

109-
return nodes, ansible_groups
110-
end
68+
ansible = playbook[@name]['ansible']
69+
70+
# extract ansible inventory
71+
ansible_groups = ansible['inventory']
72+
73+
# cleanup ansible_group_vars files
74+
# TODO: make safe
75+
ansible_group_vars_path = File.join(@ansible_playbook_path, 'group_vars')
76+
77+
if File.exists?(ansible_group_vars_path)
78+
Dir.foreach(ansible_group_vars_path) {|f| fn = File.join(ansible_group_vars_path, f); File.delete(fn) if f.end_with?(".yml")}
79+
end
80+
81+
#generazione ansible_group_vars file (NB. 1 group = 1 gruppo host ansible)
82+
if ansible.key?("group_vars")
83+
ansible['group_vars'].each do |group, vars|
84+
# crea il file (se sono state generate delle variabili)
85+
unless vars.empty?
86+
FileUtils.mkdir_p(ansible_group_vars_path) unless File.exists?(ansible_group_vars_path)
87+
# TODO: make safe
88+
fileName = group.gsub(':', '_')
89+
File.open(File.join(ansible_group_vars_path,"#{fileName}.yml") , 'w+') do |file|
90+
file.puts YAML::dump(vars)
91+
end
92+
end
93+
end
94+
end
95+
96+
# cleanup ansible_host_vars files (NB. 1 nodo = 1 host)
97+
# TODO: make safe
98+
ansible_host_vars_path = File.join(@ansible_playbook_path, 'host_vars')
99+
100+
if File.exists?(ansible_host_vars_path)
101+
Dir.foreach(ansible_host_vars_path) {|f| fn = File.join(ansible_host_vars_path, f); File.delete(fn) if f.end_with?(".yml")}
102+
end
103+
104+
#generazione ansible_host_vars file
105+
if ansible.key?("host_vars")
106+
ansible['host_vars'].each do |host, vars|
107+
# crea il file (se sono state generate delle variabili)
108+
unless vars.empty?
109+
FileUtils.mkdir_p(ansible_host_vars_path) unless File.exists?(ansible_host_vars_path)
110+
111+
# TODO: make safe
112+
File.open(File.join(ansible_host_vars_path,"#{host}.yml") , 'w+') do |file|
113+
file.puts YAML::dump(vars)
114+
end
115+
end
116+
end
117+
end
118+
end
111119

120+
return nodes, ansible_groups
121+
end
112122

113-
# Executes pycompose command
114-
def pycompose (file)
115-
p_err = ""
116-
p_out = ""
117123

118-
begin
119-
p_status = Open4::popen4("vagrant-playbook -f #{file}") do |pid, stdin, stdout, stderr|
120-
p_err = stderr.read.strip
121-
p_out = stdout.read.strip
122-
end
123-
rescue Errno::ENOENT
124-
raise VagrantPlugins::Compose::Errors::PyComposeMissing
125-
rescue Exception => e
126-
raise VagrantPlugins::Compose::Errors::PyComposeError, :message => e.message
127-
end
124+
# Executes pycompose command
125+
def pycompose (file)
126+
p_err = ""
127+
p_out = ""
128+
129+
begin
130+
p_status = Open4::popen4("vagrant-playbook -f #{file}") do |pid, stdin, stdout, stderr|
131+
p_err = stderr.read.strip
132+
p_out = stdout.read.strip
133+
end
134+
rescue Errno::ENOENT
135+
raise VagrantPlugins::Compose::Errors::PyComposeMissing
136+
rescue Exception => e
137+
raise VagrantPlugins::Compose::Errors::PyComposeError, :message => e.message
138+
end
128139

129-
if p_status.exitstatus != 0
130-
raise VagrantPlugins::Compose::Errors::PyComposeError, :message => p_err
131-
end
140+
if p_status.exitstatus != 0
141+
raise VagrantPlugins::Compose::Errors::PyComposeError, :message => p_err
142+
end
132143

133-
return p_out
134-
end
144+
return p_out
145+
end
135146

136147
end
137148
end

0 commit comments

Comments
 (0)