1

Below is my program. When I tried to convert using from_yaml and write back the file, it removing the double quotation with single quotation. Is there a way to resovle it

- slurp:
    src: "/home/myname/config.yml"
  register: fileContent

- set_fact:
    fileContent: "{{ fileContent['content'] | b64decode | from_yaml | combine (myvariable) }}"

- copy:
    content: "{{ fileContent}}"
    dest: "/home/myname/config.yml"

sample config.yml

key:
  key:
    key1: "value"
    key2:
      key1: "value"
      key2: "value"

Also when combine, why the position of element changing. is there a way to maintain the position just change value

navin
  • 55
  • 1
  • 6
  • 1
    Welcome to StackOverflow! Please ensure that you include a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example): Keys in YAML must be unique, you cannot have `key:` two times in the same mapping (lines 3-4 and 5-6 in your config.yml). What is the other file? It also uses YAML syntax so it is obviously not a „program“. It refers to `myname.config.yml`, is that your `config.yml`? Where does `fileContent['content']` come from (it is not in your `config.yml`)? What is the desired and the actual output? – flyx Aug 02 '21 at 14:19
  • Hi. I have only 1 file which is config.yml. I have updated the config file. One of issue is i not quite sure why double quotation is being replaced with single quotation – navin Aug 02 '21 at 14:23
  • 2
    Your problem is probably what is described [here](https://stackoverflow.com/q/60891174/347964) and if so, I don't think you can avoid it at this level. Why is it an issue that the quotation is changed? – flyx Aug 02 '21 at 14:26
  • Hi, the output file need be in double quotation for later processing. Besides why combine the allignment of key value changing. I get the expected result but the order is changing. Any idea? – navin Aug 02 '21 at 14:35
  • 1
    Because declaration order of keys in a yaml dict has absolutely no importance, is not guaranteed to be stable and should not be relied upon. Else use a list. This and quotation changes is a non issue: the resulting data structure is absolutely identical. If your own "later processing" tool does not like that, then you should fix it (or change the data structure). – Zeitounator Aug 02 '21 at 14:55

1 Answers1

0

Q: "Is there a way to maintain the position just change the value?"

A: No. There isn't such a filter available in Ansible which would preserve this format. If you need any special format you'll have to use template.

Except for the changed formatting, your code should be working fine, actually. For example

- hosts: localhost
  tasks:
    - slurp:
        src: config.yml
      register: fileContent
    - set_fact:
        fileContent: "{{ fileContent.content|
                         b64decode|from_yaml|
                         combine(myvariable) }}"
      vars:
        myvariable:
          next_key:
            key1: "value"
            key2:
              key1: "value"
              key2: "value"
    - copy:
        content: "{{ fileContent }}"
        dest: config2.yml

gives valid JSON

shell> cat config2.yml 
{"key": {"key": {"key1": "value", "key2": {"key1": "value", "key2": "value"}}}, "next_key": {"key1": "value", "key2": {"key1": "value", "key2": "value"}}}

The content of the file depends on formating. For example the default JSON above. You can change it to YAML, e.g.

    - copy:
        content: "{{ fileContent|to_nice_yaml }}"
        dest: config2.yml

gives

shell> cat config2.yml 
key:
    key:
        key1: value
        key2:
            key1: value
            key2: value
next_key:
    key1: value
    key2:
        key1: value
        key2: value

or you can get nice JSON by using the filter to_nice_json, e.g.

    - copy:
        content: "{{ fileContent|to_nice_json }}"
        dest: config2.yml

gives

shell> cat config2.yml 
{
    "key": {
        "key": {
            "key1": "value",
            "key2": {
                "key1": "value",
                "key2": "value"
            }
        }
    },
    "next_key": {
        "key1": "value",
        "key2": {
            "key1": "value",
            "key2": "value"
        }
    }
}
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • i notice that double quotation is changed to single quotation due to form_yaml. is there a way to resolve it – navin Aug 03 '21 at 01:19