Bootstrap FreeKB - Ansible - Edit YAML file using kwoodson.yedit
Ansible - Edit YAML file using kwoodson.yedit

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

 

The kwoodson.yedit role can be used to add, change or remove certain keys / values from YAML. You probably don't have the role installed so you'll probably want to use the ansible-galaxy install command to install the kwoodson.yedit role.

ansible-galaxy install kwoodson.yedit

 

Underneath the hood, yedit uses Python ruamel.yaml

In this exmaple, the YAML file will be checked to see if the "bar" key exists below the "domains" key.

---
- hosts: localhost
  roles:
  - kwoodson.yedit
  tasks:
  - name: edit example
    yedit:
      src: /path/to/example.yml
      separator: ':'
      key: domains:foo:alias
      value: hello
    register: out

  - debug:
      var: out
...

 

Something like this should be returned. In this example, result is null since there is no key named "bar" below the "domains" key.

ok: [localhost] => {
    "out": {
        "changed": false, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "item", 
                "changed": false, 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "append": false, 
                        "backup": false, 
                        "backup_ext": ".20230825T023510", 
                        "content": null, 
                        "content_type": "yaml", 
                        "curr_value": null, 
                        "curr_value_format": "yaml", 
                        "debug": false, 
                        "edits": null, 
                        "index": null, 
                        "insert": false, 
                        "key": "was_hosts:dlcmwas002", 
                        "separator": ":", 
                        "src": "/path/to/example.yml", 
                        "state": "list", 
                        "update": false, 
                        "value": null, 
                        "value_type": ""
                    }
                }, 
                "item": "bar", 
                "result": null, 
                "state": "list"
            }
        ]
    }
}

 

In this example, result is not null since there is a key named "bar.example.com" below the "domains" key.

ok: [localhost] => {
    "yeditlist": {
        "changed": false, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "item", 
                "changed": false, 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "append": false, 
                        "backup": false, 
                        "backup_ext": ".20230825T023824", 
                        "content": null, 
                        "content_type": "yaml", 
                        "curr_value": null, 
                        "curr_value_format": "yaml", 
                        "debug": false, 
                        "edits": null, 
                        "index": null, 
                        "insert": false, 
                        "key": "domain:bar.example.com", 
                        "separator": ":", 
                        "src": "/path/to/example.yml", 
                        "state": "list", 
                        "update": false, 
                        "value": null, 
                        "value_type": ""
                    }
                }, 
                "item": "bar.example.com", 
                "result": {
                    "alias": "bar",
                    "region": "us-east-1"
                },
                "state": "list"
            }
        ]
    }
}

 

In this example, the domains:bar.example.com:alias value will be changed from "bar" to "hello".

---
- hosts: localhost
  roles:
  - kwoodson.yedit
  tasks:
  - name: edit example
    yedit:
      src: /path/to/example.yml
      separator: ':'
      key: domains:foo:alias
      value: hello
...

 

In this example, the domains:new.example.com:alias key will be created with a value of "world".

---
- hosts: localhost
  roles:
  - kwoodson.yedit
  tasks:
  - name: create new.example.com
    yedit:
      src: /path/to/example.yml
      separator: ':'
      key: domains:new.example.com:alias
      value: world
...

 

In this example, the domains:bar.example.com:alia key will be removed from the YAML file.

---
- hosts: localhost
  roles:
  - kwoodson.yedit
  tasks:
  - name: remove bar.example.com
    yedit:
      src: /path/to/example.yml
      separator: ':'
      key: domains:bar.example.com
      state: absent
...

 




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 f418dd in the box below so that we can be sure you are a human.