---
- name: Create Non-Root User
  hosts: all:!localhost:!hypervisor
  gather_facts: no
  any_errors_fatal: true
  vars:
      # Password isn't exposed by by default
      non_root_pw: "{{ provision.topology.password | default('redhat') }}"
  tasks:
    - name: wait for hosts to be reachable
      wait_for:
          port: 22
          host: "{{ ansible_ssh_host }}"
          search_regex: OpenSSH
      delegate_to: hypervisor
      tags: vms

    - name: create non root user
      user:
          name: "{{ provision.topology.username }}"
          state: present
          password: "{{ non_root_pw | password_hash('sha512') }}"

    - name: add user to sudoers
      lineinfile:
          dest: "/etc/sudoers"
          line: "{{ provision.topology.username }} ALL=(root) NOPASSWD:ALL"

    - name: Set up authorized_keys for non root user
      authorized_key:
          user: "{{ provision.topology.username }}"
          key: "{{ item }}"
      with_file:
          - "{{ inventory_dir }}/id_rsa.pub"

    - name: Update hosts user
      add_host:
          name: "{{ inventory_hostname }}"
          ansible_ssh_user: "{{ provision.topology.username }}"

---
- name: Create Non-Root User
  hosts: all:!localhost:!hypervisor
  gather_facts: no
  any_errors_fatal: true
  vars:
      # Password isn't exposed by by default
      non_root_pw: "{{ provision.topology.password | default('redhat') }}"
  tasks:
    - name: wait for hosts to be reachable
      wait_for:
          port: 22
          host: "{{ ansible_ssh_host }}"
          search_regex: OpenSSH
      delegate_to: hypervisor
      tags: vms

    - name: create non root user
      user:
          name: "{{ provision.topology.username }}"
          state: present
          password: "{{ non_root_pw | password_hash('sha512') }}"

    - name: add user to sudoers
      lineinfile:
          dest: "/etc/sudoers"
          line: "{{ provision.topology.username }} ALL=(root) NOPASSWD:ALL"

    - name: Set up authorized_keys for non root user
      authorized_key:
          user: "{{ provision.topology.username }}"
          key: "{{ item }}"
      with_file:
          - "{{ inventory_dir }}/id_rsa.pub"

    - name: Update hosts user
      add_host:
          name: "{{ item }}"
          ansible_ssh_user: "{{ provision.topology.username }}"
      with_items: "{{ ansible_play_hosts }}"

Fix virsh usage of the non-root user
Fix virsh usage of the non-root user

Ensures that ALL the virsh provisioned
hosts will have non-root ssh user in the
ansible inventory

Change-Id: Ia4291298465d7d12c70cc4018603aa2d092de1c3
