pythonansibleansible-factsansible-module

Ansible: How to execute set_fact module from within ansible action plugin python code


I am writing a custom Action Plugin for Ansible which I use in my playbook and I am trying to set a variable that will be used in the next task, in the playbook, by a (custom) module.

Effectively, the playbook equivalent of what I am trying to mimic is a set_fact task like so:

- name: set_fact task
  set_fact: 
    ansible_python_interpreter: /path/to/python

In my custom Action Plugin, I have used self._execute_module before to execute other modules (such as slurp) within the plugin code. However, with the set_fact module it doesn't seem to be updating the ansible_python_interpreter variable, as expected.

I have tried the following:

self._execute_module(module_name='ansible.builtin.set_fact',
                     module_args=dict(ansible_python_interpreter=/path/to/python),
                     task_vars=task_vars)

And I have also tried different variations of module_args:

module_args=dict(key_value={ansible_python_interpreter=/path/to/python})
module_args=dict(key_value='ansible_python_interpreter:/path/to/python')

However, my ansible_python_interpreter does not seem to be changing.

Any help, please?


Solution

  • The closest I can get is to return a dict containing the Ansible facts that I want to be set for the playbook simply by following the dev guide for Action Plugins on the Ansible docs.

    So, as I am returning an _execute_model() call in my plugin as well, my run() function in my plugin would look something like this:

    def run(self, tmp=None, task_vars=None):
        # Plugin code here
    
        facts = dict()
        facts['ansible_python_interpreter'] = '/path/to/python'
    
        return dict(self._execute_module(module_name='my_custom_module',
                                         module_args=module_args,
                                         task_vars=task_vars),
                    ansible_facts=dict(facts))
    

    However, unfortunately this throws another warning/error of:

    [WARNING]: Removed restricted key from module data: ansible_python_interpreter

    And this seems to be due to a safety mechanism for overriding connection details, and so I have gone down a different route for my plugin.

    In another use case, returning dict(ansible_facts=dict(facts)) (like in the docs) would work if it wasn't a connection var I was trying to override, I believe.