In playbooks, each pattern stanza can reference it's own user to run as, so you can

run things as multiple sets of users (if you want) in the same playbook.
This commit is contained in:
Michael DeHaan 2012-02-26 19:58:56 -05:00
parent 61d064d011
commit 78a254fc52
2 changed files with 12 additions and 8 deletions

View file

@ -1,9 +1,6 @@
- pattern: '*'
hosts: /etc/ansible/hosts
tasks:
- do:
- restart apache for kicks
- command /sbin/service apache restart
- do:
- configure template & module variables for future template calls
- setup a=2 b=3 c=4

View file

@ -88,7 +88,8 @@ class PlayBook(object):
}
return results
def _run_task(self, pattern=None, task=None, host_list=None, handlers=None, conditional=False):
def _run_task(self, pattern=None, task=None, host_list=None,
remote_user=None, handlers=None, conditional=False):
'''
run a single task in the playbook and
recursively run any subtasks.
@ -115,10 +116,10 @@ class PlayBook(object):
module_args=module_args,
host_list=host_list,
forks=self.forks,
remote_user=self.remote_user, # FIXME: read from playbook
remote_pass=self.remote_pass,
module_path=self.module_path,
timeout=self.timeout
timeout=self.timeout,
remote_user=remote_user
)
results = runner.run()
@ -198,6 +199,7 @@ class PlayBook(object):
pattern = pg['pattern']
tasks = pg['tasks']
handlers = pg['handlers']
user = pg.get('user', C.DEFAULT_REMOTE_USER)
self.host_list = pg.get('hosts', '/etc/ansible/hosts')
@ -205,7 +207,11 @@ class PlayBook(object):
print "PLAY: [%s] from [%s] ********** " % (pattern, self.host_list)
for task in tasks:
self._run_task(pattern=pattern, task=task, handlers=handlers)
self._run_task(
pattern=pattern,
task=task,
handlers=handlers,
remote_user=user)
for task in handlers:
if type(task.get("run", None)) == list:
self._run_task(
@ -213,7 +219,8 @@ class PlayBook(object):
task=task,
handlers=handlers,
host_list=task.get('run',[]),
conditional=True
conditional=True,
remote_user=user
)