# Pedantically commented playbook example

**URL:** <https://forum.ansible.com/t/pedantically-commented-playbook-example/11607>\
**Category:** Ansible Project\
**Created:** [June 8, 2012, 8:20pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607 "2012-06-08T20:20:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred\_Alger](https://avatars.discourse-cdn.com/v4/letter/f/c89c15/32.png) [@Fred\_Alger](https://forum.ansible.com/u/Fred_Alger)\
**Post date:** [June 8, 2012, 8:20pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/1 "2012-06-08T20:20:44Z")

</div>

I’ve been working on describing all of the options & useful combinations possible in a playbook by writing one LONG super-commented playbook. This is about 60% done, but I’d love feedback and comments. Is this useful to you? Anything else you’d like to see?

[https://gist.github.com/2897937](https://gist.github.com/2897937)

Thanks!!

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [June 9, 2012, 6:42am UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/2 "2012-06-09T06:42:33Z")

</div>

Fred,

> Is this useful to you?

this is useful for beginners like myself; thank you.

> Anything else you'd like to see?

The example states that vars cannot be expanded inside the vars  
section ($config); how about an example with YAML &something and  
\*something ? [I forget their official term for that...]

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [June 9, 2012, 2:23pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/3 "2012-06-09T14:23:07Z")

</div>

I’ve heard them called “anchors” – I’m not sure they are usable for substrings.

Making vars used later on in the vars list is something a small patch could achieve, we’d just have to repeatedly template each line with the results of the vars storage.

Not ideal, but doable.

–Michael

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [June 9, 2012, 4:41pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/4 "2012-06-09T16:41:17Z")

</div>

> \> The example states that vars cannot be expanded inside the vars  
> \> section ($config); how about an example with YAML &something and  
> \> \*something ? [I forget their official term for that...]
> 
> I've heard them called "anchors" -- I'm not sure they are usable for  
> substrings.

Indeed; I hadn't thought of that.

> Making vars used later on in the vars list is something a small patch  
> could achieve, we'd just have to repeatedly template each line with  
> the results of the vars storage.
> 
> Not ideal, but doable.

Useful!

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [June 9, 2012, 5:03pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/5 "2012-06-09T17:03:02Z")

</div>

Fred Alger wrote:

> I've been working on describing all of the options & useful combinations possible in a  
> playbook by writing one LONG super-commented playbook. This is about 60% done, but I'd love  
> feedback and comments. Is this useful to you? Anything else you'd like to see?
> 
> [https://gist.github.com/2897937](https://gist.github.com/2897937)

I think this is a great idea, it definitely would have helped me get started  
a lot faster.

A few comments though. Line 36 starts a play, not a playbook. You can have  
multiple plays in a playbook, that is the strength of the playbook in my  
opinion. The play can also be named, by default the name is whatever the  
hosts field is set to.

I think line 112 saying that variables in variables won't be fixed is  
incorrect, my understanding is that it is something that will be fixed.

I'm confused by what the comment says for line 120. The variable will  
always be set to the same thing, but if used in only\_if can be a Python  
expression to determine whether a task should be run on this particular  
host.

Lines 270-273, 286-287, 305-306 are missing an indentation.

---

<div class="post-metadata">

**Author:** ![John\_Callender](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@John\_Callender](https://forum.ansible.com/u/John_Callender)\
**Post date:** [June 9, 2012, 6:52pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/6 "2012-06-09T18:52:25Z")

</div>

Yes, this is excellent, and will be very helpful for a person new to ansible (like me).

I’d love to see this completed. If you were willing to do do even more, and give additional examples demonstrating nice, scalable approaches to common ansible configuration and deployment use cases, I’d love that as well. Go pedantry! 🙂

Thanks.

John

---

<div class="post-metadata">

**Author:** ![Brad\_Olson](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@Brad\_Olson](https://forum.ansible.com/u/Brad_Olson)\
**Post date:** [June 13, 2012, 5:22pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/7 "2012-06-13T17:22:56Z")

</div>

I enjoyed the read! Nice work, Fred!

---

<div class="post-metadata">

**Author:** ![Rodney\_Quillo](https://avatars.discourse-cdn.com/v4/letter/r/7ba0ec/32.png) [@Rodney\_Quillo](https://forum.ansible.com/u/Rodney_Quillo)\
**Post date:** [June 14, 2012, 9:09am UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/8 "2012-06-14T09:09:11Z")

</div>

Very nice.

I've also compiled some playbooks here:  
[https://github.com/cocoy/ansible-playbooks](https://github.com/cocoy/ansible-playbooks)

I hope it will help others too. 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [June 15, 2012, 11:37am UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/9 "2012-06-15T11:37:42Z")

</div>

I don’t know what that means either 🙂

---

<div class="post-metadata">

**Author:** ![Fred\_Alger](https://avatars.discourse-cdn.com/v4/letter/f/c89c15/32.png) [@Fred\_Alger](https://forum.ansible.com/u/Fred_Alger)\
**Post date:** [June 15, 2012, 4:46pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/10 "2012-06-15T16:46:21Z")

</div>

Sorry, my brain failed when I wrote that section. There are three things going on:

1. Right now, variables set IN the vars section can’t be used in the vars section. There’s an enhancement issue filed for 0.5 about this.

2. Variables defined BEFORE the vars section CAN be used in vars. For example --extra-vars=“…” and host group variables.

3. My understanding of conditionals in playbooks is weak. I’m going to experiment and brush up that section.

---

<div class="post-metadata">

**Author:** ![Brad\_Olson](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@Brad\_Olson](https://forum.ansible.com/u/Brad_Olson)\
**Post date:** [June 19, 2012, 12:14pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/11 "2012-06-19T12:14:20Z")

</div>

Fred,

> > What I do not understand is  
> > Line 108: Variables do not expand inside the ‘vars’ section.  
> > and then  
> > Line 120: is\_color\_blue: “‘$color’ == ‘blue’”

That can be a bit confusing, but I think I can clear that up for you. The fine point here is that conditionals are expanded later and evaluated as python code.

So let’s when the vars section is evaluated, let’s say color=“red”. Then, is\_color\_blue is set, literally, to “‘$color’ == ‘blue’” (a string containing single quotes and a dollar sign).

If you’re new to Python, note that Python does not distinguish between single and double quotes (you just have to terminate with what you started).

Later, when the only\_if is evaluated, a few things happen:

1. The string “‘$color’ == ‘blue’” is expanded to “‘red’ == ‘blue’”.
2. Python eval() is called: eval(“‘red’ == ‘blue’”)
3. Since that returns False, the task is skipped.  
I might have some of the deep timing details wrong, but that is the logic that’s happening: only\_if’s receive strings that are expanded very late in the run and evaluated by Python.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [June 19, 2012, 12:39pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/12 "2012-06-19T12:39:43Z")

</div>

> 1. Right now, variables set IN the vars section can’t be used in the vars section. There’s an enhancement issue filed for 0.5 about this.

This is now possible, FWIW…

---

<div class="post-metadata">

**Author:** ![Fred\_Alger](https://avatars.discourse-cdn.com/v4/letter/f/c89c15/32.png) [@Fred\_Alger](https://forum.ansible.com/u/Fred_Alger)\
**Post date:** [June 19, 2012, 2:30pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/13 "2012-06-19T14:30:46Z")

</div>

> Fred,  
> …
> 
> Later, when the only\_if is evaluated, a few things happen:
> 
> 1. The string “‘$color’ == ‘blue’” is expanded to “‘red’ == ‘blue’”.
> 2. Python eval() is called: eval(“‘red’ == ‘blue’”)
> 3. Since that returns False, the task is skipped.  
> I might have some of the deep timing details wrong, but that is the logic that’s happening: only\_if’s receive strings that are expanded very late in the run and evaluated by Python.
> 
> Hope that helps.

Very clear, thanks. I’ve worded things better in my example.

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [June 22, 2012, 9:15am UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/14 "2012-06-22T09:15:27Z")

</div>

> \> 1. Right now, variables set IN the vars section can't be used in the  
> \> vars section. There's an enhancement issue filed for 0.5 about this.
> 
> This is now possible, FWIW…

I can't get this to work with the devel branch checked out just a few  
minutes ago:

playbook

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [June 22, 2012, 9:55am UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/15 "2012-06-22T09:55:58Z")

</div>

Jan-Piet Mens wrote:

> > \> 1. Right now, variables set IN the vars section can't be used in the  
> > \> vars section. There's an enhancement issue filed for 0.5 about this.
> > 
> > This is now possible, FWIW…
> 
> I can't get this to work with the devel branch checked out just a few  
> minutes ago:
> 
> playbook  
> --------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- hosts: all  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;domain: example.org  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server: www.${domain}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user: root  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[...]  
> run  
> ---  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ ansible-playbook jp.yaml
> 
> target-node  
> -----------
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ egrep '(domain|server)' /etc/ansible/setup  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"domain": "example.org",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"server": "www.${domain}"
> 
> Did I misunderstand something?

Use it in the playbook. E.g.  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- name: test variables  
&nbsp;&nbsp;&nbsp;&nbsp;action: shell echo $server \>\> /tmp/foo

/etc/ansible/setup gets the data as-is.

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [June 22, 2012, 3:47pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/16 "2012-06-22T15:47:51Z")

</div>

> \> playbook  
> \> --------  
> \> ---  
> \> - hosts: all  
> \> vars:  
> \> domain: example.org  
> \> server: www.${domain}

[...]

> \> $ egrep '(domain|server)' /etc/ansible/setup  
> \> "domain": "example.org",  
> \> "server": "www.${domain}"
> 
> Use it in the playbook. E.g.  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- name: test variables  
> &nbsp;&nbsp;&nbsp;&nbsp;action: shell echo $server \>\> /tmp/foo

I'll try that (on the road at the moment), but:

> /etc/ansible/setup gets the data as-is.

That's not very intuitive, is it? Furthermore, that means that the  
variable $server (i.e. {{ server }}) can't be used in a template, I  
assume?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [June 22, 2012, 3:54pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/17 "2012-06-22T15:54:28Z")

</div>

> > /etc/ansible/setup gets the data as-is.
> 
> That’s not very intuitive, is it? Furthermore, that means that the  
> variable $server (i.e. {{ server }}) can’t be used in a template, I  
> assume?

/etc/ansible/setup is not really for public consumption, it’s an artifact of how the system works.

That variable can totally be used in a template. Templates are actually computed server side in 0.4 and beyond.

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [June 22, 2012, 5:06pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/18 "2012-06-22T17:06:08Z")

</div>

> /etc/ansible/setup is not really for public consumption, it's an  
> artifact of how the system works.

Understood. Nevertheless, /etc/ansible/setup is useful: fetch it from  
servers to have a parseable inventory. 🙂

> That variable can totally be used in a template. Templates are  
> actually computed server side in 0.4 and beyond.

Then I'm really doing something wrong -- I don't get the variable in the  
computed template, but where's my error? (Using commit  
1b5d5feef37b5c821ffa32b2f079b727f6ead959)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

template (temp.j2):

---

<div class="post-metadata">

**Author:** ![Mark\_Theunissen](https://avatars.discourse-cdn.com/v4/letter/m/898d66/32.png) [@Mark\_Theunissen](https://forum.ansible.com/u/Mark_Theunissen)\
**Post date:** [June 25, 2012, 3:24pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/19 "2012-06-25T15:24:16Z")

</div>

Fred, thanks for the awesome example. I have found a bug, in the example for what a vars\_file should look like, you have :

```auto

Example YAML for a file to be included by vars_files:
  # ---
  # - monitored_by: phobos.mars.nasa.gov
  # - fish_sticks: "good with custard"
  # # (END OF DOCUMENT)

```

This doesn’t work, it needs to be changed to remove the initial ‘-’ from each variable, like so:

```auto

Example YAML for a file to be included by vars_files:
  # ---
  # monitored_by: phobos.mars.nasa.gov
  # fish_sticks: "good with custard"
  # # (END OF DOCUMENT)

```

I’m not sure how to send pull requests on gists. 🙂

- Mark

---

<div class="post-metadata">

**Author:** ![Brad\_Olson](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@Brad\_Olson](https://forum.ansible.com/u/Brad_Olson)\
**Post date:** [July 2, 2012, 1:42pm UTC](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607/20 "2012-07-02T13:42:44Z")

</div>

Time for this useful example to get into a repo, I think. It’s outgrown gist.  
I’m not sure how to send pull requests on gists. 🙂

[Next page](https://forum.ansible.com/t/pedantically-commented-playbook-example/11607.md?page=2)
