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
31d4ee32
Commit
31d4ee32
authored
Apr 14, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Looping! With items! See examples/playbook/loop_with_items.yml for details
parent
579bd1d4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
1 deletions
+54
-1
examples/playbooks/loop_with_items.yml
+30
-0
lib/ansible/playbook.py
+24
-1
No files found.
examples/playbooks/loop_with_items.yml
0 → 100644
View file @
31d4ee32
---
# this is an example of how to run repeated task elements over lists
# of items, for example, installing multiple packages or configuring
# multiple users
-
hosts
:
all
user
:
root
tasks
:
-
name
:
install $item
action
:
yum pkg=$item state=installed
with_items
:
-
cobbler
-
httpd
-
name
:
configure user $item
action
:
user name=$item state=present groups=wheel
with_items
:
-
testuser1
-
testuser2
-
name
:
remove user $item
action
:
user name=$item state=absent
with_items
:
-
testuser1
-
testuser2
lib/ansible/playbook.py
View file @
31d4ee32
...
@@ -163,7 +163,30 @@ class PlayBook(object):
...
@@ -163,7 +163,30 @@ class PlayBook(object):
self
.
_include_tasks
(
play
,
task
,
dirname
,
new_tasks
)
self
.
_include_tasks
(
play
,
task
,
dirname
,
new_tasks
)
else
:
else
:
new_tasks
.
append
(
task
)
new_tasks
.
append
(
task
)
play
[
'tasks'
]
=
new_tasks
# now new_tasks contains a list of tasks, but tasks may contain
# lists of with_items to loop over. Do that.
# TODO: refactor into subfunction
new_tasks2
=
[]
for
task
in
new_tasks
:
if
'with_items'
in
task
:
for
item
in
task
[
'with_items'
]:
produced_task
=
{}
name
=
task
.
get
(
'name'
,
task
.
get
(
'action'
,
'unnamed task'
))
action
=
task
.
get
(
'action'
,
None
)
only_if
=
task
.
get
(
'only_if'
,
None
)
if
action
is
None
:
raise
errors
.
AnsibleError
(
'action is required'
)
produced_task
=
task
.
copy
()
produced_task
[
'action'
]
=
utils
.
template
(
action
,
dict
(
item
=
item
))
produced_task
[
'name'
]
=
utils
.
template
(
name
,
dict
(
item
=
item
))
if
only_if
:
produced_task
[
'only_if'
]
=
utils
.
template
(
only_if
,
dict
(
item
=
item
))
new_tasks2
.
append
(
produced_task
)
else
:
new_tasks2
.
append
(
task
)
play
[
'tasks'
]
=
new_tasks2
# process handlers as well as imported handlers
# process handlers as well as imported handlers
new_handlers
=
[]
new_handlers
=
[]
...
...
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