Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
13604e75
Commit
13604e75
authored
Feb 24, 2014
by
Richard Isaacson
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #6149 from risaacson/issues_4986
This tests clean with the new integration tests.
parents
e2a47162
8be8dbc9
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
162 additions
and
7 deletions
+162
-7
lib/ansible/runner/action_plugins/script.py
+58
-7
library/commands/script
+18
-0
test/integration/non_destructive.yml
+2
-0
test/integration/roles/test_script/files/create_afile.sh
+4
-0
test/integration/roles/test_script/files/remove_afile.sh
+4
-0
test/integration/roles/test_script/files/test.sh
+4
-0
test/integration/roles/test_script/meta/main.yml
+3
-0
test/integration/roles/test_script/tasks/main.yml
+69
-0
No files found.
lib/ansible/runner/action_plugins/script.py
View file @
13604e75
...
...
@@ -16,6 +16,7 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import
os
import
re
import
shlex
import
ansible.constants
as
C
...
...
@@ -24,8 +25,8 @@ from ansible import utils
from
ansible
import
errors
from
ansible.runner.return_data
import
ReturnData
class
ActionModule
(
object
):
class
ActionModule
(
object
):
TRANSFERS_FILES
=
True
def
__init__
(
self
,
runner
):
...
...
@@ -36,14 +37,64 @@ class ActionModule(object):
if
self
.
runner
.
noop_on_check
(
inject
):
# in check mode, always skip this module
return
ReturnData
(
conn
=
conn
,
comm_ok
=
True
,
result
=
dict
(
skipped
=
True
,
msg
=
'check mode not supported for this module'
))
return
ReturnData
(
conn
=
conn
,
comm_ok
=
True
,
result
=
dict
(
skipped
=
True
,
msg
=
'check mode not supported for this module'
))
# extract ansible reserved parameters
# From library/command keep in sync
creates
=
None
removes
=
None
r
=
re
.
compile
(
r'(^|\s)(creates|removes)=(?P<quote>[\'"])?(.*?)(?(quote)(?<!\\)(?P=quote))((?<!\\)(?=\s)|$)'
)
for
m
in
r
.
finditer
(
module_args
):
v
=
m
.
group
(
4
)
.
replace
(
"
\\
"
,
""
)
if
m
.
group
(
2
)
==
"creates"
:
creates
=
v
elif
m
.
group
(
2
)
==
"removes"
:
removes
=
v
module_args
=
r
.
sub
(
""
,
module_args
)
if
creates
:
# do not run the command if the line contains creates=filename
# and the filename already exists. This allows idempotence
# of command executions.
module_args_tmp
=
"path=
%
s"
%
creates
module_return
=
self
.
runner
.
_execute_module
(
conn
,
tmp
,
'stat'
,
module_args_tmp
,
inject
=
inject
,
complex_args
=
complex_args
,
persist_files
=
True
)
stat
=
module_return
.
result
.
get
(
'stat'
,
None
)
if
stat
and
stat
.
get
(
'exists'
,
False
):
return
ReturnData
(
conn
=
conn
,
comm_ok
=
True
,
result
=
dict
(
skipped
=
True
,
msg
=
(
"skipped, since
%
s exists"
%
creates
)
)
)
if
removes
:
# do not run the command if the line contains removes=filename
# and the filename does not exist. This allows idempotence
# of command executions.
module_args_tmp
=
"path=
%
s"
%
removes
module_return
=
self
.
runner
.
_execute_module
(
conn
,
tmp
,
'stat'
,
module_args_tmp
,
inject
=
inject
,
complex_args
=
complex_args
,
persist_files
=
True
)
stat
=
module_return
.
result
.
get
(
'stat'
,
None
)
if
stat
and
not
stat
.
get
(
'exists'
,
False
):
return
ReturnData
(
conn
=
conn
,
comm_ok
=
True
,
result
=
dict
(
skipped
=
True
,
msg
=
(
"skipped, since
%
s does not exist"
%
removes
)
)
)
# Decode the result of shlex.split() to UTF8 to get around a bug in that's been fixed in Python 2.7 but not Python 2.6.
# See: http://bugs.python.org/issue6988
tokens
=
shlex
.
split
(
module_args
.
encode
(
'utf8'
))
tokens
=
[
s
.
decode
(
'utf8'
)
for
s
in
tokens
]
# extract source script
source
=
tokens
[
0
]
# FIXME: error handling
args
=
" "
.
join
(
tokens
[
1
:])
source
=
template
.
template
(
self
.
runner
.
basedir
,
source
,
inject
)
...
...
@@ -53,18 +104,18 @@ class ActionModule(object):
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?
args
=
args
.
replace
(
'
\x00
'
,
''
)
# why does this happen here?
source
=
source
.
replace
(
'
\x00
'
,
''
)
# why does this happen here?
args
=
args
.
replace
(
'
\x00
'
,
''
)
# why does this happen here?
tmp_src
=
os
.
path
.
join
(
tmp
,
os
.
path
.
basename
(
source
))
tmp_src
=
tmp_src
.
replace
(
'
\x00
'
,
''
)
conn
.
put_file
(
source
,
tmp_src
)
sudoable
=
True
sudoable
=
True
# set file permissions, more permisive when the copy is done as a different user
if
self
.
runner
.
sudo
and
self
.
runner
.
sudo_user
!=
'root'
:
cmd_args_chmod
=
"chmod a+rx
%
s"
%
tmp_src
sudoable
=
False
sudoable
=
False
else
:
cmd_args_chmod
=
"chmod +rx
%
s"
%
tmp_src
self
.
runner
.
_low_level_exec_command
(
conn
,
cmd_args_chmod
,
tmp
,
sudoable
=
sudoable
)
...
...
library/commands/script
View file @
13604e75
...
...
@@ -18,6 +18,18 @@ options:
required: true
default: null
aliases: []
creates:
description:
- a filename, when it already exists, this step will B(not) be run.
required: no
default: null
version_added: "1.5"
removes:
description:
- a filename, when it does not exist, this step will B(not) be run.
required: no
default: null
version_added: "1.5"
notes:
- It is usually preferable to write Ansible modules than pushing scripts. Convert your script to an Ansible module for bonus points!
author: Michael DeHaan
...
...
@@ -26,4 +38,10 @@ author: Michael DeHaan
EXAMPLES = '''
# Example from Ansible Playbooks
- script: /some/local/script.sh --some-arguments 1234
# Run a script that creates a file, but only if the file is not yet created
- script: /some/local/create_file.sh --some-arguments 1234 creates=/the/created/file.txt
# Run a script that removes a file, but only if the file is not yet removed
- script: /some/local/remove_file.sh --some-arguments 1234 removes=/the/removed/file.txt
'''
test/integration/non_destructive.yml
View file @
13604e75
...
...
@@ -34,3 +34,4 @@
-
{
role
:
test_iterators
,
tags
:
test_iterators
}
-
{
role
:
test_command_shell
,
tags
:
test_command_shell
}
-
{
role
:
test_failed_when
,
tags
:
test_failed_when
}
-
{
role
:
test_script
,
tags
:
test_script
}
\ No newline at end of file
test/integration/roles/test_script/files/create_afile.sh
0 → 100755
View file @
13604e75
#!/usr/bin/env bash
echo
"win"
>
"
$1
"
\ No newline at end of file
test/integration/roles/test_script/files/remove_afile.sh
0 → 100755
View file @
13604e75
#!/usr/bin/env bash
rm
"
$1
"
\ No newline at end of file
test/integration/roles/test_script/files/test.sh
0 → 100755
View file @
13604e75
#!/usr/bin/env bash
echo
-n
"win"
\ No newline at end of file
test/integration/roles/test_script/meta/main.yml
0 → 100644
View file @
13604e75
dependencies
:
-
prepare_tests
test/integration/roles/test_script/tasks/main.yml
0 → 100644
View file @
13604e75
# Test code for the script module and action_plugin.
# (c) 2014, Richard Isaacson <richard.c.isaacson@gmail.com>
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
##
## prep
##
-
set_fact
:
output_dir_test={{output_dir}}/test_script
-
name
:
make sure our testing sub-directory does not exist
file
:
path="{{ output_dir_test }}" state=absent
-
name
:
create our testing sub-directory
file
:
path="{{ output_dir_test }}" state=directory
##
## script
##
-
name
:
execute the test.sh script via command
script
:
test.sh
register
:
script_result0
-
name
:
assert that the script executed correctly
assert
:
that
:
-
"
script_result0.rc
==
0"
-
"
script_result0.stderr
==
''"
-
"
script_result0.stdout
==
'win'"
# creates
-
name
:
verify that afile.txt is absent
file
:
path={{output_dir_test}}/afile.txt state=absent
-
name
:
create afile.txt with create_afile.sh via command
script
:
create_afile.sh {{output_dir_test | expanduser}}/afile.txt creates={{output_dir_test | expanduser}}/afile.txt
-
name
:
verify that afile.txt is present
file
:
path={{output_dir_test}}/afile.txt state=file
# removes
-
name
:
remove afile.txt with remote_afile.sh via command
script
:
remove_afile.sh {{output_dir_test | expanduser}}/afile.txt removes={{output_dir_test | expanduser}}/afile.txt
-
name
:
verify that afile.txt is absent
file
:
path={{output_dir_test}}/afile.txt state=absent
register
:
script_result1
-
name
:
assert that the file was removed by the script
assert
:
that
:
-
"
script_result1.changed
!=
True"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment