How to incorporate password along with passwordless login in ansible dynamic inventory

I give the end user to either provide username i.e dest_user for which ssh-keys is already setup on Linux for all destination hosts i.e dest_host.

Below is how i setup dynamic inventory for passwordless user

  • add_host:name: “{{ item }}”
    groups: dest_nodes
    ansible_user: “{{ dest_user }}”
    with_items: “{{ dest_host.split() }}”
    when: dest_user != “”

All good. The second scenario is when the user does not provide any dest_user then his SSO username (i get this from thier Jenkins login) and password i.e dest_pass which he enters manually should be used for all hosts in the dynamic inventory.

How can i construct dest_nodes group to accomodate both username for passwordless login and both username-password?