I stumbled across following problem creating custom python ansbile action plugin: how to execute some modules once (i know in yaml it is possible to set run_once and delegate_to)?

For example having following code:

from ansible.plugins.action import ActionBase
from ansible.errors import AnsibleError

class ActionModule(ActionBase):

    def run(self, tmp=None, task_vars=None):

      super(ActionModule, self).run(tmp, task_vars)

      module_args = self._task.args.copy()

      ret = {}

      # create group
      module_return = self._execute_module(module_name='group',module_args={'state':'present','name' : 'bob'}, task_vars=task_vars,tmp=tmp)

      module_return = self._execute_module(module_name='user',module_args = {'state':'present','name' : 'bob','group' : 'bob','home':'/nfs/bob' },task_vars=task_vars,tmp=tmp)

      # run once this command ????
      module_return = self._execute_module(module_name='file',module_args = {'path':'/nfs/bob','state':'directory','mode' : '0700' },task_vars=task_vars,tmp=tmp)

      return ret

Current code will execute file module for every host, but i need to execute that part once.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.