# Questions/comments regarding a playbook

**URL:** <https://forum.ansible.com/t/questions-comments-regarding-a-playbook/12034>\
**Category:** Ansible Project\
**Created:** [October 26, 2012, 1:16pm UTC](https://forum.ansible.com/t/questions-comments-regarding-a-playbook/12034 "2012-10-26T13:16:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maykel\_Moya](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Maykel\_Moya](https://forum.ansible.com/u/Maykel_Moya)\
**Post date:** [October 26, 2012, 1:16pm UTC](https://forum.ansible.com/t/questions-comments-regarding-a-playbook/12034/1 "2012-10-26T13:16:32Z")

</div>

Hello

I'm coming from Puppet world and starting to play with Ansible devel branch.

I'm trying to "translate" a class for configuring nrsysmond and have  
some questions/comments. Please see [http://pastebin.com/RScspjRd](http://pastebin.com/RScspjRd)

the "Bugs" section

---

<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:** [October 26, 2012, 1:28pm UTC](https://forum.ansible.com/t/questions-comments-regarding-a-playbook/12034/2 "2012-10-26T13:28:53Z")

</div>

> the "Bugs" section  
> ------------------  
> In line 11 I had to escape the '\1' because python was parsing it and  
> sending a '\u0001' char to sed. I think this should be documented in  
> both 'shell' and 'command' modules.

I'd highly recommend using the template module than relying on sed in  
a playbook, for general reasons of idempotence.  
It's far easier.

- template: src=... dest=...

Versus the 5 line stanza of only\_if/ignore\_errors/etc that you have there.

> template module in line 3 complains if I add a 'state=file'.According to  
> the documentation template accepts everything accepted by file.

Yeah, that's true, it accepts all parameters that modify the file.  
I think you are the first to try passing state 🙂

> the "Concepts" section  
> ----------------------  
> 1. In line 3 I had to create a template for one line. Is there any plan  
> to support including text in the yaml itself? I'm thinking in something  
> like Puppet's inline\_template[1].

No immediate plans, but the whole "lookup" plugin system that powers  
with\_items could be easily hijacked to add that.

with\_inline\_template: "\<string\>"

could set $item appropriately. However, as I don't see many people  
making use of this, I generally don't think it's something we would  
add to core.  
Lookup plugins are pluggable though, so you could add it if you wanted.

> 2. As can be seen I'm using "only\_if: is\_set('${newrelic\_key}')" in  
> every task. This file in pastebin.com is included from main.yml so  
> something like this:
> 
> - include: tasks/nrsysmond.yml  
> &nbsp;&nbsp;only\_if: is\_set(...)

Yeah, that's not going to happen like that, because of /when/ only\_if  
gets evaluated,

What seems to make more sense to me is to allow attaching an "only\_if"  
(well, the new 'when' operator coming in 0.9 is much more likely) to  
the play itself, and that would in turn automatically attach the  
conditional to all of the tasks below it.

> vars:  
> &nbsp;&nbsp;desired\_state: "'file' if is\_set('${newrelic\_key}') else 'absent'"
> 
> but it didn't work, desired\_state got the expression as is, it wasn't  
> evaluated. Recommendations?

only\_if is for boolean conditionals only. Unlike Puppet, we do not  
want things to start looking like a programming language. That's the  
whole  
point of Ansible. In this case you would have two tasks, one with a  
conditional, one with the inverse conditional.

You can save only\_if terms in variables, so it is as simple as:

vars:  
&nbsp;&nbsp;&nbsp;&nbsp;newrelic\_set: "is\_set('$newrelic\_key')"

only\_if: "not $newrelic\_set"

AND

only\_if: "$newrelic\_set"

again, having conditions attached to plays also makes sense. Maybe later.

> 4. I could write a module for getting the current key if  
> /etc/newrelic/nrsysmond.cfg is present but I think it's overkill for  
> just a line match on a file, no matter how easy is to write a module. In  
> Puppet I have:
> 
> exec {"nrsysmond-config --set license\_key=${sysmond::key}":  
> &nbsp;&nbsp;unless =\> "grep -E '^license\_key=${sysmond::key}'  
> /etc/newrelic/nrsysmond.cfg"  
> }

You can use the "shell/command" module to do this already, and  
register the variable -- to avoid writing a module.

command: blarg  
register: $newrelic\_key

and later use

${newrelic\_key.stdout} anywhere later on down the line.
