What you need it is to split into two lists based on true/false and then sort them in forward order:
- name: "Output"
ansible.builtin.debug:
msg: |
{% for db in databases | selectattr('is_standby','true') | sort(attribute='dbname') %}
{{ db.dbname }}
{% endfor %}
{% for db in databases | selectattr('is_standby','false') | sort(attribute='dbname') %}
{{ db.dbname }}
{% endfor %}
Results
TASK [Output] ***********************************************************************************************
ok: [localhost] => {
"msg": "B_dg\nC_dg\n\nA_dg\n"
}
PLAY RECAP **************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
I wouldn’t off the top of my head know how sorting true/false works. I wouln’ t use sort when there’s only two options.