Bootstrap FreeKB - Ansible - Parse YAML using from_yaml
Ansible - Parse YAML using from_yaml

Updated:   |  Ansible articles

Let's say you have a YAML file, perhaps something like this.

domains:
  foo:
    alias: foo
    region: us-east-1
  bar.example.com:
    alias: bar
    region: us-east-2

 

If you parse the YAML file using the shell or command or slurp modules.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.yml
    register: slurp

  - debug:
      var: slurp.content | b64decode
...

 

You'll probably get something like this returned.

ok: [localhost] => {
    "slurp.content | b64decode": "domains:\n  foo:\n    alias: foo\n    region: us-east-1\n  bar.example.com:\n    alias: bar\n    region: us-east-2\n"
}

 

from_yaml can be used to return the YAML as a dictionary of structured data.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.yml
    register: slurp

  - debug:
      var: slurp.content | b64decode | from_yaml
...

 

Which should now return something like this.

ok: [localhost] => {
    "slurp.content | b64decode | from_yaml": {
        "domains": {
            "bar.example.com": {
                "alias": "bar", 
                "region": "us-east-2"
            }, 
            "foo": {
                "alias": "foo", 
                "region": "us-east-1"
            }
        }
    }
}

 

And here is how to return the data below the "domains" key.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.yml
    register: slurp

  - debug:
      var: (slurp.content | b64decode | from_yaml).domains
...

 

Or like this.

- debug:
    var: (slurp.content | b64decode | from_yaml)['domains']

 

Which should return the following.

ok: [localhost] => {
    "(slurp.content | b64decode | from_yaml).domains": {
        "bar.example.com": {
            "alias": "bar", 
            "region": "us-east-2"
        }, 
        "foo": {
            "alias": "foo", 
            "region": "us-east-1"
        }
    }
}

 

And here is how to return the data below the "foo" key.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.yml
    register: slurp

  - debug:
      var: (slurp.content | b64decode | from_yaml).domains.foo
...

 

Or like this.

- debug:
    var: (slurp.content | b64decode | from_yaml).domains['foo']

 

Or like this.

- debug:
    var: (slurp.content | b64decode | from_yaml)['domains'].foo

 

Or like this.

- debug:
    var: (slurp.content | b64decode | from_yaml)['domains']['foo']

 

Which should return the following.

ok: [localhost] => {
    "(slurp.content | b64decode | from_yaml).domains.foo": {
        "alias": "foo", 
        "region": "us-east-1"
    }
}

 

Since the bar.example.com key contains periods (dot notation), the bar.example.com must be placed inside of single quoted brackets like this.

- debug:
    var: (slurp.content | b64decode | from_yaml)['domains']['bar.example.com']

 

Or like this.

- debug:
    var: (slurp.content | b64decode | from_yaml).domains['bar.example.com']

 

If you were to try something like this.

- debug:
    var: (slurp.content | b64decode | from_yaml).domainsbar.example.com

 

Something like VARIABLE IS NOT DEFINED may be returned.

ok: [localhost] => {
    "(slurp.content | b64decode | from_yaml).domains.bar.example.com": "VARIABLE IS NOT DEFINED!"
}

 

The following can be used to loop through the YAML.

For example, with_items to loop through the keys below the "domain" key.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.yml
    register: slurp

  - debug:
      var: item
    with_items: "{{ (slurp.content | b64decode | from_yaml).domains }}"
...

 

Should return the following, just the keys below the "domain" key.

ok: [localhost] => (item=bar.example.com) => {
    "ansible_loop_var": "item", 
    "item": "bar.example.com"
}
ok: [localhost] => (item=foo) => {
    "ansible_loop_var": "item", 
    "item": "foo"
}

 

with_dict to loop through the "domain" key.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.yml
    register: slurp

  - debug:
      var: item
    with_dict: "{{ (slurp.content | b64decode | from_yaml).domains }}"
...

 

Should return the following, all of the keys and values below the "domain" key.

ok: [localhost] => (item={u'key': u'bar.example.com', u'value': {u'alias': u'bar', u'region': u'us-east-2'}}) => {
    "ansible_loop_var": "item", 
    "item": {
        "key": "bar.example.com", 
        "value": {
            "alias": "bar", 
            "region": "us-east-2"
        }
    }
}
ok: [localhost] => (item={u'key': u'foo', u'value': {u'alias': u'foo', u'region': u'us-east-1'}}) => {
    "ansible_loop_var": "item", 
    "item": {
        "key": "foo", 
        "value": {
            "alias": "foo", 
            "region": "us-east-1"
        }
    }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter eb1ade in the box below so that we can be sure you are a human.