How to split action into multiline?

How to split action into multiline?

e.g.

Your indentation is off.

Several options.

# (1) complex arguments by passing a hash to the module

  - name: Update the Apache config
    copy:
        src: httpd.conf
        dest: /etc/httpd/httpd.conf

# (2) or equivalently

  - name: Update the Apache config
    action:
        module: copy
        src: httpd.conf
        dest: /etc/httpd/httpd.conf

# (3) YAML line continuations

  - name: Update the Apache config
    copy: >
        src=httpd.conf
        dest=/etc/httpd/httpd.conf

# )4_ YAML line continuations

  - name: Update the Apache config
    action: copy >
        src=httpd.conf
        dest=/etc/httpd/httpd.conf

I generally prefer option 1 or 3.

I usually prefer 1 too, since the others get messy quickly and it creates better diff in git. But it makes problems with modules which don’t have a named argument. For instance:

  • name: Create SSH keypair files
    command: >
    ssh-keygen -f id_rsa -N ‘’
    creates=id_rsa

work but I cannot do:

  • name: Create SSH keypair files
    command:
    command: ssh-keygen -f id_rsa -N ‘’
    creates: id_rsa

Is there a way? I also tried to “0:” instead of “command:” but that didn’t work either.

This?

  • name: Create SSH keypair files

command:
command: ssh-keygen -f id_rsa -N ‘’
creates: id_rsa

No, that won’t work. The command module doesn’t do key=value arguments.

I've had no problems with the following, also:

    - name: Update the Apache config
      copy: src=httpd.conf
            dest=/etc/httpd/httpd.conf

PyYAML, at least, seems to assign all of
"src=httpd.conf dest=/etc/httpd/httpd.conf" as the value of the "copy"
key. yamllint.com also seems to have no issues with this.

Anything no good about this that I'm missing?

# (3) YAML line continuations
- name: Update the Apache config
   copy: >
       src=httpd.conf
       dest=/etc/httpd/httpd.conf

(3) Also seems to work without the ">", for example:

    copy:
        src=httpd.conf
        dest=/etc/httpd/httpd.conf

Formatting long lines is something we run into frequently with a lot
of people working on the same playbooks, would a section on line
continuation in the best practices section of the docs make sense?
I wouldn't mind taking a stab at it if it's something the community
would find useful.

-John

That would be great!

Probably should go here:

http://www.ansibleworks.com/docs/YAMLSyntax.html

I just ran into the multiple line issue and found this thread via search.

Probably should go here:

http://www.ansibleworks.com/docs/YAMLSyntax.html

Info about multiple lines doesn’t seem to be there, however it’s mentioned in http://www.ansibleworks.com/docs/playbooks.html#tasks-list

This thread seems to be the most complete source of documentation on the topic :slight_smile:
Is it worth raising a GitHub issue for “incomplete” documentation or is the split-on-space method the preferred way of breaking long lines?

Thanks,
Adam Baxter

You can file github tickets on the docs, sure.

Since the documentation is open source, an even better thing to do (if you are so inclined) is to patch the docs:

https://github.com/ansible/ansible/tree/devel/docsite/rst

I think covering this in multiple places can’t hurt.