From b62b2de48620549c74af71e4e50aa654d4fa9322 Mon Sep 17 00:00:00 2001 From: Antonio Messina Date: Sat, 6 Apr 2013 10:46:09 +0200 Subject: [PATCH] Add example playbooks --- examples/drbd.yml | 47 +++++++++++++++++++++++++++++++++++++++++ examples/nestedvars.yml | 45 +++++++++++++++++++++++++++++++++++++++ examples/variables.j2 | 44 ++++++++++++++++++++++++++++++++++++++ examples/variables.yml | 20 ++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 examples/drbd.yml create mode 100644 examples/nestedvars.yml create mode 100644 examples/variables.j2 create mode 100644 examples/variables.yml diff --git a/examples/drbd.yml b/examples/drbd.yml new file mode 100644 index 0000000..d7fe7fe --- /dev/null +++ b/examples/drbd.yml @@ -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; +# } +# } diff --git a/examples/nestedvars.yml b/examples/nestedvars.yml new file mode 100644 index 0000000..4f367a9 --- /dev/null +++ b/examples/nestedvars.yml @@ -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 diff --git a/examples/variables.j2 b/examples/variables.j2 new file mode 100644 index 0000000..7b8d08a --- /dev/null +++ b/examples/variables.j2 @@ -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 %} + diff --git a/examples/variables.yml b/examples/variables.yml new file mode 100644 index 0000000..63ab183 --- /dev/null +++ b/examples/variables.yml @@ -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