Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using programatically generated Ansible playbooks. In general, because playbooks are just YAML, this is straightforward. However, when using the "simple" key=value form, playbooks aren't pure YAML -- they include content embedded a shlex-parsable form.

To avoid the ambiguity in this form (is that key=value pair an argument to the command or an argument for ansible?), and have only a single format to parse and generate, I'm unconditionally using the complex args mechanism demonstrated by example in the ansible-examples repository.

This uses a syntax of the following kind:

action: module-name
args:
  key1: value1
  key2: value2

...which is well and good. However, when trying to use this form for the shell or command modules (whose documentation describes the actual command as being passed in an argument named free_form), this doesn't work so well:

action: shell
args:
  free_form: echo hello_world >/tmp/something
  creates: /tmp/something

When invoked, this runs the following:

/bin/sh -c " free_form='echo hello_world >/tmp/something'  "

...which is very much not what I'm trying to accomplish.

What's the right way to use Ansible modules taking "free-form" commands using pure YAML syntax?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Short answer: Don't use the command, raw, script, or shell modules. Write your own module that accepts the command as a "normal" argument.

Long answer:

In most cases, you can do this:

- shell: echo hello_world > /tmp/something
  args:
    creates: /tmp/something

However, this fails in some edge cases:

- shell: echo hello_world > creates=something
  args:
    creates: creates=something  # The file is named "creates=something"

I don't know of a general way to handle this, but a bash-specific solution is:

- shell: echo hello_world > "creates=something"
  args:
    creates: creates=something
share|improve this answer
    
Is there a data structure I can pass to any compliant YAML generator to make it emit - shell: ...? If this structure is something that can only be reliably generated by hand, that somewhat defeats the point of the question. –  Charles Duffy Jun 13 at 17:50
    
@CharlesDuffy: I don't think you can escape the ... part in general. If you look at library/commands/command, you'll find a pretty generous regex matching creates=, removes=, chdir=, and so on. If you need to guarantee any command can be passed through, you'll have to write your own module. –  Snowball Jun 13 at 19:30
    
Fair 'nuff. This is, in my mind, a significant design misfeature... but, well, is what it is. –  Charles Duffy Jun 13 at 19:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.