How to split action into multiline?

5,827 views
Skip to first unread message

howa...@gmail.com

unread,
Aug 7, 2013, 1:07:24 PM8/7/13
to ansible...@googlegroups.com
How to split action into multiline?

e.g.

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

the following one does not work

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

Any idea to split a long action?

Michael DeHaan

unread,
Aug 7, 2013, 2:12:23 PM8/7/13
to ansible...@googlegroups.com
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.



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Andy

unread,
Aug 7, 2013, 4:01:05 PM8/7/13
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
Aug 7, 2013, 4:48:42 PM8/7/13
to ansible...@googlegroups.com
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.


--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

C. Morgan Hamill

unread,
Aug 8, 2013, 12:19:52 PM8/8/13
to ansible-project
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?
--
Morgan Hamill

John Jarvis

unread,
Aug 11, 2013, 12:34:27 PM8/11/13
to ansible...@googlegroups.com
> # (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

Michael DeHaan

unread,
Aug 11, 2013, 2:57:44 PM8/11/13
to ansible...@googlegroups.com
That would be great!

Probably should go here:



Adam Baxter

unread,
Nov 23, 2013, 6:32:42 PM11/23/13
to ansible...@googlegroups.com
I just ran into the multiple line issue and found this thread via search.

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 :)
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

Michael DeHaan

unread,
Nov 25, 2013, 1:44:22 PM11/25/13
to ansible...@googlegroups.com
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:


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


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages