Yaml confusion with local_action

I needed to add the local_action directive to an action, so I tried this:

    - name: Create unique tempdir path 
      local_action:
        ansible.builtin.tempfile:
          state: directory
          suffix: _sshpub

But the YAML parser wasn’t happy with this. Its rather weak error message didn’t give me much clue, so I resorted to ChatGPT + Google. “They” suggested:

    - name: Create unique tempdir path 
      local_action:
        module: ansible.builtin.tempfile
        state: directory
        suffix: _sshpub
      register: temp_dir

This works. – But why does the first version not work? I usually do not write my actions with the explicit module specifier (but rather as the action “heading”) , and it feels annoying that in this one situation I have to do use “module:”.

I am confused. I clearly miss something here…


ps: Bonus question: Is it possible to apply the local_action direction to multiple actions (via a block construct maybe)? – My AI didn’t think so, but that doesn’t mean too much.

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html#delegating-tasks

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_blocks.html#grouping-tasks-with-blocks

I personally think it’s better to use the delegate_to: localhost parameter than to use local_action … for one, just the reading of the playbook is simpler from an indentation perspective…

I think the first block you have didn’t work because of the indentation - with local_action, I believe it flattens the indentation of the module parameters. Again, I personally like everything uniform, so I prefer delegate_to…

So I think this block should work fine as well, and do the same thing:

- name: Create unique tempdir path 
  delegate_to: localhost
  ansible.builtin.tempfile:
    state: directory
    suffix: _sshpub
3 Likes

I agree with @jrglynn2’s observation on using delegate_to, I never use local_action. And when using a block, you can set delegate_to on that level to make it apply to all tasks in the block :slight_smile:

2 Likes

Since delegate_to: localhost has already been suggested I’m just going to clarify that action and local_action do not allow for unknown entries in their args in YAML form only in k=v, which assumes they are the ‘module/action plugin’, so these are all the valid forms:

- action:
       module: <action name>
- action: <action name>
- <action name>:

Of course, you can substitute action for local_action in any of these, but what you attempted it just not supported.

3 Likes