yedit.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. #!/usr/bin/env python
  2. # pylint: disable=missing-docstring
  3. # ___ ___ _ _ ___ ___ _ _____ ___ ___
  4. # / __| __| \| | __| _ \ /_\_ _| __| \
  5. # | (_ | _|| .` | _|| / / _ \| | | _|| |) |
  6. # \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____
  7. # | \ / _ \ | \| |/ _ \_ _| | __| \_ _|_ _|
  8. # | |) | (_) | | .` | (_) || | | _|| |) | | | |
  9. # |___/ \___/ |_|\_|\___/ |_| |___|___/___| |_|
  10. #
  11. # Copyright 2016 Red Hat, Inc. and/or its affiliates
  12. # and other contributors as indicated by the @author tags.
  13. #
  14. # Licensed under the Apache License, Version 2.0 (the "License");
  15. # you may not use this file except in compliance with the License.
  16. # You may obtain a copy of the License at
  17. #
  18. # http://www.apache.org/licenses/LICENSE-2.0
  19. #
  20. # Unless required by applicable law or agreed to in writing, software
  21. # distributed under the License is distributed on an "AS IS" BASIS,
  22. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. # See the License for the specific language governing permissions and
  24. # limitations under the License.
  25. #
  26. # -*- -*- -*- Begin included fragment: lib/import.py -*- -*- -*-
  27. # pylint: disable=wrong-import-order,wrong-import-position,unused-import
  28. from __future__ import print_function # noqa: F401
  29. import copy # noqa: F401
  30. import json # noqa: F401
  31. import os # noqa: F401
  32. import re # noqa: F401
  33. import shutil # noqa: F401
  34. try:
  35. import ruamel.yaml as yaml # noqa: F401
  36. except ImportError:
  37. import yaml # noqa: F401
  38. from ansible.module_utils.basic import AnsibleModule
  39. # -*- -*- -*- End included fragment: lib/import.py -*- -*- -*-
  40. # -*- -*- -*- Begin included fragment: doc/yedit -*- -*- -*-
  41. DOCUMENTATION = '''
  42. ---
  43. module: yedit
  44. short_description: Create, modify, and idempotently manage yaml files.
  45. description:
  46. - Modify yaml files programmatically.
  47. options:
  48. state:
  49. description:
  50. - State represents whether to create, modify, delete, or list yaml
  51. required: true
  52. default: present
  53. choices: ["present", "absent", "list"]
  54. aliases: []
  55. debug:
  56. description:
  57. - Turn on debug information.
  58. required: false
  59. default: false
  60. aliases: []
  61. src:
  62. description:
  63. - The file that is the target of the modifications.
  64. required: false
  65. default: None
  66. aliases: []
  67. content:
  68. description:
  69. - Content represents the yaml content you desire to work with. This
  70. - could be the file contents to write or the inmemory data to modify.
  71. required: false
  72. default: None
  73. aliases: []
  74. content_type:
  75. description:
  76. - The python type of the content parameter.
  77. required: false
  78. default: 'dict'
  79. aliases: []
  80. key:
  81. description:
  82. - The path to the value you wish to modify. Emtpy string means the top of
  83. - the document.
  84. required: false
  85. default: ''
  86. aliases: []
  87. value:
  88. description:
  89. - The incoming value of parameter 'key'.
  90. required: false
  91. default:
  92. aliases: []
  93. value_type:
  94. description:
  95. - The python type of the incoming value.
  96. required: false
  97. default: ''
  98. aliases: []
  99. update:
  100. description:
  101. - Whether the update should be performed on a dict/hash or list/array
  102. - object.
  103. required: false
  104. default: false
  105. aliases: []
  106. append:
  107. description:
  108. - Whether to append to an array/list. When the key does not exist or is
  109. - null, a new array is created. When the key is of a non-list type,
  110. - nothing is done.
  111. required: false
  112. default: false
  113. aliases: []
  114. index:
  115. description:
  116. - Used in conjunction with the update parameter. This will update a
  117. - specific index in an array/list.
  118. required: false
  119. default: false
  120. aliases: []
  121. curr_value:
  122. description:
  123. - Used in conjunction with the update parameter. This is the current
  124. - value of 'key' in the yaml file.
  125. required: false
  126. default: false
  127. aliases: []
  128. curr_value_format:
  129. description:
  130. - Format of the incoming current value.
  131. choices: ["yaml", "json", "str"]
  132. required: false
  133. default: false
  134. aliases: []
  135. backup:
  136. description:
  137. - Whether to make a backup copy of the current file when performing an
  138. - edit.
  139. required: false
  140. default: true
  141. aliases: []
  142. separator:
  143. description:
  144. - The separator being used when parsing strings.
  145. required: false
  146. default: '.'
  147. aliases: []
  148. author:
  149. - "Kenny Woodson <kwoodson@redhat.com>"
  150. extends_documentation_fragment: []
  151. '''
  152. EXAMPLES = '''
  153. # Simple insert of key, value
  154. - name: insert simple key, value
  155. yedit:
  156. src: somefile.yml
  157. key: test
  158. value: somevalue
  159. state: present
  160. # Results:
  161. # test: somevalue
  162. # Multilevel insert of key, value
  163. - name: insert simple key, value
  164. yedit:
  165. src: somefile.yml
  166. key: a#b#c
  167. value: d
  168. state: present
  169. # Results:
  170. # a:
  171. # b:
  172. # c: d
  173. #
  174. # multiple edits at the same time
  175. - name: perform multiple edits
  176. yedit:
  177. src: somefile.yml
  178. edits:
  179. - key: a#b#c
  180. value: d
  181. - key: a#b#c#d
  182. value: e
  183. state: present
  184. # Results:
  185. # a:
  186. # b:
  187. # c:
  188. # d: e
  189. '''
  190. # -*- -*- -*- End included fragment: doc/yedit -*- -*- -*-
  191. # -*- -*- -*- Begin included fragment: class/yedit.py -*- -*- -*-
  192. # pylint: disable=undefined-variable,missing-docstring
  193. # noqa: E301,E302
  194. class YeditException(Exception):
  195. ''' Exception class for Yedit '''
  196. pass
  197. # pylint: disable=too-many-public-methods
  198. class Yedit(object):
  199. ''' Class to modify yaml files '''
  200. re_valid_key = r"(((\[-?\d+\])|([0-9a-zA-Z%s/_-]+)).?)+$"
  201. re_key = r"(?:\[(-?\d+)\])|([0-9a-zA-Z%s/_-]+)"
  202. com_sep = set(['.', '#', '|', ':'])
  203. # pylint: disable=too-many-arguments
  204. def __init__(self,
  205. filename=None,
  206. content=None,
  207. content_type='yaml',
  208. separator='.',
  209. backup=False):
  210. self.content = content
  211. self._separator = separator
  212. self.filename = filename
  213. self.__yaml_dict = content
  214. self.content_type = content_type
  215. self.backup = backup
  216. self.load(content_type=self.content_type)
  217. if self.__yaml_dict is None:
  218. self.__yaml_dict = {}
  219. @property
  220. def separator(self):
  221. ''' getter method for yaml_dict '''
  222. return self._separator
  223. @separator.setter
  224. def separator(self):
  225. ''' getter method for yaml_dict '''
  226. return self._separator
  227. @property
  228. def yaml_dict(self):
  229. ''' getter method for yaml_dict '''
  230. return self.__yaml_dict
  231. @yaml_dict.setter
  232. def yaml_dict(self, value):
  233. ''' setter method for yaml_dict '''
  234. self.__yaml_dict = value
  235. @staticmethod
  236. def parse_key(key, sep='.'):
  237. '''parse the key allowing the appropriate separator'''
  238. common_separators = list(Yedit.com_sep - set([sep]))
  239. return re.findall(Yedit.re_key % ''.join(common_separators), key)
  240. @staticmethod
  241. def valid_key(key, sep='.'):
  242. '''validate the incoming key'''
  243. common_separators = list(Yedit.com_sep - set([sep]))
  244. if not re.match(Yedit.re_valid_key % ''.join(common_separators), key):
  245. return False
  246. return True
  247. @staticmethod
  248. def remove_entry(data, key, sep='.'):
  249. ''' remove data at location key '''
  250. if key == '' and isinstance(data, dict):
  251. data.clear()
  252. return True
  253. elif key == '' and isinstance(data, list):
  254. del data[:]
  255. return True
  256. if not (key and Yedit.valid_key(key, sep)) and \
  257. isinstance(data, (list, dict)):
  258. return None
  259. key_indexes = Yedit.parse_key(key, sep)
  260. for arr_ind, dict_key in key_indexes[:-1]:
  261. if dict_key and isinstance(data, dict):
  262. data = data.get(dict_key, None)
  263. elif (arr_ind and isinstance(data, list) and
  264. int(arr_ind) <= len(data) - 1):
  265. data = data[int(arr_ind)]
  266. else:
  267. return None
  268. # process last index for remove
  269. # expected list entry
  270. if key_indexes[-1][0]:
  271. if isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: # noqa: E501
  272. del data[int(key_indexes[-1][0])]
  273. return True
  274. # expected dict entry
  275. elif key_indexes[-1][1]:
  276. if isinstance(data, dict):
  277. del data[key_indexes[-1][1]]
  278. return True
  279. @staticmethod
  280. def add_entry(data, key, item=None, sep='.'):
  281. ''' Get an item from a dictionary with key notation a.b.c
  282. d = {'a': {'b': 'c'}}}
  283. key = a#b
  284. return c
  285. '''
  286. if key == '':
  287. pass
  288. elif (not (key and Yedit.valid_key(key, sep)) and
  289. isinstance(data, (list, dict))):
  290. return None
  291. key_indexes = Yedit.parse_key(key, sep)
  292. for arr_ind, dict_key in key_indexes[:-1]:
  293. if dict_key:
  294. if isinstance(data, dict) and dict_key in data and data[dict_key]: # noqa: E501
  295. data = data[dict_key]
  296. continue
  297. elif data and not isinstance(data, dict):
  298. raise YeditException("Unexpected item type found while going through key " +
  299. "path: {} (at key: {})".format(key, dict_key))
  300. data[dict_key] = {}
  301. data = data[dict_key]
  302. elif (arr_ind and isinstance(data, list) and
  303. int(arr_ind) <= len(data) - 1):
  304. data = data[int(arr_ind)]
  305. else:
  306. raise YeditException("Unexpected item type found while going through key path: {}".format(key))
  307. if key == '':
  308. data = item
  309. # process last index for add
  310. # expected list entry
  311. elif key_indexes[-1][0] and isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: # noqa: E501
  312. data[int(key_indexes[-1][0])] = item
  313. # expected dict entry
  314. elif key_indexes[-1][1] and isinstance(data, dict):
  315. data[key_indexes[-1][1]] = item
  316. # didn't add/update to an existing list, nor add/update key to a dict
  317. # so we must have been provided some syntax like a.b.c[<int>] = "data" for a
  318. # non-existent array
  319. else:
  320. raise YeditException("Error adding to object at path: {}".format(key))
  321. return data
  322. @staticmethod
  323. def get_entry(data, key, sep='.'):
  324. ''' Get an item from a dictionary with key notation a.b.c
  325. d = {'a': {'b': 'c'}}}
  326. key = a.b
  327. return c
  328. '''
  329. if key == '':
  330. pass
  331. elif (not (key and Yedit.valid_key(key, sep)) and
  332. isinstance(data, (list, dict))):
  333. return None
  334. key_indexes = Yedit.parse_key(key, sep)
  335. for arr_ind, dict_key in key_indexes:
  336. if dict_key and isinstance(data, dict):
  337. data = data.get(dict_key, None)
  338. elif (arr_ind and isinstance(data, list) and
  339. int(arr_ind) <= len(data) - 1):
  340. data = data[int(arr_ind)]
  341. else:
  342. return None
  343. return data
  344. @staticmethod
  345. def _write(filename, contents):
  346. ''' Actually write the file contents to disk. This helps with mocking. '''
  347. tmp_filename = filename + '.yedit'
  348. with open(tmp_filename, 'w') as yfd:
  349. yfd.write(contents)
  350. os.rename(tmp_filename, filename)
  351. def write(self):
  352. ''' write to file '''
  353. if not self.filename:
  354. raise YeditException('Please specify a filename.')
  355. if self.backup and self.file_exists():
  356. shutil.copy(self.filename, self.filename + '.orig')
  357. # Try to set format attributes if supported
  358. try:
  359. self.yaml_dict.fa.set_block_style()
  360. except AttributeError:
  361. pass
  362. # Try to use RoundTripDumper if supported.
  363. try:
  364. Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
  365. except AttributeError:
  366. Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))
  367. return (True, self.yaml_dict)
  368. def read(self):
  369. ''' read from file '''
  370. # check if it exists
  371. if self.filename is None or not self.file_exists():
  372. return None
  373. contents = None
  374. with open(self.filename) as yfd:
  375. contents = yfd.read()
  376. return contents
  377. def file_exists(self):
  378. ''' return whether file exists '''
  379. if os.path.exists(self.filename):
  380. return True
  381. return False
  382. def load(self, content_type='yaml'):
  383. ''' return yaml file '''
  384. contents = self.read()
  385. if not contents and not self.content:
  386. return None
  387. if self.content:
  388. if isinstance(self.content, dict):
  389. self.yaml_dict = self.content
  390. return self.yaml_dict
  391. elif isinstance(self.content, str):
  392. contents = self.content
  393. # check if it is yaml
  394. try:
  395. if content_type == 'yaml' and contents:
  396. # Try to set format attributes if supported
  397. try:
  398. self.yaml_dict.fa.set_block_style()
  399. except AttributeError:
  400. pass
  401. # Try to use RoundTripLoader if supported.
  402. try:
  403. self.yaml_dict = yaml.safe_load(contents, yaml.RoundTripLoader)
  404. except AttributeError:
  405. self.yaml_dict = yaml.safe_load(contents)
  406. # Try to set format attributes if supported
  407. try:
  408. self.yaml_dict.fa.set_block_style()
  409. except AttributeError:
  410. pass
  411. elif content_type == 'json' and contents:
  412. self.yaml_dict = json.loads(contents)
  413. except yaml.YAMLError as err:
  414. # Error loading yaml or json
  415. raise YeditException('Problem with loading yaml file. %s' % err)
  416. return self.yaml_dict
  417. def get(self, key):
  418. ''' get a specified key'''
  419. try:
  420. entry = Yedit.get_entry(self.yaml_dict, key, self.separator)
  421. except KeyError:
  422. entry = None
  423. return entry
  424. def pop(self, path, key_or_item):
  425. ''' remove a key, value pair from a dict or an item for a list'''
  426. try:
  427. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  428. except KeyError:
  429. entry = None
  430. if entry is None:
  431. return (False, self.yaml_dict)
  432. if isinstance(entry, dict):
  433. # AUDIT:maybe-no-member makes sense due to fuzzy types
  434. # pylint: disable=maybe-no-member
  435. if key_or_item in entry:
  436. entry.pop(key_or_item)
  437. return (True, self.yaml_dict)
  438. return (False, self.yaml_dict)
  439. elif isinstance(entry, list):
  440. # AUDIT:maybe-no-member makes sense due to fuzzy types
  441. # pylint: disable=maybe-no-member
  442. ind = None
  443. try:
  444. ind = entry.index(key_or_item)
  445. except ValueError:
  446. return (False, self.yaml_dict)
  447. entry.pop(ind)
  448. return (True, self.yaml_dict)
  449. return (False, self.yaml_dict)
  450. def delete(self, path):
  451. ''' remove path from a dict'''
  452. try:
  453. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  454. except KeyError:
  455. entry = None
  456. if entry is None:
  457. return (False, self.yaml_dict)
  458. result = Yedit.remove_entry(self.yaml_dict, path, self.separator)
  459. if not result:
  460. return (False, self.yaml_dict)
  461. return (True, self.yaml_dict)
  462. def exists(self, path, value):
  463. ''' check if value exists at path'''
  464. try:
  465. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  466. except KeyError:
  467. entry = None
  468. if isinstance(entry, list):
  469. if value in entry:
  470. return True
  471. return False
  472. elif isinstance(entry, dict):
  473. if isinstance(value, dict):
  474. rval = False
  475. for key, val in value.items():
  476. if entry[key] != val:
  477. rval = False
  478. break
  479. else:
  480. rval = True
  481. return rval
  482. return value in entry
  483. return entry == value
  484. def append(self, path, value):
  485. '''append value to a list'''
  486. try:
  487. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  488. except KeyError:
  489. entry = None
  490. if entry is None:
  491. self.put(path, [])
  492. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  493. if not isinstance(entry, list):
  494. return (False, self.yaml_dict)
  495. # AUDIT:maybe-no-member makes sense due to loading data from
  496. # a serialized format.
  497. # pylint: disable=maybe-no-member
  498. entry.append(value)
  499. return (True, self.yaml_dict)
  500. # pylint: disable=too-many-arguments
  501. def update(self, path, value, index=None, curr_value=None):
  502. ''' put path, value into a dict '''
  503. try:
  504. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  505. except KeyError:
  506. entry = None
  507. if isinstance(entry, dict):
  508. # AUDIT:maybe-no-member makes sense due to fuzzy types
  509. # pylint: disable=maybe-no-member
  510. if not isinstance(value, dict):
  511. raise YeditException('Cannot replace key, value entry in ' +
  512. 'dict with non-dict type. value=[%s] [%s]' % (value, type(value))) # noqa: E501
  513. entry.update(value)
  514. return (True, self.yaml_dict)
  515. elif isinstance(entry, list):
  516. # AUDIT:maybe-no-member makes sense due to fuzzy types
  517. # pylint: disable=maybe-no-member
  518. ind = None
  519. if curr_value:
  520. try:
  521. ind = entry.index(curr_value)
  522. except ValueError:
  523. return (False, self.yaml_dict)
  524. elif index is not None:
  525. ind = index
  526. if ind is not None and entry[ind] != value:
  527. entry[ind] = value
  528. return (True, self.yaml_dict)
  529. # see if it exists in the list
  530. try:
  531. ind = entry.index(value)
  532. except ValueError:
  533. # doesn't exist, append it
  534. entry.append(value)
  535. return (True, self.yaml_dict)
  536. # already exists, return
  537. if ind is not None:
  538. return (False, self.yaml_dict)
  539. return (False, self.yaml_dict)
  540. def put(self, path, value):
  541. ''' put path, value into a dict '''
  542. try:
  543. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  544. except KeyError:
  545. entry = None
  546. if entry == value:
  547. return (False, self.yaml_dict)
  548. # deepcopy didn't work
  549. # Try to use ruamel.yaml and fallback to pyyaml
  550. try:
  551. tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict,
  552. default_flow_style=False),
  553. yaml.RoundTripLoader)
  554. except AttributeError:
  555. tmp_copy = copy.deepcopy(self.yaml_dict)
  556. # set the format attributes if available
  557. try:
  558. tmp_copy.fa.set_block_style()
  559. except AttributeError:
  560. pass
  561. result = Yedit.add_entry(tmp_copy, path, value, self.separator)
  562. if not result:
  563. return (False, self.yaml_dict)
  564. self.yaml_dict = tmp_copy
  565. return (True, self.yaml_dict)
  566. def create(self, path, value):
  567. ''' create a yaml file '''
  568. if not self.file_exists():
  569. # deepcopy didn't work
  570. # Try to use ruamel.yaml and fallback to pyyaml
  571. try:
  572. tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict,
  573. default_flow_style=False),
  574. yaml.RoundTripLoader)
  575. except AttributeError:
  576. tmp_copy = copy.deepcopy(self.yaml_dict)
  577. # set the format attributes if available
  578. try:
  579. tmp_copy.fa.set_block_style()
  580. except AttributeError:
  581. pass
  582. result = Yedit.add_entry(tmp_copy, path, value, self.separator)
  583. if result:
  584. self.yaml_dict = tmp_copy
  585. return (True, self.yaml_dict)
  586. return (False, self.yaml_dict)
  587. @staticmethod
  588. def get_curr_value(invalue, val_type):
  589. '''return the current value'''
  590. if invalue is None:
  591. return None
  592. curr_value = invalue
  593. if val_type == 'yaml':
  594. curr_value = yaml.load(invalue)
  595. elif val_type == 'json':
  596. curr_value = json.loads(invalue)
  597. return curr_value
  598. @staticmethod
  599. def parse_value(inc_value, vtype=''):
  600. '''determine value type passed'''
  601. true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE',
  602. 'on', 'On', 'ON', ]
  603. false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE',
  604. 'off', 'Off', 'OFF']
  605. # It came in as a string but you didn't specify value_type as string
  606. # we will convert to bool if it matches any of the above cases
  607. if isinstance(inc_value, str) and 'bool' in vtype:
  608. if inc_value not in true_bools and inc_value not in false_bools:
  609. raise YeditException('Not a boolean type. str=[%s] vtype=[%s]' % (inc_value, vtype))
  610. elif isinstance(inc_value, bool) and 'str' in vtype:
  611. inc_value = str(inc_value)
  612. # There is a special case where '' will turn into None after yaml loading it so skip
  613. if isinstance(inc_value, str) and inc_value == '':
  614. pass
  615. # If vtype is not str then go ahead and attempt to yaml load it.
  616. elif isinstance(inc_value, str) and 'str' not in vtype:
  617. try:
  618. inc_value = yaml.load(inc_value)
  619. except Exception:
  620. raise YeditException('Could not determine type of incoming ' +
  621. 'value. value=[%s] vtype=[%s]'
  622. % (type(inc_value), vtype))
  623. return inc_value
  624. @staticmethod
  625. def process_edits(edits, yamlfile):
  626. '''run through a list of edits and process them one-by-one'''
  627. results = []
  628. for edit in edits:
  629. value = Yedit.parse_value(edit['value'], edit.get('value_type', ''))
  630. if 'action' in edit and edit['action'] == 'update':
  631. # pylint: disable=line-too-long
  632. curr_value = Yedit.get_curr_value(Yedit.parse_value(edit.get('curr_value', None)), # noqa: E501
  633. edit.get('curr_value_format', None)) # noqa: E501
  634. rval = yamlfile.update(edit['key'],
  635. value,
  636. edit.get('index', None),
  637. curr_value)
  638. elif 'action' in edit and edit['action'] == 'append':
  639. rval = yamlfile.append(edit['key'], value)
  640. else:
  641. rval = yamlfile.put(edit['key'], value)
  642. if rval[0]:
  643. results.append({'key': edit['key'], 'edit': rval[1]})
  644. return {'changed': len(results) > 0, 'results': results}
  645. # pylint: disable=too-many-return-statements,too-many-branches
  646. @staticmethod
  647. def run_ansible(params):
  648. '''perform the idempotent crud operations'''
  649. yamlfile = Yedit(filename=params['src'],
  650. backup=params['backup'],
  651. separator=params['separator'])
  652. state = params['state']
  653. if params['src']:
  654. rval = yamlfile.load()
  655. if yamlfile.yaml_dict is None and state != 'present':
  656. return {'failed': True,
  657. 'msg': 'Error opening file [%s]. Verify that the ' +
  658. 'file exists, that it is has correct' +
  659. ' permissions, and is valid yaml.'}
  660. if state == 'list':
  661. if params['content']:
  662. content = Yedit.parse_value(params['content'], params['content_type'])
  663. yamlfile.yaml_dict = content
  664. if params['key']:
  665. rval = yamlfile.get(params['key']) or {}
  666. return {'changed': False, 'result': rval, 'state': state}
  667. elif state == 'absent':
  668. if params['content']:
  669. content = Yedit.parse_value(params['content'], params['content_type'])
  670. yamlfile.yaml_dict = content
  671. if params['update']:
  672. rval = yamlfile.pop(params['key'], params['value'])
  673. else:
  674. rval = yamlfile.delete(params['key'])
  675. if rval[0] and params['src']:
  676. yamlfile.write()
  677. return {'changed': rval[0], 'result': rval[1], 'state': state}
  678. elif state == 'present':
  679. # check if content is different than what is in the file
  680. if params['content']:
  681. content = Yedit.parse_value(params['content'], params['content_type'])
  682. # We had no edits to make and the contents are the same
  683. if yamlfile.yaml_dict == content and \
  684. params['value'] is None:
  685. return {'changed': False, 'result': yamlfile.yaml_dict, 'state': state}
  686. yamlfile.yaml_dict = content
  687. # If we were passed a key, value then
  688. # we enapsulate it in a list and process it
  689. # Key, Value passed to the module : Converted to Edits list #
  690. edits = []
  691. _edit = {}
  692. if params['value'] is not None:
  693. _edit['value'] = params['value']
  694. _edit['value_type'] = params['value_type']
  695. _edit['key'] = params['key']
  696. if params['update']:
  697. _edit['action'] = 'update'
  698. _edit['curr_value'] = params['curr_value']
  699. _edit['curr_value_format'] = params['curr_value_format']
  700. _edit['index'] = params['index']
  701. elif params['append']:
  702. _edit['action'] = 'append'
  703. edits.append(_edit)
  704. elif params['edits'] is not None:
  705. edits = params['edits']
  706. if edits:
  707. results = Yedit.process_edits(edits, yamlfile)
  708. # if there were changes and a src provided to us we need to write
  709. if results['changed'] and params['src']:
  710. yamlfile.write()
  711. return {'changed': results['changed'], 'result': results['results'], 'state': state}
  712. # no edits to make
  713. if params['src']:
  714. # pylint: disable=redefined-variable-type
  715. rval = yamlfile.write()
  716. return {'changed': rval[0],
  717. 'result': rval[1],
  718. 'state': state}
  719. return {'failed': True, 'msg': 'Unkown state passed'}
  720. # -*- -*- -*- End included fragment: class/yedit.py -*- -*- -*-
  721. # -*- -*- -*- Begin included fragment: ansible/yedit.py -*- -*- -*-
  722. # pylint: disable=too-many-branches
  723. def main():
  724. ''' ansible oc module for secrets '''
  725. module = AnsibleModule(
  726. argument_spec=dict(
  727. state=dict(default='present', type='str',
  728. choices=['present', 'absent', 'list']),
  729. debug=dict(default=False, type='bool'),
  730. src=dict(default=None, type='str'),
  731. content=dict(default=None),
  732. content_type=dict(default='dict', choices=['dict']),
  733. key=dict(default='', type='str'),
  734. value=dict(),
  735. value_type=dict(default='', type='str'),
  736. update=dict(default=False, type='bool'),
  737. append=dict(default=False, type='bool'),
  738. index=dict(default=None, type='int'),
  739. curr_value=dict(default=None, type='str'),
  740. curr_value_format=dict(default='yaml',
  741. choices=['yaml', 'json', 'str'],
  742. type='str'),
  743. backup=dict(default=True, type='bool'),
  744. separator=dict(default='.', type='str'),
  745. edits=dict(default=None, type='list'),
  746. ),
  747. mutually_exclusive=[["curr_value", "index"], ['update', "append"]],
  748. required_one_of=[["content", "src"]],
  749. )
  750. rval = Yedit.run_ansible(module.params)
  751. if 'failed' in rval and rval['failed']:
  752. module.fail_json(**rval)
  753. module.exit_json(**rval)
  754. if __name__ == '__main__':
  755. main()
  756. # -*- -*- -*- End included fragment: ansible/yedit.py -*- -*- -*-