6

I'd like to print out the command line arguments used to invoke ansible-playbook. E.g., if I do

ansible-playbook foo.yml -e bar=quux

, I'd like to have access to the above string, so that I can do as a task

- shell: slack_notify.sh "{{ ansible_cli_invocation }}"

where ansible_cli_invocation is a string with the value "ansible-playbook foo.yml -e bar=quux". Is there a way to do this?

Linus Arver
  • 1,331
  • 1
  • 13
  • 18

1 Answers1

6

I'm not sure you can do it out of the box.
But you can write a tiny action plugin:

from ansible.plugins.action import ActionBase
import sys

class ActionModule(ActionBase):

    TRANSFERS_FILES = False

    def run(self, tmp=None, task_vars=None):
        return { 'changed': False, 'ansible_facts': { 'argv': sys.argv } }

Save it as ./action_plugins/get_argv.py and also make an empty file ./library/get_argv.py. This creates local action get_argv that populates argv fact with arguments list.

Then in your playbook:

- get_argv:
- shell: slack_notify.sh "{{ argv | join(' ') }}"
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • 1
    I had to create `action_plugins` and `library` folders at the location of my playbook that I was running (I tried putting the folders in the ansible project root, and ansible could not find the module). – Linus Arver Sep 16 '16 at 23:55
  • @opert they should be in the folder from where you execute ansible or you can place them somewhere else and set paths in your ansible.cfg – Konstantin Suvorov Sep 17 '16 at 06:25
  • I suppose I forgot to add that I have playbooks in a non-standard `./playbooks` subfolder (hence my comment above). Thanks for clarifying; the upstream docs do mention that you can set the location of the library folder in ansible.cfg and such, but I'll try to avoid further customizations if possible. – Linus Arver Sep 17 '16 at 06:37
  • @opert in this case, you should place them in `./playbooks/action_plugins/` and `./playbooks/library` – Konstantin Suvorov Sep 17 '16 at 07:39
  • 1
    Sorry if I didn't make it clear in my first comment, but that's exactly what I did; I was merely commenting for posterity what I did to get it to work. – Linus Arver Sep 17 '16 at 08:01