kwoodson / ansible-role-yedit

Ansible Role for Yaml editing
164 stars 62 forks source link

Add item to list, creating list on demand #96

Open jdmansour opened 2 years ago

jdmansour commented 2 years ago

Hi, I would like to ensure that an item is in a list. For that I can use update: yes:

- name: Test
  hosts: [localhost]
  gather_facts: no

  roles:
    - kwoodson.yedit

  tasks:
  - name: Modify YAML
    yedit:
      content:
        foo: bar
        users:
          - alice
      key: users
      value: "bob"
      update: yes
      # append: yes
      state: present
    register: res

  - debug: var=res

However, this does not work when the list 'users' doesn't exist. It just succeeds without doing anything. If I use append: yes instead, it will create the list if neccessary. But then I get duplicates if the item does exist.

I think the source of this difference is that append has the following code, but update doesn't:

https://github.com/kwoodson/ansible-role-yedit/blob/0ae7b9c9803fc8664a62a07bec72b338fe2a0846/library/yedit.py#L622-L624

Could this be added to update, or is there another way to achive this?