From 4c9ebe8522fd8ccd33f9d777a7659f6029ce7da2 Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Fri, 24 May 2013 22:10:46 +0200 Subject: [PATCH 1/3] Add roles support for the script module allows to put scripts directly in a dir within the role: roles//scripts/.. Same as the copy and template module. As requested in and closes #2969 --- lib/ansible/runner/action_plugins/script.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/ansible/runner/action_plugins/script.py b/lib/ansible/runner/action_plugins/script.py index b86d9c3f1d1..8794c343c50 100644 --- a/lib/ansible/runner/action_plugins/script.py +++ b/lib/ansible/runner/action_plugins/script.py @@ -41,7 +41,10 @@ class ActionModule(object): # FIXME: error handling args = " ".join(tokens[1:]) source = template.template(self.runner.basedir, source, inject) - source = utils.path_dwim(self.runner.basedir, source) + if '_original_file' in inject: + source = utils.path_dwim_relative(inject['_original_file'], 'scripts', source, self.runner.basedir) + else: + source = utils.path_dwim(self.runner.basedir, source) # transfer the file to a remote tmp location source = source.replace('\x00','') # why does this happen here? From 0224dc464dcc8808652d618fd0138ae39bdce3d6 Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Fri, 24 May 2013 22:42:24 +0200 Subject: [PATCH 2/3] roles/x/scripts support: update docs also added example on where regular tasks fit in --- docsite/latest/rst/bestpractices.rst | 2 ++ docsite/latest/rst/playbooks.rst | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/docsite/latest/rst/bestpractices.rst b/docsite/latest/rst/bestpractices.rst index 1a1d2ea137f..ac66c3ebcd4 100644 --- a/docsite/latest/rst/bestpractices.rst +++ b/docsite/latest/rst/bestpractices.rst @@ -48,6 +48,8 @@ The top level of the directory would contain files and directories like so:: main.yml # <-- tasks file can include smaller files if warranted handlers/ # main.yml # <-- handlers file + scripts/ # + foo.sh # <-- script files for use with the script resource templates/ # <-- files for use with the template resource ntp.conf.j2 # <------- templates end in .j2 files/ # diff --git a/docsite/latest/rst/playbooks.rst b/docsite/latest/rst/playbooks.rst index 451950ad30e..ef63349a674 100644 --- a/docsite/latest/rst/playbooks.rst +++ b/docsite/latest/rst/playbooks.rst @@ -463,12 +463,14 @@ Example project structure:: roles/ common/ files/ + scripts/ templates/ tasks/ handlers/ vars/ webservers/ files/ + scripts/ templates/ tasks/ handlers/ @@ -488,6 +490,7 @@ This designates the following behaviors, for each role 'x': - If roles/x/handlers/main.yml exists, handlers listed therein will be added to the play - If roles/x/vars/main.yml exists, variables listed therein will be added to the play - Any copy tasks can reference files in roles/x/files/ without having to path them relatively or absolutely +- Any script tasks can reference scripts in roles/x/sripts/ without having to path them relatively or absolutely - Any template tasks can reference files in roles/x/templates/ without having to path them relatively or absolutely If any files are not present, they are just ignored. So it's ok to not have a 'vars/' subdirectory for the role, @@ -526,6 +529,8 @@ If you want to define certain tasks to happen before AND after roles are applied - shell: echo 'hello' roles: - { role: some_role } + tasks: + - shell: echo 'still busy' post_tasks: - shell: echo 'goodbye' From 5859af7285e425113b124c58def6939f3911cb4b Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Sat, 25 May 2013 16:51:59 +0200 Subject: [PATCH 3/3] script support for roles: use the files/ directory instead of an additional scripts/ directory --- docsite/latest/rst/bestpractices.rst | 3 +-- docsite/latest/rst/playbooks.rst | 4 +--- lib/ansible/runner/action_plugins/script.py | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/docsite/latest/rst/bestpractices.rst b/docsite/latest/rst/bestpractices.rst index ac66c3ebcd4..c0afa4488fd 100644 --- a/docsite/latest/rst/bestpractices.rst +++ b/docsite/latest/rst/bestpractices.rst @@ -48,12 +48,11 @@ The top level of the directory would contain files and directories like so:: main.yml # <-- tasks file can include smaller files if warranted handlers/ # main.yml # <-- handlers file - scripts/ # - foo.sh # <-- script files for use with the script resource templates/ # <-- files for use with the template resource ntp.conf.j2 # <------- templates end in .j2 files/ # bar.txt # <-- files for use with the copy resource + foo.sh # <-- script files for use with the script resource webtier/ # same kind of structure as "common" was above, done for the webtier role monitoring/ # "" diff --git a/docsite/latest/rst/playbooks.rst b/docsite/latest/rst/playbooks.rst index ef63349a674..cb403b1ee39 100644 --- a/docsite/latest/rst/playbooks.rst +++ b/docsite/latest/rst/playbooks.rst @@ -463,14 +463,12 @@ Example project structure:: roles/ common/ files/ - scripts/ templates/ tasks/ handlers/ vars/ webservers/ files/ - scripts/ templates/ tasks/ handlers/ @@ -490,7 +488,7 @@ This designates the following behaviors, for each role 'x': - If roles/x/handlers/main.yml exists, handlers listed therein will be added to the play - If roles/x/vars/main.yml exists, variables listed therein will be added to the play - Any copy tasks can reference files in roles/x/files/ without having to path them relatively or absolutely -- Any script tasks can reference scripts in roles/x/sripts/ without having to path them relatively or absolutely +- Any script tasks can reference scripts in roles/x/files/ without having to path them relatively or absolutely - Any template tasks can reference files in roles/x/templates/ without having to path them relatively or absolutely If any files are not present, they are just ignored. So it's ok to not have a 'vars/' subdirectory for the role, diff --git a/lib/ansible/runner/action_plugins/script.py b/lib/ansible/runner/action_plugins/script.py index 8794c343c50..84c0ab015bf 100644 --- a/lib/ansible/runner/action_plugins/script.py +++ b/lib/ansible/runner/action_plugins/script.py @@ -42,7 +42,7 @@ class ActionModule(object): args = " ".join(tokens[1:]) source = template.template(self.runner.basedir, source, inject) if '_original_file' in inject: - source = utils.path_dwim_relative(inject['_original_file'], 'scripts', source, self.runner.basedir) + source = utils.path_dwim_relative(inject['_original_file'], 'files', source, self.runner.basedir) else: source = utils.path_dwim(self.runner.basedir, source)