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

43 views
Skip to first unread message

Frank Thommen

unread,
Jul 27, 2017, 12:43:14 PM7/27/17
to Ansible Project
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


Kai Stian Olstad

unread,
Jul 27, 2017, 1:26:02 PM7/27/17
to ansible...@googlegroups.com
On 27. juli 2017 18:42, Frank Thommen wrote:
> 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


--
Kai Stian Olstad

Frank Thommen

unread,
Jul 27, 2017, 1:42:29 PM7/27/17
to ansible...@googlegroups.com
Thanks a lot

On 07/27/2017 07:25 PM, Kai Stian Olstad wrote:
> On 27. juli 2017 18:42, Frank Thommen wrote:
>> 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. :-(

frank


Kai Stian Olstad

unread,
Jul 27, 2017, 2:11:42 PM7/27/17
to ansible...@googlegroups.com
On 27. juli 2017 19:41, Frank Thommen wrote:
> On 07/27/2017 07:25 PM, Kai Stian Olstad wrote:
>> On 27. juli 2017 18:42, Frank Thommen wrote:
>>> 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. :-(
If you don't need the cache update a module exist, it's called packages.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages