i got it to work the simple way with multiple tasks
---
- name: test
hosts: k8s
tasks:
- name: comment line
lineinfile:
line: "#image: graylog/graylog4.2.5"
regexp: '^image:\sgraylog/graylog4.2.5'
path: /tmp/temp2.txt
- name: add line
lineinfile:
line: "image: graylog/graylog4.2.8"
path: /tmp/temp2.txt
create: true
probably not the most efficient.
On Thu, May 5, 2022 at 9:50 PM Vladimir Botka <[email protected]> wrote:
> > shell> cat pb.yml
> > - hosts: localhost
> > vars:
> > versions:
> > 4.2.5: false
> > 4.2.8: true
> > tasks:
> > - name: Remove versions
> > lineinfile:
> > state: absent
> > path: docker-compose
> > regexp: '^\s*#*\s*image: graylog/graylog{{ item.key }}\s*$'
> > loop: "{{ versions|dict2items }}"
> > when: force_remove_versions|d(false)|bool
> > - name: Add versions
> > lineinfile:
> > path: docker-compose
> > regexp: '^\s*#*\s*image: graylog/graylog{{ item.key }}\s*$'
> > line: '{{ hash }}image: graylog/graylog{{ item.key }}'
> > loop: "{{ versions|dict2items }}"
> > vars:
> > hash: "{{ item.value|ternary('', '#') }}"
>
> The playbook is idempotent
>
> shell> cat docker-compose
> #image: graylog/graylog4.2.5
> image: graylog/graylog4.2.8
>
>
> shell> ansible-playbook pb.yml
>
> PLAY [localhost]
> ****************************************************************
>
> TASK [Remove versions]
> ****************************************************************
> skipping: [localhost] => (item={'key': '4.2.5', 'value': False})
> skipping: [localhost] => (item={'key': '4.2.8', 'value': True})
>
> TASK [Add versions]
> ****************************************************************
> ok: [localhost] => (item={'key': '4.2.5', 'value': False})
> ok: [localhost] => (item={'key': '4.2.8', 'value': True})
>
> PLAY RECAP
> ****************************************************************
> localhost: ok=1 changed=0 unreachable=0
> failed=0 skipped=1 rescued=0 ignored=0
>
>
> --
> Vladimir Botka
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CALmkhkoDJAE6O%2BrjFu5gWHY%2BkZQ9xT_cW6s71WQPHzPW0Ww01A%40mail.gmail.com.