Skip to content

Commit

Permalink
Add example playbooks
Browse files Browse the repository at this point in the history
  • Loading branch information
arcimboldo committed Apr 6, 2013
1 parent 5e09af7 commit b62b2de
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
47 changes: 47 additions & 0 deletions examples/drbd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
################
# DRBD example #
################
#
# This is a module created by us, and present in the `modules`
# directory. This is just an example on how to use it. However, the
# module is documented in the comments at the beginning of the file.
#
# This is a minimal example:

- hosts: all
tasks:
- action: drbd name=resourcename disk=/dev/drbd1
device=/dev/sdb
address0=1.1.1.1:7789 address1=1.1.1.2:7789

# while this will define also the optional variables. Note that the
# two modules produce the same results, considering the value I've put
# in this second example in order to show you the default behavior.

- hosts: all
tasks:
- action: drbd name=resourcename dest=/etc/drbd.d/resourcename.res
disk0=/dev/drdb1 disk1=/dev/drdb1
device0=/dev/sdb device1=/dev/sdb
address0=1.1.1.1:7789 address1=1.1.1.2:7789
peer0=1.1.1.1:7789 peer1=1.1.1.2:7789
metadisk0=internal metadisk1=internal

# The output produced should be found in /etc/drbd.d/resousrcename.res
# and should looks like:
#
# resource resourcename {
# on 1.1.1.1:7789 {
# address 1.1.1.1:7789;
# device /dev/sdb;
# disk /dev/drdb1;
# meta-disk internal;
# }
# on 1.1.1.2:7789 {
# address 1.1.1.2:7789;
# device /dev/sdb;
# disk /dev/drdb1;
# meta-disk internal;
# }
# }
45 changes: 45 additions & 0 deletions examples/nestedvars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
#######################
# Nested VARS example #
#######################
#
# This example is used to explain how you can call nested variables
# (like, an item of a dictionary) using ansible prior v1.2. It may be
# a bit tricky because of the way ansible does the expansion of th
# evariables...
#
# Bascially, to expand `hostvars[item]` where `hostvars` is a
# dictionary-like object and `item` is a variable, you have to do:
#
# ${hostvars.{$item}}
#
# Note that the expansion of the dictionary is done with `${...}`
# while the expansion of the attribute of the dictionary is done with
# `{$...}`.
#
# The following example will define a dictionary `hosts`:
#
# hosts = { 'a': {'b': 'c'}}
#
# and try to print `hosts['a']['b']`
#
- hosts: all
vars:
hosts:
a:
b: c
x: a
y: b
tasks:
- debug: msg="${hosts.{$x}.{$y}}"

# The original use case I used for this was taken from this task, in
# which I wanted to produce an ``/etc/hosts`` file starting from the
# data sotred in the inventory, adding or updating the IP addresses of
# all the known hosts.
#
# - hosts: all
# connection: local
# tasks:
# - lineinfile: dest=/tmp/hostfile regexp=".* $item .*" line="${hostvars.{$item}.ansible_default_ipv4.address} $item "
# with_items: $hostvars
44 changes: 44 additions & 0 deletions examples/variables.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
`groups`
========
{% for key, value in groups.items() %}
{{ key }} : {{ value }}
{% endfor %}

groups:
{% for group in group_names %}
{{ group }}
{% endfor %}

`hostvars`
==========

{% for key, value in hostvars.items() -%}

{{ key }}:
{% if value is mapping %}
{%- for key2, value2 in value.items() %}

{{ key2 }}:
{% if value2 is string %}
'{{ value2 }}'
{% elif value2 is mapping %}
{%- for key3, value3 in value2.items() %}

{{ key3 }}:
{% if value3 is string %}
'{{ value3 }}'
{% else %}
{{ value3 }}
{% endif %}
{% endfor %}
{% else %}
{{ value2 }}
{% endif %}
{% endfor %}
{% elif value is string %}
'{{ value }}'
{% else %}
{{ value }}
{% endif %}
{% endfor %}

20 changes: 20 additions & 0 deletions examples/variables.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
###########################
# Storing facts to a file #
###########################
#
# This will store some information in `variables.txt`
#
# In most cases you may want to use instead:
#
# ansible -m setup -t ./variables.d all
#
# which will store in directory `variables.d` all facts gathered by
# the `setup` module.
#
# However, the template system will also get facts or variables set by
# modules executed before in the playbook.

- hosts: all
tasks:
- template: src=variables.j2 dest=./variables.txt

0 comments on commit b62b2de

Please sign in to comment.