Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly template list of hosts in playbooks. #441

Merged
merged 1 commit into from
Jun 1, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/ansible/playbook/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ def __init__(self, playbook, ds):

# TODO: more error handling

hosts = ds.get('hosts')
if hosts is None:
raise errors.AnsibleError('hosts declaration is required')
elif isinstance(hosts, list):
hosts = ';'.join(hosts)
hosts = utils.template(hosts, playbook.extra_vars, {})

self._ds = ds
self.playbook = playbook
self.hosts = ds.get('hosts', None)
self.hosts = utils.template(self.hosts, self.playbook.extra_vars, {})
self.hosts = hosts
self.name = ds.get('name', self.hosts)
self.vars = ds.get('vars', {})
self.vars_files = ds.get('vars_files', [])
Expand All @@ -56,10 +62,6 @@ def __init__(self, playbook, ds):
self._tasks = self._load_tasks(self._ds, 'tasks')
self._handlers = self._load_tasks(self._ds, 'handlers')

if self.hosts is None:
raise errors.AnsibleError('hosts declaration is required')
if isinstance(self.hosts, list):
self.hosts = ';'.join(self.hosts)
if self.sudo_user != 'root':
self.sudo = True

Expand Down
13 changes: 13 additions & 0 deletions test/TestPlayBook.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,16 @@ def test_one(self):
print data
assert data.find("ears") != -1, "template success"

def test_yaml_hosts_list(self):
# Make sure playbooks support hosts: [host1, host2]
# TODO: Actually run the play on more than one host
test_callbacks = TestCallbacks()
playbook = ansible.playbook.PlayBook(
playbook=os.path.join(self.test_dir, 'hosts_list.yml'),
host_list='test/ansible_hosts',
stats=ans_callbacks.AggregateStats(),
callbacks=test_callbacks,
runner_callbacks=test_callbacks
)
play = ansible.playbook.Play(playbook, playbook.playbook[0])
assert play.hosts == ';'.join(('host1', 'host2', 'host3'))
5 changes: 5 additions & 0 deletions test/hosts_list.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Test that playbooks support YAML lists of hosts.
---
- hosts: [host1, host2, host3]
tasks:
- action: command true