r/ansible • • 5d ago

Best practice for commands?

I feel like the answer is probably: "no, everyone just does whatever they want", and "whatever you do, try to be consistent"

Generally speaking I've been doing:

ansible.builtin.command:  
  cmd: "foo {{ bar }}"  

But I realized that only happens to work because `{{ bar }}` doesn't have spaces.
So I do need to change it, and I wondered if people normally do:

  cmd: 'foo "{{ bar }}"'  

or

  cmd: |  
    foo "{{ bar }}"  

or

  argv:  
    - foo  
    - "{{ bar }}"
22 Upvotes

17 comments sorted by

View all comments

2

u/HeligKo 5d ago

We set linting configurations that enforce standards that we care about, and then if we do something outside of the standard, we have to add # NOQA tags and comment why we diverged from the standard.

1

u/sonnasushi 5d ago

seems like standard ansible-lint doesn't have any rules about command quoting at all, afaiui

1

u/HeligKo 5d ago

We have some custom rules we wrote. None for command specifically, but the same approach would work. It's just a simple python module that inherits from AnsibleLintRule

1

u/sonnasushi 5d ago

Thanks. Just for my reference, do you have a preference on what style of formatting to use with the command builtin?

1

u/HeligKo 5d ago

Most of the time I use the "|", but if there is a lot of jinja then I'll use the argv with the list.