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:
---
- hosts: "{{ host_group }}"
remote_user: "{{ username }}"
tasks:
# Check whether there's a existinig user or whether you have to use root
- name: Check whether there's your user on the machine
action: shell /usr/bin/getent passwd $username | /usr/bin/wc -l | tr -d ''
register: user_exist
remote_user: root
when: user_exist.stdout == 0
tags:
- users
# Install openssl on Ubuntu or Debian
- name: Install openssl on Ubuntu or Debian
become: True
become_user: root
apt: name=openssl state=latest
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'
# Install openssl on CentOS or RHEL
- name: Install openssl on CentOS or RHEL
become: True
become_user: root
yum: name=openssl state=latest
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'