What exactly is the "action" module and how does it work?

Hi,

I was looking for a way to run the distro-dependent package manager w/o too many "when"s and I found this (on https://ansible-tips-and-tricks.readthedocs.io/en/latest/os-dependent-tasks/installing_packages/#installing-packages):

- name: install basic package
   action: >
     {{ ansible_pkg_mgr }} name=vim state=present update_cache=yes

However I cannot find this "action" module mentioned anywhere in the whole Ansible documentation and I also don't understand, why the ">" is needed at that point. Is this an obsolete feature or a very new one, which is not documented yet? What exactly can one do with it?

Cheers
frank

Hi,

I was looking for a way to run the distro-dependent package manager w/o too many "when"s and I found this (on https://ansible-tips-and-tricks.readthedocs.io/en/latest/os-dependent-tasks/installing_packages/#installing-packages):

- name: install basic package
   action: >
     {{ ansible_pkg_mgr }} name=vim state=present update_cache=yes

However I cannot find this "action" module mentioned anywhere in the whole Ansible documentation

https://docs.ansible.com/ansible/latest/playbooks_intro.html#tasks-list
https://docs.ansible.com/ansible/latest/playbooks_intro.html#action-shorthand

In short, it's an old syntax not encourage used anymore.

and I also don't understand, why the ">" is needed at that point.

It's an yaml indicator character, ">" indicate that the lines can be broken to multiple lines.

Is this an obsolete feature or a very new one, which is not documented yet? What exactly can one do with it?

I think you need to use action if the module name is a variable as in you example.
I think these two examples would fail.

   - name: install basic package
     {{ ansible_pkg_mgr }}: name=vim state=present update_cache=yes

and

   - name: install basic package
     {{ ansible_pkg_mgr }}:
       name: vim
       state: present
       update_cache: yes

Thanks a lot

Hi,

I was looking for a way to run the distro-dependent package manager
w/o too many "when"s and I found this (on
https://ansible-tips-and-tricks.readthedocs.io/en/latest/os-dependent-tasks/installing_packages/#installing-packages):

- name: install basic package
   action: >
     {{ ansible_pkg_mgr }} name=vim state=present update_cache=yes

However I cannot find this "action" module mentioned anywhere in the
whole Ansible documentation

https://docs.ansible.com/ansible/latest/playbooks_intro.html#tasks-list
https://docs.ansible.com/ansible/latest/playbooks_intro.html#action-shorthand

Shame on me. I overlooked it in the very basic intro page... :-}

In short, it's an old syntax not encourage used anymore.

and I also don't understand, why the ">" is needed at that point.

It's an yaml indicator character, ">" indicate that the lines can be
broken to multiple lines.

Yes, that I know that, but I just don't understand, why

- name: install basic package
    action: {{ ansible_pkg_mgr }} name=vim state=present update_cache=yes

or

- name: install basic package
    action: "{{ ansible_pkg_mgr }}" name=vim state=present update_cache=yes

Wouldn't work (both fail with YAML syntax error)

Is this an obsolete feature or a very new one, which is not
documented yet? What exactly can one do with it?

I think you need to use action if the module name is a variable as in
you example.
I think these two examples would fail.

  - name: install basic package
    {{ ansible_pkg_mgr }}: name=vim state=present update_cache=yes

and

  - name: install basic package
    {{ ansible_pkg_mgr }}:
      name: vim
      state: present
      update_cache: yes

Indeed they fail, and there doesn't seem to be a useable replacement in the current syntax. :frowning:

frank

and I also don't understand, why the ">" is needed at that point.

It's an yaml indicator character, ">" indicate that the lines can be
broken to multiple lines.

Yes, that I know that, but I just don't understand, why

It looks nicer and that don't need the quotes perhaps?

- name: install basic package
  action: {{ ansible_pkg_mgr }} name=vim state=present update_cache=yes

or

- name: install basic package
  action: "{{ ansible_pkg_mgr }}" name=vim state=present update_cache=yes

Wouldn't work (both fail with YAML syntax error)

{ it's not allox after the colon and in yaml, if it start with a quote it must end with the same qoute, so this should work.

- name: install basic package
   action: "{{ ansible_pkg_mgr }} name=vim state=present pdate_cache=yes"

Is this an obsolete feature or a very new one, which is not
documented yet? What exactly can one do with it?

I think you need to use action if the module name is a variable as in
you example.
I think these two examples would fail.

  - name: install basic package
    {{ ansible_pkg_mgr }}: name=vim state=present update_cache=yes

and

  - name: install basic package
    {{ ansible_pkg_mgr }}:
      name: vim
      state: present
      update_cache: yes

Indeed they fail, and there doesn't seem to be a useable replacement in the current syntax. :frowning:

If you don't need the cache update a module exist, it's called packages.