How to get value of --limit argument inside an Ansible playbook?

Viewed 10249

In ansible, is it possible to get the value of the argument to the "--limit" option within a playbook? I want to do is something like this:

---
- hosts: all
  remote user: root
  tasks:
  - name: The value of the --limit argument
    debug:
      msg: "argument of --limit is {{ ansible-limit-arg }}"

Then when I run he command:

$ ansible-playbook getLimitArg.yaml --limit webhosts

I'll get this output:

argument of --limit is webhost

Of course, I made up the name of the variable "ansible-limit-arg", but is there a valid way of doing this? I could specify "webhosts" twice, the second time with --extra-args, but that seems a roundabout way of having to do this.

4 Answers

Since Ansible 2.5 you can access the value using a new magic variable ansible_limit, so:

- debug:
    var: ansible_limit
Related