Questions regarding porting action plugin from 1.8.x to 2.x

34 views
Skip to first unread message

David Reuss

unread,
Aug 8, 2016, 8:54:48 AM8/8/16
to Ansible Project
Hi,

I've delayed porting my pre 2.0 action plugins, but now it's finally time to attempt to upgrade to ansible 2.x, and i have a few questions.

The porting guide is rather lacking in details, especially regarding action plugins, but i have this really simple (but incredibly stupid) plugin which i either need to work for 2.x or someone suggest a workaround to get my desired behaviour, which basically is:

From a play - set a variable available on *all* hosts, across *all* plays -- basically i need to be able to run a task anywhere - and have the variable stay the same across all hosts for the duration of the entire playbook.

Note i can't convert everything to `-e`/extra vars from the command line, since a lot of the variables are dependent on other variables from plays/tasks/whatever, during the playbook execution.

Example usage:

    - name: "Set variables"
      local_action: inject_vars
      args:
        is_a: true
        is_b: false
        
This is the meat of the plugin that currently works as i want it to:


     class ActionModule(object):
    ''' Create inventory groups based on variables '''

    ### We need to be able to modify the inventory
    BYPASS_HOST_LOOP = True
    TRANSFERS_FILES = False

    def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):

      # ... irelevant handling of args

      inventory = self.runner.inventory
      allgroup = inventory.get_group('all')

      for k, v in data.iteritems():
        allgroup.set_variable(k, v)

      result = {'changed': True, 'data': data}
      return ReturnData(conn=conn, comm_ok=True, result=result)

Action plugins seems to have changed quite a bit in 2.x and i'm currently unable to get anything to work, that will allow me to access the *current* inventory. There's probably very good reasons for this (and my desired functionality probably conflicts quite a bit with what should be allowed).

I've tried gaining access to inventory, variable manager/tqm -- no luck so far.

So - any ideas on how i can continue progress from here?

Thanks in advance.

Brian Coca

unread,
Feb 9, 2017, 10:10:24 PM2/9/17
to Ansible Project
Those are things only available 'pre fork', the plugin executes post
fork. FYI, you can probably do same with set_fact/delegate_facts.


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages