From f308194b9ab3320194f9d59394952a37b6215d6a Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sat, 6 Apr 2013 12:35:35 -0400 Subject: [PATCH] Added examples of how roles work! --- examples/playbooks/roles/foo/files/foo.txt | 2 + .../playbooks/roles/foo/handlers/main.yml | 6 +++ examples/playbooks/roles/foo/main.yml | 8 ++++ examples/playbooks/roles/foo/tasks/main.yml | 9 +++++ examples/playbooks/roles/foo/templates/foo.j2 | 1 + examples/playbooks/roles/foo/vars/main.yml | 3 ++ examples/playbooks/roletest.yml | 39 +++++++++++++++++++ lib/ansible/playbook/play.py | 6 ++- 8 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 examples/playbooks/roles/foo/files/foo.txt create mode 100644 examples/playbooks/roles/foo/handlers/main.yml create mode 100644 examples/playbooks/roles/foo/main.yml create mode 100644 examples/playbooks/roles/foo/tasks/main.yml create mode 100644 examples/playbooks/roles/foo/templates/foo.j2 create mode 100644 examples/playbooks/roles/foo/vars/main.yml create mode 100644 examples/playbooks/roletest.yml diff --git a/examples/playbooks/roles/foo/files/foo.txt b/examples/playbooks/roles/foo/files/foo.txt new file mode 100644 index 00000000000..7b6f19a00c0 --- /dev/null +++ b/examples/playbooks/roles/foo/files/foo.txt @@ -0,0 +1,2 @@ +This is a file + diff --git a/examples/playbooks/roles/foo/handlers/main.yml b/examples/playbooks/roles/foo/handlers/main.yml new file mode 100644 index 00000000000..d41caad6f97 --- /dev/null +++ b/examples/playbooks/roles/foo/handlers/main.yml @@ -0,0 +1,6 @@ +--- + +- name: blippy + shell: echo notifier called, and the value of x is '{{ x }}' + + diff --git a/examples/playbooks/roles/foo/main.yml b/examples/playbooks/roles/foo/main.yml new file mode 100644 index 00000000000..d763d16cfdb --- /dev/null +++ b/examples/playbooks/roles/foo/main.yml @@ -0,0 +1,8 @@ +--- + +- name: template operation + template: src=foo.j2 dest=/etc/motd + +- action: shell echo 'hi webserver' + notify: + - blippy diff --git a/examples/playbooks/roles/foo/tasks/main.yml b/examples/playbooks/roles/foo/tasks/main.yml new file mode 100644 index 00000000000..13a143dc9fd --- /dev/null +++ b/examples/playbooks/roles/foo/tasks/main.yml @@ -0,0 +1,9 @@ +--- + +- name: copy operation + copy: src=foo.txt dest=/tmp/roles_test1.txt + +- name: template operation + template: src=foo.j2 dest=/tmp/roles_test2.txt + notify: + - blippy diff --git a/examples/playbooks/roles/foo/templates/foo.j2 b/examples/playbooks/roles/foo/templates/foo.j2 new file mode 100644 index 00000000000..172015653fa --- /dev/null +++ b/examples/playbooks/roles/foo/templates/foo.j2 @@ -0,0 +1 @@ +I am a {{ ansible_os_family }} distribution. diff --git a/examples/playbooks/roles/foo/vars/main.yml b/examples/playbooks/roles/foo/vars/main.yml new file mode 100644 index 00000000000..68576ba28f0 --- /dev/null +++ b/examples/playbooks/roles/foo/vars/main.yml @@ -0,0 +1,3 @@ +--- +x: '{{ ansible_machine }}' + diff --git a/examples/playbooks/roletest.yml b/examples/playbooks/roletest.yml new file mode 100644 index 00000000000..8aa934588f5 --- /dev/null +++ b/examples/playbooks/roletest.yml @@ -0,0 +1,39 @@ +# in Ansible 1.2 and later, roles allow easy best-practices organization of content +# and maximize shareability of ansible building blocks. +# +# suppose a playbook applied to a group of hosts includes two roles, foo and bar. +# +# what do roles do in this case? +# +# listing the roles as foo and bar will auto include the following: +# +# tasks from ./roles/foo/tasks/main.yml, then ./roles/bar/tasks/main.yml +# handlers from ./roles/foo/handlers/main.yml, then ./roles/bar/handlers/main.yml +# vars from ./roles/foo/vars/main.yml, then ./roles/bar/vars/main.yml +# +# should any of these files not exist, that is ok, and they will simply not be loaded. +# +# should the task file in foo/tasks/main.yml want to include subtasks in other files, that +# is also permitted. +# +# templates and copy operations also get smarter about where to look for content when using +# roles. +# +# as an example, a task in foo/tasks/main.yml could copy or template a file by +# referencing a "src=foo.j2" rather than having to explicitly path src=roles/foo/templates/foo.j2. + +--- + + - hosts: all + roles: + - foo + + # add as many roles as you like, roles takes a list of roles names + # these paths can be qualified, but if bare, it will look from them in + # roles/$rolename relative to the playbook + # + # - bar + + # explicit tasks and handlers can be used, but are not required. + # they will run after the roles if present. + diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index d0a3e9138ae..7c5a7462555 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -127,11 +127,13 @@ class Play(object): new_vars_files = [] for orig_path in roles: path = utils.path_dwim(self.basedir, orig_path) - if not os.path.isdir(path): - path2 = utils.path_dwim(self.basedir, os.path.join(self.basedir, 'roles', orig_path)) + if not os.path.isdir(path) and not orig_path.startswith(".") and not orig_path.startswith("/"): + path2 = utils.path_dwim(self.basedir, os.path.join('roles', orig_path)) if not os.path.isdir(path2): raise errors.AnsibleError("cannot find role in %s or %s" % (path, path2)) path = path2 + elif not os.path.isdir(path): + raise errors.AnsibleError("cannot find role in %s" % (path)) task = utils.path_dwim(self.basedir, os.path.join(path, 'tasks', 'main.yml')) handler = utils.path_dwim(self.basedir, os.path.join(path, 'handlers', 'main.yml')) vars_file = utils.path_dwim(self.basedir, os.path.join(path, 'vars', 'main.yml'))