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
3422ae67
Commit
3422ae67
authored
Feb 23, 2013
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document how to do task includes using with_items and externalized data.
parent
9a04c7ea
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
1 deletions
+59
-1
docsite/rst/playbooks.rst
+3
-1
docsite/rst/playbooks2.rst
+56
-0
No files found.
docsite/rst/playbooks.rst
View file @
3422ae67
...
...
@@ -295,7 +295,7 @@ won't need them for much else.
Notify
handlers
are
always
run
in
the
order
written
.
Include
Files
And
Encouraging
Reuse
Task
Include
Files
And
Encouraging
Reuse
```````````````````````````````````
Suppose
you
want
to
reuse
lists
of
tasks
between
plays
or
playbooks
.
You
can
use
...
...
@@ -347,6 +347,8 @@ which also supports structured variables::
- beta
- gamma
Playbooks can include other playbooks too, but that'
s
mentioned
in
a
later
section
.
..
note
::
As
of
1.0
,
task
include
statements
can
be
used
at
arbitrary
depth
.
They
were
previously
limited
to
a
single
level
,
so
task
includes
...
...
docsite/rst/playbooks2.rst
View file @
3422ae67
...
...
@@ -946,6 +946,62 @@ feature produces a large amount of output, it is best used when checking a singl
ansible-playbook foo.yml --check --diff --limit foo.example.com
Advanced Task Includes
``````````````````````
In above sections we talked about task includes, and how to do loops using with_items. If we wish
to externalize data from the playbook rules itself, this is possible by combining some concepts.
This is not something everyone may need to do at first, but it's a clever trick and deserves explanation.
Here is a top level example playbook that loads variables from an external file and also tasks from an
external file. You will note that we use a list (using with_items) as a parameter on the include
statement.
----
# file: playbook-demo.yml
hosts: all
vars_files:
- config/users.yml
tasks:
- include: tasks/user.yml user=$item
with_items: $users
We've defined our user definitions in an external file. This allows us to reference that list of users in
multiple playbooks. The users list also defines users as a list of hashes, rather than just the usernames.
We are also loading the SSH public keys for those users from the filesystem, though we could choose to embed
them in the file instead. It's up to you::
----
# file: config/users.yml
users:
- name: alice
password: cryptedPasswordHere
sshkey: $FILE(/home/alice/id_rsa.pub)
- name: bob
password: cryptedPasswordHere
sshkey: $FILE(/home/bob/id_rsa.pub)
Now that we have these two things together, we can write a task include file the playbook can use that sets
up *all* of the users, rather than mentioning each user by name, or going to lots of trouble to correlate
the user names with the SSH keys, and so on::
---
# file: tasks/user.yml
- name: ensure user ${user.username} exists
action: user state=present name=${user.username} password=${user.password}
- name: install authorized keys for ${user.username}
action: authorized_key state=present user=${user.username} key="${user.sshkey}"
If you can follow this example, you've done pretty well! It combines most of the language features
of example all together. As you can see, there are lots of different ways to load data from
sources, and to organize things. Ansible does not really make you pick one or the other, so choose
an approach that works best for you.
Style Points
````````````
...
...
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