| 1 |
- name: user generating ssh keys tests |
| 2 |
become: true |
| 3 |
vars: |
| 4 |
home: "{{ (ansible_facts['os_family'] == 'Darwin')|ternary('/Users/ansibulluser/', '/home/ansibulluser/')}}" |
| 5 |
ssh_key_file: .ssh/ansible_test_rsa |
| 6 |
pub_file: '{{ssh_key_file}}.pub' |
| 7 |
key_files: |
| 8 |
- '{{ssh_key_file}}' |
| 9 |
- '{{pub_file}}' |
| 10 |
block: |
| 11 |
- name: Ensure clean/non existing ansibulluser |
| 12 |
user: name=ansibulluser state=absent |
| 13 |
|
| 14 |
- name: Test creating ssh key creation |
| 15 |
block: |
| 16 |
- name: Create user with ssh key |
| 17 |
user: |
| 18 |
name: ansibulluser |
| 19 |
state: present |
| 20 |
generate_ssh_key: yes |
| 21 |
ssh_key_file: '{{ ssh_key_file}}' |
| 22 |
|
| 23 |
- name: check files exist |
| 24 |
stat: |
| 25 |
path: '{{home ~ item}}' |
| 26 |
register: stat_keys |
| 27 |
loop: '{{ key_files }}' |
| 28 |
|
| 29 |
- name: Create user with ssh key (not really) |
| 30 |
check_mode: True |
| 31 |
user: |
| 32 |
name: ansibulluser |
| 33 |
state: present |
| 34 |
generate_ssh_key: yes |
| 35 |
ssh_key_file: '{{ ssh_key_file}}' |
| 36 |
force: yes |
| 37 |
|
| 38 |
- name: check files again, in case of deletion/overwrite in check mode |
| 39 |
stat: |
| 40 |
path: '{{home ~ item}}' |
| 41 |
register: stat_keys2 |
| 42 |
loop: '{{ key_files }}' |
| 43 |
|
| 44 |
- name: ensure they exist and were not changed in check mode |
| 45 |
assert: |
| 46 |
that: |
| 47 |
- stat_keys.results[item].stat.exists |
| 48 |
- stat_keys2.results[item].stat.exists |
| 49 |
- stat_keys.results[item].stat.checksum == stat_keys2.results[item].stat.checksum |
| 50 |
loop: [0, 1] |
| 51 |
|
| 52 |
always: |
| 53 |
- name: Clean ssh keys |
| 54 |
file: path={{ home ~ item }} state=absent |
| 55 |
loop: '{{ key_files }}' |
| 56 |
|
| 57 |
- name: Ensure clean/non existing ansibulluser |
| 58 |
user: name=ansibulluser state=absent |
| 59 |
|
| 60 |
- name: Ensure we don't break on conflicts |
| 61 |
block: |
| 62 |
- name: flag file for test |
| 63 |
tempfile: |
| 64 |
register: flagfile |
| 65 |
|
| 66 |
- name: precreate public .ssh |
| 67 |
file: path={{home ~ '.ssh'}} state=directory |
| 68 |
|
| 69 |
- name: setup keys linked to flag file |
| 70 |
file: path={{home ~ item}} src={{flagfile.path}} state=link |
| 71 |
loop: '{{key_files}}' |
| 72 |
|
| 73 |
- name: Create user with ssh key |
| 74 |
user: |
| 75 |
name: ansibulluser |
| 76 |
state: present |
| 77 |
generate_ssh_key: yes |
| 78 |
ssh_key_file: '{{ ssh_key_file }}' |
| 79 |
ignore_errors: true |
| 80 |
register: user_no_force |
| 81 |
|
| 82 |
- stat: path={{home ~ ssh_key_file}} |
| 83 |
register: check_priv |
| 84 |
|
| 85 |
- stat: path={{home ~ pub_file}} |
| 86 |
register: check_pub |
| 87 |
|
| 88 |
- name: ensure we didn't overwrite |
| 89 |
assert: |
| 90 |
that: |
| 91 |
- check_priv.stat.exists |
| 92 |
- check_priv.stat.islnk |
| 93 |
- check_priv.stat.uid == 0 |
| 94 |
- check_pub.stat.exists |
| 95 |
- check_pub.stat.islnk |
| 96 |
- check_pub.stat.uid == 0 |
| 97 |
|
| 98 |
- name: Create user with ssh key |
| 99 |
user: |
| 100 |
name: ansibulluser |
| 101 |
state: present |
| 102 |
generate_ssh_key: yes |
| 103 |
ssh_key_file: '{{ ssh_key_file }}' |
| 104 |
force: true |
| 105 |
ignore_errors: true |
| 106 |
register: user_force |
| 107 |
|
| 108 |
- stat: path={{home ~ ssh_key_file}} |
| 109 |
register: check_priv2 |
| 110 |
|
| 111 |
- stat: path={{home ~ pub_file}} |
| 112 |
register: check_pub2 |
| 113 |
|
| 114 |
- name: ensure we failed since we didn't force overwrite |
| 115 |
assert: |
| 116 |
that: |
| 117 |
- user_force is success |
| 118 |
- check_priv2.stat.exists |
| 119 |
- not check_priv2.stat.islnk |
| 120 |
- check_pub2.stat.exists |
| 121 |
- not check_pub2.stat.islnk |
| 122 |
- check_pub2.stat.uid != 0 |
| 123 |
always: |
| 124 |
- name: Clean up files |
| 125 |
file: path={{ home ~ item }} state=absent |
| 126 |
loop: '{{ key_files + [flagfile.path] }}' |
| 127 |
|
| 128 |
- name: Ensure clean/non existing ansibulluser |
| 129 |
user: name=ansibulluser state=absent |