Adding Multiple SSH Keys Using Inaccessible

I wrote an indispensable script to remove SSH keys from remote servers:

--- - name: "Add keys to the authorized_keys of the user ubuntu" user: ubuntu hosts: www tasks: - name: "Remove key #1" authorized_key: user=ubuntu key="{{ item }}" state=absent with_file: - id_rsa_number_one.pub - name: "Remove key #2" authorized_key: user=ubuntu key="{{ item }}" state=absent with_file: - id_rsa_number_two.pub ... 

Adding each file to another task is immaterial, so I tried using with_fileglob :

  - name: "Remove all keys at once" authorized_key: user=ubuntu key="{{ item }}" state=absent with_fileglob: - /Users/adamatan/ansible/id_rsa*.pub 

But this does not work with lines like this:

failed: [www.example.com] => (item = / Users / adamatan / ansible / id_rsa_one.pub) => {"failed": true, "item": "/Users/adamatan/ansible/id_rsa_one.pub "} msg: invalid key specified: /Users/adamatan/ansible/id_rsa_one.pub

The same key file is successfully deleted using a unique task, but fails when it is part of fileglob .

How can I add or remove SSH keys using inaccessible?

+6
source share
1 answer

I believe that you only get file names using with_fileglob , but with_file retrieves the contents of the file. And for the authorized_key module, the actual key is required.

So, you should still use the with_fileglob , but instead of sending the file name to the "key =" parameter, you should use the file search module ).

 - name: "Remove all keys at once" authorized_key: user=ubuntu key="{{ lookup('file', item) }}" state=absent with_fileglob: - /Users/adamatan/ansible/id_rsa*.pub 
+11
source

Source: https://habr.com/ru/post/976276/


All Articles