- name: enable password auth
  copy:
    src: sshd_config
    dest: /etc/ssh/sshd_config
  notify: restart sshd

- name: ensure libselinux-python
  yum:
    name: libselinux-python 
    update_cache: yes

- name: allow passwordless sudo for user 
  lineinfile:
    dest: /etc/sudoers
    state: present
    line: '{{ username }} ALL=(ALL) NOPASSWD: ALL'

- name: disable requiretty
  lineinfile:
    dest: /etc/sudoers
    state: present
    line: Defaults    !requiretty
    regexp: ^Defaults

- name: disable selinux
  lineinfile:
    line: SELINUX=permissive
    state: present
    dest: /etc/selinux/config
    regexp: ^SELINUX=
  register: selinux_disable

- block:
    - name: reboot server
      shell: sleep 2 && shutdown -r 1 "selinux change...rebooting..."
      async: 0
      poll: 0
      ignore_errors: yes

    - name: wait for instance
      delegate_to: localhost
      wait_for:
        port: 22
        host: '{{ ansible_ssh_host }}'
        search_regex: OpenSSH
        timeout: 500
  when: selinux_disable|changed

- name: enable password auth
  copy:
    src: sshd_config
    dest: /etc/ssh/sshd_config
  notify: restart sshd

- name: ensure libselinux-python
  yum:
    name: libselinux-python 
    update_cache: yes

- name: allow passwordless sudo for user 
  lineinfile:
    dest: /etc/sudoers
    state: present
    line: '{{ username }} ALL=(ALL) NOPASSWD: ALL'

- name: disable requiretty
  lineinfile:
    dest: /etc/sudoers
    state: present
    line: Defaults    !requiretty
    regexp: ^Defaults

- name: disable selinux
  lineinfile:
    line: SELINUX=permissive
    state: present
    dest: /etc/selinux/config
    regexp: ^SELINUX=
  register: selinux_disable

- block:
    - name: reboot server
      shell: sleep 2 && shutdown -r 1 "selinux change...rebooting..."
      async: 0
      poll: 0
      ignore_errors: yes

    - name: wait for instance
      delegate_to: localhost
      wait_for:
        port: 22
        host: '{{ ansible_ssh_host }}'
        search_regex: OpenSSH
        timeout: 500
        delay: 120
  when: selinux_disable|changed

Add delay to wait_for task that checks for host after reboot
Add delay to wait_for task that checks for host after reboot

Without this delay, the system has not yet rebooted when the wait_for task fires. This
meant the playbook would contiune to run with a reboot pending. The host would
then reboot in the middle of a subsequent task, which happened to be yum. Rebooting
in the middle of the yum task creates inconsistencies in the rpm database, causing
subsequest executions of the playbook to fail on the yum task.
