Ansible: Check if my user exists on remote host, else use root user to connect with ssh

Question:

I’m currently writting an Ansible script which should update openssl on every host running Debian or CentOS. On the hosts our SSH-Keys are deposited for my own user or root. I want to check if my user is existing on the host, if not I want to authenticate with the root user. Is there a possibility to do this? I tried it with a bash command but I want to check if my user exists before I’m running the tasks. Maybe there are other solutions to my problem but I don’t know them. Running this playbook throws a syntax error. My Script looks like this right now:

Answer:

You can test the connection with local_action first.
Ansible need to know how to connect to the host for sure, otherwise it will trigger host unreachable error and skip remaining tasks for that host.
Something like this:

Leave a Reply