Assuming you only have one line for each version, it will assure that the line for any version that's marked false will be commented, and any line for a version marked true will be uncommented.
Where it gets hairy is when you have more than one line for any version. In that case, the second task only manages the first of the lines for that version. That shouldn't happen, but you've already seen that it does sometimes. When it does, you can run the playbook with `force_remove_versions` set to true, and it will remove all the matching lines - both good and bad - before the second task puts the single entries back in the file, properly commented or not as the case may be. On Friday, May 6, 2022 at 4:36:29 PM UTC-4 [email protected] wrote: > does this mean I cannot keep the old commented line? > > #image: graylog/graylog4.2.5 > > requirement is I need to have this line commented > > 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/45720724-972c-4963-81b8-0907ecb2646bn%40googlegroups.com.
