/lib/ansible/runner/action_plugins/group_by.py

https://github.com/ajanthanm/ansible · Python · 106 lines · 63 code · 18 blank · 25 comment · 15 complexity · 7fe0a2eacb86fa9b6e0508f438148bfc MD5 · raw file

  1. # Copyright 2012, Jeroen Hoekx <jeroen@hoekx.be>
  2. #
  3. # This file is part of Ansible
  4. #
  5. # Ansible is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # Ansible is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
  17. import ansible
  18. from ansible.callbacks import vv
  19. from ansible.errors import AnsibleError as ae
  20. from ansible.runner.return_data import ReturnData
  21. from ansible.utils import parse_kv, check_conditional
  22. import ansible.utils.template as template
  23. class ActionModule(object):
  24. ''' Create inventory groups based on variables '''
  25. ### We need to be able to modify the inventory
  26. BYPASS_HOST_LOOP = True
  27. TRANSFERS_FILES = False
  28. def __init__(self, runner):
  29. self.runner = runner
  30. def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
  31. # the group_by module does not need to pay attention to check mode.
  32. # it always runs.
  33. # module_args and complex_args have already been templated for the first host.
  34. # Use them here only to check that a key argument is provided.
  35. args = {}
  36. if complex_args:
  37. args.update(complex_args)
  38. args.update(parse_kv(module_args))
  39. if not 'key' in args:
  40. raise ae("'key' is a required argument.")
  41. vv("created 'group_by' ActionModule: key=%s"%(args['key']))
  42. inventory = self.runner.inventory
  43. result = {'changed': False}
  44. ### find all groups
  45. groups = {}
  46. for host in self.runner.host_set:
  47. data = {}
  48. data.update(inject)
  49. data.update(inject['hostvars'][host])
  50. conds = self.runner.conditional
  51. if type(conds) != list:
  52. conds = [ conds ]
  53. next_host = False
  54. for cond in conds:
  55. if not check_conditional(cond, self.runner.basedir, data, fail_on_undefined=self.runner.error_on_undefined_vars):
  56. next_host = True
  57. break
  58. if next_host:
  59. continue
  60. # Template original module_args and complex_args from runner for each host.
  61. host_module_args = template.template(self.runner.basedir, self.runner.module_args, data)
  62. host_complex_args = template.template(self.runner.basedir, self.runner.complex_args, data)
  63. host_args = {}
  64. if host_complex_args:
  65. host_args.update(host_complex_args)
  66. host_args.update(parse_kv(host_module_args))
  67. group_name = host_args['key']
  68. group_name = group_name.replace(' ','-')
  69. if group_name not in groups:
  70. groups[group_name] = []
  71. groups[group_name].append(host)
  72. result['groups'] = groups
  73. ### add to inventory
  74. for group, hosts in groups.items():
  75. inv_group = inventory.get_group(group)
  76. if not inv_group:
  77. inv_group = ansible.inventory.Group(name=group)
  78. inventory.add_group(inv_group)
  79. for host in hosts:
  80. if host in self.runner.inventory._vars_per_host:
  81. del self.runner.inventory._vars_per_host[host]
  82. inv_host = inventory.get_host(host)
  83. if not inv_host:
  84. inv_host = ansible.inventory.Host(name=host)
  85. if inv_group not in inv_host.get_groups():
  86. result['changed'] = True
  87. inv_group.add_host(inv_host)
  88. return ReturnData(conn=conn, comm_ok=True, result=result)