Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add item to list, creating list on demand #96

Open
jdmansour opened this issue Oct 6, 2022 · 0 comments
Open

Add item to list, creating list on demand #96

jdmansour opened this issue Oct 6, 2022 · 0 comments

Comments

@jdmansour
Copy link

jdmansour commented Oct 6, 2022

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:

if entry is None:
self.put(path, [])
entry = Yedit.get_entry(self.yaml_dict, path, self.separator)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant