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
21258dcc
Commit
21258dcc
authored
Oct 27, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added 'when' as a shortcut around only_if.
parent
a768e9a9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
143 additions
and
31 deletions
+143
-31
examples/playbooks/conditionals_part2.yml
+33
-30
examples/playbooks/conditionals_part3.yml
+47
-0
lib/ansible/playbook/task.py
+63
-1
No files found.
examples/playbooks/conditionals_part2.yml
View file @
21258dcc
---
---
# this is a demo of conditional executions using '
only_if'
, which can skip
# this is a demo of conditional executions using '
when' statements
, which can skip
# certain tasks on machines/platforms/etc where they do not apply.
# certain tasks on machines/platforms/etc where they do not apply.
-
hosts
:
all
-
hosts
:
all
user
:
root
user
:
root
vars
:
vars
:
favcolor
:
"
red"
favcolor
:
"
red"
dog
:
"
fido"
cat
:
"
whiskers"
ssn
:
8675309
ssn
:
8675309
# Below we're going to define some expressions.
# These are the types of when statemnets available
# when_set: $variable_name
# when_unset: $variable_name
# when_str: $x == "test"
# when_int: $y > 2
# when_float: $z => 2.3
#
#
# Not only can we assign variables for reuse, but we can also assign conditional
# when using 'when', take care to make sure any variables given are surrounded by spaces
# expressions. By keeping these in 'vars', the task section remains
# as an example, $z>3 will not do what you want, use "$z > 3"
# extraordinarily clean, and not littered with programming language
# constructs -- so it's easily skimmed by humans.
#
# Remember to quote any variables if they are not numbers!
#
# Interesting fact: aside from the $variables, these expressions are actually
# tiny bits of Python. They are evaluated in the context of each host, so different
# steps can be skipped on different hosts! They should evaluate to either True
# or False
is_favcolor_blue
:
"
'$favcolor'
==
'blue'"
is_centos
:
"
'$facter_operatingsystem'
==
'CentOS'"
# NOTE:
#
# facter and ohai variables can be used in only_if statements too
# ex: "'$facter_operatingsystem' == 'CentOS'", which bubble up automatically
# from the managed machines
#
# this example won't do that though, as you might not have facter or ohai,
# but you get the idea...
tasks
:
tasks
:
-
name
:
"
do
this
if
my
favcolor
is
blue"
-
name
:
"
do
this
if
my
favcolor
is
blue
,
and
my
dog
is
named
fido
"
action
:
shell /bin/false
action
:
shell /bin/false
only_if
:
'
$is_favcolor_blue'
when_string
:
$favcolor == 'blue' and $dog == 'fido'
-
name
:
"
do
this
if
my
favcolor
is
not
blue,
and
my
dog
is
named
fido"
action
:
shell /bin/true
when_string
:
$favcolor != 'blue' and $dog == 'fido'
-
name
:
"
do
this
if
my
SSN
is
over
9000"
action
:
shell /bin/true
when_integer
:
$ssn > 9000
-
name
:
"
do
this
if
my
favcolor
is
not
blue"
-
name
:
"
do
this
if
I
have
one
of
these
SSNs"
action
:
shell /bin/true
when_integer
:
$ssn in [ 8675309, 8675310, 8675311 ]
-
name
:
"
do
this
if
a
variable
named
hippo
is
NOT
defined"
action
:
shell /bin/true
action
:
shell /bin/true
only_if
:
'
not
($is_favcolor_blue)'
when_unset
:
$hippo
-
name
:
"
do
this
if
a
variable
named
hippo
is
defined"
action
:
shell /bin/true
when_set
:
$hippo
examples/playbooks/conditionals_part3.yml
0 → 100644
View file @
21258dcc
---
# this is a demo of conditional executions using 'only_if', which can skip
# certain tasks on machines/platforms/etc where they do not apply. This is
# the more 'raw' version of the 'when' statement, most users will be able to
# use 'when' directly. 'only_if' is an older feature, and useful for when
# you need more advanced expression control.
-
hosts
:
all
user
:
root
vars
:
favcolor
:
"
red"
ssn
:
8675309
# Below we're going to define some expressions.
#
# Not only can we assign variables for reuse, but we can also assign conditional
# expressions. By keeping these in 'vars', the task section remains
# extraordinarily clean, and not littered with programming language
# constructs -- so it's easily skimmed by humans.
#
# Remember to quote any variables if they are not numbers!
#
# Interesting fact: aside from the $variables, these expressions are actually
# tiny bits of Python. They are evaluated in the context of each host, so different
# steps can be skipped on different hosts! They should evaluate to either True
# or False
is_favcolor_blue
:
"
'$favcolor'
==
'blue'"
is_centos
:
"
'$facter_operatingsystem'
==
'CentOS'"
# NOTE:
#
# setup module values, facter and ohai variables can be used in only_if statements too
# ex: "'$facter_operatingsystem' == 'CentOS'", which bubble up automatically
# from the managed machines. This example doesn't do that though.
tasks
:
-
name
:
"
do
this
if
my
favcolor
is
blue"
action
:
shell /bin/false
only_if
:
'
$is_favcolor_blue'
-
name
:
"
do
this
if
my
favcolor
is
not
blue"
action
:
shell /bin/true
only_if
:
'
not
($is_favcolor_blue)'
lib/ansible/playbook/task.py
View file @
21258dcc
...
@@ -22,7 +22,7 @@ from ansible import utils
...
@@ -22,7 +22,7 @@ from ansible import utils
class
Task
(
object
):
class
Task
(
object
):
__slots__
=
[
__slots__
=
[
'name'
,
'action'
,
'only_if'
,
'async_seconds'
,
'async_poll_interval'
,
'name'
,
'action'
,
'only_if'
,
'
when'
,
'
async_seconds'
,
'async_poll_interval'
,
'notify'
,
'module_name'
,
'module_args'
,
'module_vars'
,
'notify'
,
'module_name'
,
'module_args'
,
'module_vars'
,
'play'
,
'notified_by'
,
'tags'
,
'register'
,
'play'
,
'notified_by'
,
'tags'
,
'register'
,
'delegate_to'
,
'first_available_file'
,
'ignore_errors'
,
'delegate_to'
,
'first_available_file'
,
'ignore_errors'
,
...
@@ -58,6 +58,11 @@ class Task(object):
...
@@ -58,6 +58,11 @@ class Task(object):
else
:
else
:
raise
errors
.
AnsibleError
(
"cannot find lookup plugin named
%
s for usage in with_
%
s"
%
(
plugin_name
,
plugin_name
))
raise
errors
.
AnsibleError
(
"cannot find lookup plugin named
%
s for usage in with_
%
s"
%
(
plugin_name
,
plugin_name
))
elif
x
.
startswith
(
"when_"
):
when_name
=
x
.
replace
(
"when_"
,
""
)
ds
[
'when'
]
=
"
%
s
%
s"
%
(
when_name
,
ds
[
x
])
ds
.
pop
(
x
)
elif
not
x
in
Task
.
VALID_KEYS
:
elif
not
x
in
Task
.
VALID_KEYS
:
raise
errors
.
AnsibleError
(
"
%
s is not a legal parameter in an Ansible task or handler"
%
x
)
raise
errors
.
AnsibleError
(
"
%
s is not a legal parameter in an Ansible task or handler"
%
x
)
...
@@ -109,6 +114,8 @@ class Task(object):
...
@@ -109,6 +114,8 @@ class Task(object):
# load various attributes
# load various attributes
self
.
only_if
=
ds
.
get
(
'only_if'
,
'True'
)
self
.
only_if
=
ds
.
get
(
'only_if'
,
'True'
)
self
.
when
=
ds
.
get
(
'when'
,
None
)
self
.
async_seconds
=
int
(
ds
.
get
(
'async'
,
0
))
# not async by default
self
.
async_seconds
=
int
(
ds
.
get
(
'async'
,
0
))
# not async by default
self
.
async_poll_interval
=
int
(
ds
.
get
(
'poll'
,
10
))
# default poll = 10 seconds
self
.
async_poll_interval
=
int
(
ds
.
get
(
'poll'
,
10
))
# default poll = 10 seconds
self
.
notify
=
ds
.
get
(
'notify'
,
[])
self
.
notify
=
ds
.
get
(
'notify'
,
[])
...
@@ -169,3 +176,58 @@ class Task(object):
...
@@ -169,3 +176,58 @@ class Task(object):
self
.
tags
.
extend
(
apply_tags
)
self
.
tags
.
extend
(
apply_tags
)
self
.
tags
.
extend
(
import_tags
)
self
.
tags
.
extend
(
import_tags
)
if
self
.
when
is
not
None
:
if
self
.
only_if
!=
'True'
:
raise
errors
.
AnsibleError
(
'when obsoletes only_if, only use one or the other'
)
self
.
only_if
=
self
.
compile_when_to_only_if
(
self
.
when
)
def
compile_when_to_only_if
(
self
,
expression
):
'''
when is a shorthand for writing only_if conditionals. It requires less quoting
magic. only_if is retained for backwards compatibility.
'''
# when: set $variable
# when: unset $variable
# when: int $x >= $z and $y < 3
# when: int $x in $alist
# when: float $x > 2 and $y <= $z
# when: str $x != $y
if
type
(
expression
)
not
in
[
str
,
unicode
]:
raise
errors
.
AnsibleError
(
"invalid usage of when_ operator:
%
s"
%
expression
)
tokens
=
expression
.
split
()
if
len
(
tokens
)
<
2
:
raise
errors
.
AnsibleError
(
"invalid usage of when_ operator:
%
s"
%
expression
)
# when_set / when_unset
if
tokens
[
0
]
in
[
'set'
,
'unset'
]:
if
len
(
tokens
)
!=
2
:
raise
errors
.
AnsibleError
(
"usage: when: <set|unset> <$variableName>"
)
return
"is_
%
s('
%
s')"
%
(
tokens
[
0
],
tokens
[
1
])
# when_integer / when_float / when_string
elif
tokens
[
0
]
in
[
'integer'
,
'float'
,
'string'
]:
cast
=
None
if
tokens
[
0
]
==
'integer'
:
cast
=
'int'
elif
tokens
[
0
]
==
'string'
:
cast
=
'str'
elif
tokens
[
0
]
==
'float'
:
cast
=
'float'
tcopy
=
tokens
[
1
:]
for
(
i
,
t
)
in
enumerate
(
tokens
[
1
:]):
if
t
.
find
(
"$"
)
!=
-
1
:
# final variable substitution will happen in Runner code
tcopy
[
i
]
=
"
%
s('
%
s')"
%
(
cast
,
t
)
else
:
tcopy
[
i
]
=
t
return
" "
.
join
(
tcopy
)
else
:
raise
errors
.
AnsibleError
(
"invalid usage of when_ operator:
%
s"
%
expression
)
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