Hi, To add markers in all of them, i either have the option of lineinefile with regex or this. Regex for such a case look almost impossible. So i have to make this work.
When i print the complete *yamlVars*, it works perfectly fine. Can i not append each key-value pair to a temporary dictionary and print that dictionary ? On Tuesday, 27 February 2018 16:40:47 UTC+1, Matt Martz wrote: > > You are going to have to not use vars files then. Or you will need to > store the data as strings. > > If you don't want formatting to change at all, you can't convert from YAML > to python dict, to YAML, to jinja2+YAML. You will need to find a way to > join them without going through those steps. > > > On Tue, Feb 27, 2018 at 9:34 AM, ishan jain <[email protected] > <javascript:>> wrote: > >> Hi Matt, >> >> Yes, you are thinking in the right direction. Unfortunately it is still >> not working as intended. The indentation generated is crazy. >> >> Basically i want idempotency by maintaining the actual indentation of the >> complete file always. >> >> On Tuesday, 27 February 2018 16:10:12 UTC+1, Matt Martz wrote: >>> >>> I *think* you want the following: >>> >>> {% for key, value in yamlVars.iteritems() %} >>> >>> # BEGIN BLOCK {{ key }} >>> {{ key }}: >>> {{ value | to_nice_yaml | indent }} >>> # END BLOCK {{ key }} >>> >>> {% endfor %} >>> >>> http://jinja.pocoo.org/docs/dev/templates/#indent >>> >>> On Tue, Feb 27, 2018 at 8:54 AM, ishan jain <[email protected]> wrote: >>> >>>> I have a large number of YAML files prepared manually which have a >>>> fixed structure - list of dictionaries. But within different files, >>>> sequence of these keys and their values differ. I need to manage them >>>> automatically, so i am writing a kind of converter to add marker blocks. >>>> But i am stuck at this point: >>>> >>>> >>>> example input YAML file >>>> #something >>>> >>>> keyA: >>>> A1: >>>> - bla >>>> - bla >>>> A2: bla bla >>>> A3: >>>> A3.1: bla >>>> A3.2: bla >>>> >>>> keyA: >>>> C1: >>>> - bla >>>> C2: bla bla >>>> C3: >>>> C3.1: bla >>>> >>>> keyB: >>>> B1: >>>> - bla >>>> - bla >>>> B2: bla bla >>>> B3: >>>> B3.1: bla >>>> B3.2: bla >>>> B3.3: bla >>>> B4: >>>> - bla >>>> - bla >>>> >>>> >>>> playbook to load one of the YAML, and try to write the same variables >>>> again with markers appended >>>> - include_vars: >>>> file: "path to one of the YAML file" >>>> name: yamlVars >>>> >>>> - template: >>>> src: updated.yaml.j2 >>>> dest: "path to that same file" >>>> >>>> >>>> the template >>>> {% for key, value in yamlVars.iteritems() %} >>>> >>>> # BEGIN BLOCK {{ key }} >>>> {{ value | to_nice_yaml }} >>>> # END BLOCK {{ key }} >>>> >>>> {% endfor %} >>>> >>>> >>>> Poblem is, always the contents of each key is printed: >>>> (manually prepared output to hide my variables) >>>> >>>> >>>> actual output >>>> >>>> # BEGIN BLOCK keyA >>>> A1: >>>> - bla >>>> - bla >>>> A2: bla bla >>>> A3: >>>> A3.1: bla >>>> A3.2: bla >>>> # END BLOCK keyA >>>> >>>> >>>> # BEGIN BLOCK keyC >>>> C1: >>>> - bla >>>> C2: bla bla >>>> C3: >>>> C3.1: bla >>>> # END BLOCK keyC >>>> >>>> >>>> # BEGIN BLOCK keyB >>>> B1: >>>> - bla >>>> - bla >>>> B2: bla bla >>>> B3: >>>> B3.1: bla >>>> B3.2: bla >>>> B3.3: bla >>>> B4: >>>> - bla >>>> - bla >>>> # END BLOCK keyB >>>> >>>> >>>> >>>> >>>> expected output >>>> >>>> # BEGIN BLOCK keyA >>>> keyA: >>>> A1: >>>> - bla >>>> - bla >>>> A2: bla bla >>>> A3: >>>> A3.1: bla >>>> A3.2: bla >>>> # END BLOCK keyA >>>> >>>> >>>> # BEGIN BLOCK keyC >>>> keyC: >>>> C1: >>>> - bla >>>> C2: bla bla >>>> C3: >>>> C3.1: bla >>>> # END BLOCK keyC >>>> >>>> >>>> # BEGIN BLOCK keyB >>>> keyB: >>>> B1: >>>> - bla >>>> - bla >>>> B2: bla bla >>>> B3: >>>> B3.1: bla >>>> B3.2: bla >>>> B3.3: bla >>>> B4: >>>> - bla >>>> - bla >>>> # END BLOCK keyB >>>> >>>> >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Ansible Project" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to [email protected]. >>>> To post to this group, send email to [email protected]. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/ansible-project/9a06be57-73b2-4459-88f4-9ffb4a8ef4c6%40googlegroups.com >>>> >>>> <https://groups.google.com/d/msgid/ansible-project/9a06be57-73b2-4459-88f4-9ffb4a8ef4c6%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >>> >>> >>> -- >>> Matt Martz >>> @sivel >>> sivel.net >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ansible Project" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> To post to this group, send email to [email protected] >> <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/ansible-project/235c7a6c-27cd-4e39-90c2-454d57c56db1%40googlegroups.com >> >> <https://groups.google.com/d/msgid/ansible-project/235c7a6c-27cd-4e39-90c2-454d57c56db1%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Matt Martz > @sivel > sivel.net > -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2c04b3b0-fa16-4f13-90f3-773888e565d6%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
