parse_ignition.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """Ansible action plugin to decode ignition payloads"""
  2. import base64
  3. import os
  4. from ansible.plugins.action import ActionBase
  5. from ansible import errors
  6. from six.moves import urllib
  7. def get_files(files_dict, systemd_dict, dir_list, data):
  8. """parse data to populate file_dict"""
  9. for item in data['storage']['files']:
  10. path = item["path"]
  11. dir_list.add(os.path.dirname(path))
  12. # remove prefix "data:,"
  13. encoding, contents = item['contents']['source'].split(',', 1)
  14. if 'base64' in encoding:
  15. contents = base64.b64decode(contents).decode('utf-8')
  16. else:
  17. contents = urllib.parse.unquote(contents)
  18. # convert from int to octal, padding at least to 4 places.
  19. # eg, 420 becomes '0644'
  20. mode = str(format(int(item["mode"]), '04o'))
  21. inode = {"contents": contents, "mode": mode}
  22. files_dict[path] = inode
  23. # get the systemd units files we're here
  24. for item in data['systemd']['units']:
  25. contents = item['contents']
  26. mode = "0644"
  27. inode = {"contents": contents, "mode": mode}
  28. name = item['name']
  29. path = '/etc/systemd/system/' + name
  30. dir_list.add(os.path.dirname(path))
  31. files_dict[path] = inode
  32. enabled = item.get('enabled') or True
  33. systemd_dict[name] = enabled
  34. class ActionModule(ActionBase):
  35. """ActionModule for parse_ignition.py"""
  36. def run(self, tmp=None, task_vars=None):
  37. """Run parse_ignition action plugin"""
  38. result = super(ActionModule, self).run(tmp, task_vars)
  39. result["changed"] = False
  40. result["failed"] = False
  41. result["msg"] = "Parsed successfully"
  42. files_dict = {}
  43. systemd_dict = {}
  44. dir_list = set()
  45. result["files_dict"] = files_dict
  46. result["systemd_dict"] = systemd_dict
  47. # self.task_vars holds all in-scope variables.
  48. # Ignore settting self.task_vars outside of init.
  49. # pylint: disable=W0201
  50. self.task_vars = task_vars or {}
  51. ign_file_contents = self._task.args.get('ign_file_contents')
  52. get_files(files_dict, systemd_dict, dir_list, ign_file_contents)
  53. result["dir_list"] = list(dir_list)
  54. return result