@ -22,52 +22,59 @@
- name : copy an existing file in place with comments
- name : copy an existing file in place with comments
copy : src=existing_authorized_keys dest="{{output_dir|expanduser}}/authorized_keys"
copy:
src : existing_authorized_keys
dest : "{{ output_dir | expanduser }}/authorized_keys"
- name : add multiple keys different order
- name : add multiple keys different order
authorized_key:
authorized_key:
user : root
user : root
key : "{{ multiple_key_different_order_2 }}"
key : "{{ multiple_key_different_order_2 }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : get the file content
- name : get the file content
shell : cat "{{output_dir|expanduser}}/authorized_keys"
shell : cat "{{ output_dir | expanduser }}/authorized_keys"
changed_when : no
register : multiple_keys_existing
register : multiple_keys_existing
- name : assert that the key was added and comments and ordering preserved
- name : assert that the key was added and comments and ordering preserved
assert:
assert:
that:
that:
- 'result.changed == True'
- 'result.changed == True'
- '"# I like candy" in multiple_keys_existing.stdout'
- '"# I like candy" in multiple_keys_existing.stdout'
- '"# I like candy" in multiple_keys_existing.stdout_lines[0]'
- '"# I like candy" in multiple_keys_existing.stdout_lines[0]'
- '"ssh-rsa DATA_BASIC 1@testing" in multiple_keys_existing.stdout'
- '"ssh-rsa DATA_BASIC 1@testing" in multiple_keys_existing.stdout'
# The specific index is a little fragile, but I want to verify the line shows up
# The specific index is a little fragile, but I want to verify the line shows up
# as the 3rd line in the new entries after the existing entries and comments are preserved
# as the 3rd line in the new entries after the existing entries and comments are preserved
- '"ssh-rsa DATA_BASIC 1@testing" in multiple_keys_existing.stdout_lines[7]'
- '"ssh-rsa DATA_BASIC 1@testing" in multiple_keys_existing.stdout_lines[7]'
# start afresh
# start afresh
- name : remove file foo.txt
- name : remove file foo.txt
file : path="{{output_dir|expanduser}}/authorized_keys" state=absent
file:
path : "{{ output_dir | expanduser }}/authorized_keys"
state : absent
- name : touch the authorized_keys file
- name : touch the authorized_keys file
file : dest="{{output_dir}}/authorized_keys" state=touch
file:
dest : "{{ output_dir }}/authorized_keys"
state : touch
register : result
register : result
- name : assert that the authorized_keys file was created
- name : assert that the authorized_keys file was created
assert:
assert:
that:
that:
- 'result.changed == True'
- 'result.changed == True'
- 'result.state == "file"'
- 'result.state == "file"'
- name : add multiple keys
- name : add multiple keys
authorized_key:
authorized_key:
user : root
user : root
key : "{{ multiple_key_base }}"
key : "{{ multiple_key_base }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that the key was added
- name : assert that the key was added
@ -82,7 +89,7 @@
user : root
user : root
key : "{{ multiple_key_different_order }}"
key : "{{ multiple_key_different_order }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that the key was added
- name : assert that the key was added
@ -97,23 +104,23 @@
user : root
user : root
key : "{{ multiple_key_exclusive }}"
key : "{{ multiple_key_exclusive }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
exclusive : true
exclusive : true
register : result
register : result
- name : assert that the key was added
- name : assert that the key was added
assert:
assert:
that:
that:
- 'result.changed == True'
- 'result.changed == True'
- 'result.key == multiple_key_exclusive'
- 'result.key == multiple_key_exclusive'
- 'result.key_options == None'
- 'result.key_options == None'
- name : add multiple keys in different calls
- name : add multiple keys in different calls
authorized_key:
authorized_key:
user : root
user : root
key : "ecdsa-sha2-nistp521 ECDSA_DATA 4@testing"
key : "ecdsa-sha2-nistp521 ECDSA_DATA 4@testing"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : add multiple keys in different calls
- name : add multiple keys in different calls
@ -121,38 +128,40 @@
user : root
user : root
key : "ssh-rsa DATA_BASIC 1@testing"
key : "ssh-rsa DATA_BASIC 1@testing"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : get the file content
- name : get the file content
shell : cat "{{output_dir|expanduser}}/authorized_keys"
shell : cat "{{ output_dir | expanduser }}/authorized_keys"
changed_when : no
register : multiple_keys_at_a_time
register : multiple_keys_at_a_time
- name : assert that the key was added
- name : assert that the key was added
assert:
assert:
that:
that:
- 'result.changed == false'
- 'result.changed == false'
- 'multiple_keys_at_a_time.stdout == multiple_key_exclusive.strip()'
- 'multiple_keys_at_a_time.stdout == multiple_key_exclusive.strip()'
- name : add multiple keys comment
- name : add multiple keys comment
authorized_key:
authorized_key:
user : root
user : root
key : "{{ multiple_keys_comments }}"
key : "{{ multiple_keys_comments }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
exclusive : true
exclusive : true
register : result
register : result
- name : get the file content
- name : get the file content
shell : cat "{{output_dir|expanduser}}/authorized_keys"
shell : cat "{{ output_dir | expanduser }}/authorized_keys"
changed_when : no
register : multiple_keys_comments
register : multiple_keys_comments
- name : assert that the keys exist and comment only lines were not added
- name : assert that the keys exist and comment only lines were not added
assert:
assert:
that:
that:
- 'result.changed == False'
- 'result.changed == False'
- 'multiple_keys_comments.stdout == multiple_key_exclusive.strip()'
- 'multiple_keys_comments.stdout == multiple_key_exclusive.strip()'
- 'result.key_options == None'
- 'result.key_options == None'
@ -160,86 +169,86 @@
# basic ssh-dss key
# basic ssh-dss key
- name : add basic ssh-dss key
- name : add basic ssh-dss key
authorized_key : user=root key="{{ dss_key_basic }}" state=present path="{{ output_dir|expanduser}}/authorized_keys"
authorized_key : user=root key="{{ dss_key_basic }}" state=present path="{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that the key was added
- name : assert that the key was added
assert:
assert:
that:
that:
- 'result.changed == True'
- 'result.changed == True'
- 'result.key == dss_key_basic'
- 'result.key == dss_key_basic'
- 'result.key_options == None'
- 'result.key_options == None'
- name : re-add basic ssh-dss key
- name : re-add basic ssh-dss key
authorized_key : user=root key="{{ dss_key_basic }}" state=present path="{{ output_dir|expanduser}}/authorized_keys"
authorized_key : user=root key="{{ dss_key_basic }}" state=present path="{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that nothing changed
- name : assert that nothing changed
assert:
assert:
that:
that:
- 'result.changed == False'
- 'result.changed == False'
# -------------------------------------------------------------
# -------------------------------------------------------------
# ssh-dss key with an unquoted option
# ssh-dss key with an unquoted option
- name : add ssh-dss key with an unquoted option
- name : add ssh-dss key with an unquoted option
authorized_key :
authorized_key:
user : root
user : root
key : "{{ dss_key_unquoted_option }}"
key : "{{ dss_key_unquoted_option }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that the key was added
- name : assert that the key was added
assert:
assert:
that:
that:
- 'result.changed == True'
- 'result.changed == True'
- 'result.key == dss_key_unquoted_option'
- 'result.key == dss_key_unquoted_option'
- 'result.key_options == None'
- 'result.key_options == None'
- name : re-add ssh-dss key with an unquoted option
- name : re-add ssh-dss key with an unquoted option
authorized_key:
authorized_key:
user : root
user : root
key : "{{ dss_key_unquoted_option }}"
key : "{{ dss_key_unquoted_option }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that nothing changed
- name : assert that nothing changed
assert:
assert:
that:
that:
- 'result.changed == False'
- 'result.changed == False'
# -------------------------------------------------------------
# -------------------------------------------------------------
# ssh-dss key with a leading command="/bin/foo"
# ssh-dss key with a leading command="/bin/foo"
- name : add ssh-dss key with a leading command
- name : add ssh-dss key with a leading command
authorized_key :
authorized_key:
user : root
user : root
key : "{{ dss_key_command }}"
key : "{{ dss_key_command }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that the key was added
- name : assert that the key was added
assert:
assert:
that:
that:
- 'result.changed == True'
- 'result.changed == True'
- 'result.key == dss_key_command'
- 'result.key == dss_key_command'
- 'result.key_options == None'
- 'result.key_options == None'
- name : re-add ssh-dss key with a leading command
- name : re-add ssh-dss key with a leading command
authorized_key:
authorized_key:
user : root
user : root
key : "{{ dss_key_command }}"
key : "{{ dss_key_command }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that nothing changed
- name : assert that nothing changed
assert:
assert:
that:
that:
- 'result.changed == False'
- 'result.changed == False'
# -------------------------------------------------------------
# -------------------------------------------------------------
# ssh-dss key with a complex quoted leading command
# ssh-dss key with a complex quoted leading command
@ -250,28 +259,28 @@
user : root
user : root
key : "{{ dss_key_complex_command }}"
key : "{{ dss_key_complex_command }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that the key was added
- name : assert that the key was added
assert:
assert:
that:
that:
- 'result.changed == True'
- 'result.changed == True'
- 'result.key == dss_key_complex_command'
- 'result.key == dss_key_complex_command'
- 'result.key_options == None'
- 'result.key_options == None'
- name : re-add ssh-dss key with a complex quoted leading command
- name : re-add ssh-dss key with a complex quoted leading command
authorized_key:
authorized_key:
user : root
user : root
key : "{{ dss_key_complex_command }}"
key : "{{ dss_key_complex_command }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that nothing changed
- name : assert that nothing changed
assert:
assert:
that:
that:
- 'result.changed == False'
- 'result.changed == False'
# -------------------------------------------------------------
# -------------------------------------------------------------
# ssh-dss key with a command and a single option, which are
# ssh-dss key with a command and a single option, which are
@ -282,28 +291,28 @@
user : root
user : root
key : "{{ dss_key_command_single_option }}"
key : "{{ dss_key_command_single_option }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that the key was added
- name : assert that the key was added
assert:
assert:
that:
that:
- 'result.changed == True'
- 'result.changed == True'
- 'result.key == dss_key_command_single_option'
- 'result.key == dss_key_command_single_option'
- 'result.key_options == None'
- 'result.key_options == None'
- name : re-add ssh-dss key with a command and a single option
- name : re-add ssh-dss key with a command and a single option
authorized_key:
authorized_key:
user : root
user : root
key : "{{ dss_key_command_single_option }}"
key : "{{ dss_key_command_single_option }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that nothing changed
- name : assert that nothing changed
assert:
assert:
that:
that:
- 'result.changed == False'
- 'result.changed == False'
# -------------------------------------------------------------
# -------------------------------------------------------------
# ssh-dss key with a command and multiple other options
# ssh-dss key with a command and multiple other options
@ -313,28 +322,28 @@
user : root
user : root
key : "{{ dss_key_command_multiple_options }}"
key : "{{ dss_key_command_multiple_options }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that the key was added
- name : assert that the key was added
assert:
assert:
that:
that:
- 'result.changed == True'
- 'result.changed == True'
- 'result.key == dss_key_command_multiple_options'
- 'result.key == dss_key_command_multiple_options'
- 'result.key_options == None'
- 'result.key_options == None'
- name : re-add ssh-dss key with a command and multiple options
- name : re-add ssh-dss key with a command and multiple options
authorized_key:
authorized_key:
user : root
user : root
key : "{{ dss_key_command_multiple_options }}"
key : "{{ dss_key_command_multiple_options }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that nothing changed
- name : assert that nothing changed
assert:
assert:
that:
that:
- 'result.changed == False'
- 'result.changed == False'
# -------------------------------------------------------------
# -------------------------------------------------------------
# ssh-dss key with multiple trailing parts, which are space-
# ssh-dss key with multiple trailing parts, which are space-
@ -345,81 +354,132 @@
user : root
user : root
key : "{{ dss_key_trailing }}"
key : "{{ dss_key_trailing }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that the key was added
- name : assert that the key was added
assert:
assert:
that:
that:
- 'result.changed == True'
- 'result.changed == True'
- 'result.key == dss_key_trailing'
- 'result.key == dss_key_trailing'
- 'result.key_options == None'
- 'result.key_options == None'
- name : re-add ssh-dss key with trailing parts
- name : re-add ssh-dss key with trailing parts
authorized_key:
authorized_key:
user : root
user : root
key : "{{ dss_key_trailing }}"
key : "{{ dss_key_trailing }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that nothing changed
- name : assert that nothing changed
assert:
assert:
that:
that:
- 'result.changed == False'
- 'result.changed == False'
# -------------------------------------------------------------
# -------------------------------------------------------------
# basic ssh-dss key with mutliple permit-open options
# basic ssh-dss key with mutliple permit-open options
# https://github.com/ansible/ansible-modules-core/issues/1715
# https://github.com/ansible/ansible-modules-core/issues/1715
- name : add basic ssh-dss key with multi-opts
- name : add basic ssh-dss key with multi-opts
authorized_key :
authorized_key:
user : root
user : root
key : "{{ dss_key_basic }}"
key : "{{ dss_key_basic }}"
key_options : 'no-agent-forwarding,no-X11-forwarding,permitopen="10.9.8.1:8080",permitopen="10.9.8.1:9001"'
key_options : 'no-agent-forwarding,no-X11-forwarding,permitopen="10.9.8.1:8080",permitopen="10.9.8.1:9001"'
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- name : assert that the key with multi-opts was added
- name : assert that the key with multi-opts was added
assert:
assert:
that:
that:
- 'result.changed == True'
- 'result.changed == True'
- 'result.key == dss_key_basic'
- 'result.key == dss_key_basic'
- 'result.key_options == "no-agent-forwarding,no-X11-forwarding,permitopen=\"10.9.8.1:8080\",permitopen=\"10.9.8.1:9001\""'
- 'result.key_options == "no-agent-forwarding,no-X11-forwarding,permitopen=\"10.9.8.1:8080\",permitopen=\"10.9.8.1:9001\""'
- name : get the file content
- name : get the file content
shell : cat "{{output_dir|expanduser}}/authorized_keys" | fgrep DATA_BASIC
shell : cat "{{ output_dir | expanduser }}/authorized_keys" | fgrep DATA_BASIC
changed_when : no
register : content
register : content
- name : validate content
- name : validate content
assert:
assert:
that:
that:
- 'content.stdout == "no-agent-forwarding,no-X11-forwarding,permitopen=\"10.9.8.1:8080\",permitopen=\"10.9.8.1:9001\" ssh-dss DATA_BASIC root@testing"'
- 'content.stdout == "no-agent-forwarding,no-X11-forwarding,permitopen=\"10.9.8.1:8080\",permitopen=\"10.9.8.1:9001\" ssh-dss DATA_BASIC root@testing"'
# -------------------------------------------------------------
# -------------------------------------------------------------
# check mode
# check mode
- name : copy an existing file in place with comments
- name : copy an existing file in place with comments
copy : src=existing_authorized_keys dest="{{output_dir|expanduser}}/authorized_keys"
copy:
src : existing_authorized_keys
dest : "{{ output_dir | expanduser }}/authorized_keys"
- authorized_key:
- authorized_key:
user : root
user : root
key : "{{ multiple_key_different_order_2 }}"
key : "{{ multiple_key_different_order_2 }}"
state : present
state : present
path : "{{ output_dir|expanduser}}/authorized_keys"
path : "{{ output_dir | expanduser }}/authorized_keys"
check_mode : True
check_mode : True
register : result
register : result
- name : assert that the key would be added and that the diff is shown
assert:
that:
- 'result.changed'
- '"ssh-rsa WHATEVER 2.5@testing" in result.diff.after'
- name : assert that the file was not changed
- name : assert that the file was not changed
copy : src=existing_authorized_keys dest="{{output_dir|expanduser}}/authorized_keys"
copy:
src : existing_authorized_keys
dest : "{{ output_dir | expanduser }}/authorized_keys"
register : result
register : result
- assert:
- assert:
that:
that:
- 'result.changed == False'
- 'result.changed == False'
# -------------------------------------------------------------
# comments
- name : Add rsa key with existing comment
authorized_key:
user : root
key : "{{ rsa_key_basic }}"
state : present
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
- name : Change the comment on an existing key
authorized_key:
user : root
key : "{{ rsa_key_basic }}"
comment : user@acme.com
state : present
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
- name : get the file content
shell : cat "{{ output_dir | expanduser }}/authorized_keys" | fgrep DATA_BASIC
changed_when : no
register : content
- name : Assert that comment on an existing key was changed
assert:
that:
- "'user@acme.com' in content.stdout"
- name : Set the same key with comment to ensure no changes are reported
authorized_key:
user : root
key : "{{ rsa_key_basic }}"
comment : user@acme.com
state : present
path : "{{ output_dir | expanduser }}/authorized_keys"
register : result
- name : Assert that no changes were made when running again
assert:
that:
- not result.changed
- debug:
var : "{{ item }}"
verbosity : 1
with_items:
- result
- content