How to access variables declared in a playbook file inside a custom Ansible plugin?

Viewed 38

I am going through the official Ansible documentation regarding developing custom plugins. I am unable to find a way to access the variables declared in a playbook YML file from within the custom plugin (action). Is there a way to accomplish this task? I'd love to view a wiki page for the same (if it exists). TIA

For example:

1.) The playbook:

---
- hosts: all

  tasks:
  - name: "Set facts"
    set_facts:
      gate_enabled: "True"

2.) The plugin:

class ActionModule(ActionBase):
  def run(self, tmp=None, task_vars=None):
    #access the variable gate_enabled here 
1 Answers

I've managed to access the playbook variables inside the action plugin using ActionBase class' run method's task_vars property. It is a dictionary type and one can use the get method to access a particular variable. Hope it helps others as well.

EDIT: Added code sample

class ActionModule(ActionBase):
  def run(self, tmp=None, task_vars=None):
    #access the variable gate_enabled here
    task_vars.get("gate_enabled")
Related