|
| 1 | +--- |
| 2 | +- name: Install tools |
| 3 | + package: |
| 4 | + name: "{{ item }}" |
| 5 | + state: present |
| 6 | + ignore_errors: true |
| 7 | + loop: |
| 8 | + - sshpass |
| 9 | + - openssh |
| 10 | + - openssh-clients |
| 11 | + - openssh-server |
| 12 | + |
| 13 | +- name: Allow password Login for sshd |
| 14 | + ansible.builtin.lineinfile: |
| 15 | + path: /etc/ssh/sshd_config |
| 16 | + search_string: PasswordAuthentication no |
| 17 | + line: PasswordAuthentication yes |
| 18 | + when: |
| 19 | + - ansible_facts.distribution == "Amazon" |
| 20 | + |
| 21 | +- name: Start sshd service |
| 22 | + ansible.builtin.service: |
| 23 | + name: "{{ item }}" |
| 24 | + state: started |
| 25 | + ignore_errors: true |
| 26 | + loop: |
| 27 | + - sshd |
| 28 | + - ssh |
| 29 | + |
| 30 | +- name: Set password for test |
| 31 | + ansible.builtin.set_fact: |
| 32 | + test_pw: myTest!pw |
| 33 | + |
| 34 | +- name: Create locked_user |
| 35 | + user: |
| 36 | + name: locked_user |
| 37 | + password: "{{ test_pw | password_hash('sha512') }}" |
| 38 | + |
| 39 | +- name: Create ssh-client-keypair |
| 40 | + community.crypto.openssh_keypair: |
| 41 | + path: /root/locked_user_id |
| 42 | + type: ed25519 |
| 43 | + state: present |
| 44 | + register: generated_key |
| 45 | + |
| 46 | +- name: Add ssh-public-key to locked_user |
| 47 | + ansible.posix.authorized_key: |
| 48 | + user: locked_user |
| 49 | + key: "{{ generated_key.public_key }}" |
| 50 | + state: present |
| 51 | + |
| 52 | +- name: Check successful login with password |
| 53 | + ansible.builtin.shell: |
| 54 | + cmd: sshpass -p {{ test_pw }} ssh -o StrictHostKeyChecking=no locked_user@localhost echo "success" |
| 55 | + |
| 56 | +- name: Check successful login with ssh key |
| 57 | + ansible.builtin.shell: |
| 58 | + cmd: ssh -i /root/locked_user_id -o StrictHostKeyChecking=no locked_user@localhost echo "success" |
| 59 | + |
| 60 | +- name: Set password change date for locked_user |
| 61 | + ansible.builtin.shell: |
| 62 | + cmd: chage -d 2020-01-01 locked_user |
| 63 | + |
| 64 | +- name: Check unsuccessful login with password |
| 65 | + ansible.builtin.shell: |
| 66 | + cmd: sshpass -p {{ test_pw }} ssh -o StrictHostKeyChecking=no locked_user@localhost echo "success" |
| 67 | + register: output |
| 68 | + ignore_errors: true |
| 69 | + |
| 70 | +- name: Assert check unsuccessful login |
| 71 | + ansible.builtin.assert: |
| 72 | + that: |
| 73 | + - output.rc | int == 1 |
| 74 | + - "'WARNING: Your password has expired.' in output.stderr" |
| 75 | + - "'success' not in output.stdout" |
| 76 | + when: |
| 77 | + - ansible_facts.os_family != "Suse" |
| 78 | + |
| 79 | +- name: Assert check unsuccessful login |
| 80 | + ansible.builtin.assert: |
| 81 | + that: |
| 82 | + - output.rc | int == 5 |
| 83 | + - output.stderr | length == 0 |
| 84 | + - output.stdout | length == 0 |
| 85 | + when: |
| 86 | + - ansible_facts.os_family == "Suse" |
| 87 | + |
| 88 | +- name: Check successful login with ssh key |
| 89 | + ansible.builtin.shell: |
| 90 | + cmd: ssh -i /root/locked_user_id -o StrictHostKeyChecking=no locked_user@localhost echo "success" |
0 commit comments