-1

I need to copy the SSH public key from a local file, then use it in a uri task in my playbook. Keep in mind, I cannot use "authorized_key" module as this is a system I must use the API to configure public keys for users.

Code below keeps failing, I am 100% sure its because of the filter I am using. I am including the commented out section that does work for the body. Trying to use a lookup with a regex_search, I used [^\s]\s[^\s] which works in python. Also the key is in a different directory in my local host (../../ssh/ssh_key/key.pub)

Any ideas?

- name: copy public key to gitea
  hosts: localhost

  tasks:

          - name: include user to add as variable
            include_vars:
              file: users.yaml
              name: users

          - name: Gather users key contents and create variable
            # shell: "cat ../keys/ssh_keys/zz123z.pub | awk '{print $1 FS $2}'"
            shell: "cat ../keys/ssh_keys/{{item.username}}.pub | awk '{print $1 FS $2}'"
            register: key
            with_items:
              - "{{users.user}}"



          - name: Add user's key to gitea
            uri:
              url: https://10.10.10.10/api/v1/admin/users/{{ item.username }}/keys
              headers:
                Authorization: "token {{ users.GiteaApiToken }}"
              validate_certs: no
              return_content: yes
              status_code: 201
              method: POST
              body: "{\"key\": \"{{ key.stdout }}\", \"read_only\": true, \"title\": \"{{ item.username }} shared 
              body_format: json
            with_items:
              - "{{users.user}}"

This is the error I receive when using -vvv

TASK [Add user's key to gitea] *************************************************
task path: /home/dave/projects/Infrastructure/ansible/AddTempUsers/addusers.yaml:275
Wednesday 04 March 2020  18:14:29 -0500 (0:00:00.537)       0:00:01.991 ******* 
fatal: [localhost]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to be in '/home/dave/projects/Infrastructure/ansible/AddTempUsers/addusers.yaml': line 275, column 13, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n          - name: Add user's key to gitea\n            ^ here\n"
}
Dave
  • 727
  • 1
  • 9
  • 20
  • 3
    Shouldn't the stuff inside the `regex_search(...)` be in quotes? – Jack Feb 26 '20 at 03:03
  • I updated my code, same problem happens. Maybe I am going about this all wrong. I need to find a way to extract a string using a regex from a local file, then utilize it in the uri module's body. Am I going about this right trying to use regex_search? – Dave Feb 26 '20 at 15:59
  • 1
    Now you added another typo: `../../keys/ssh_keys/regex_search` should be `../../keys/ssh_keys | regex_search`. – Jack Feb 26 '20 at 16:09
  • Good catch! I updated my code again. I also moved the specific file I am trying to search in the same directory this playbook is being run, I also removed other variables on the line. I get a new error which I updated above as well... – Dave Feb 26 '20 at 23:55
  • 1
    Is `zz456z.pub` a variable, or a file? It needs to be a variable. If it is a file, you need to replace `zz456z.pub` with `lookup('file', 'zz456z.pub')`. – Jack Feb 27 '20 at 02:31
  • I figured out how to get this to work if I define the user. I updated my code to show (used shell with an awk command). The main problem now is how to iterate over my list of users in another file. I need to find a way to send key.stdout to a list, then iterate over that same list's index for each variable. Keep getting this error: The error was: 'dict object' has no attribute 'stdout' How can I write each stdout of the shell command to a variable and iterate through said list? – Dave Mar 04 '20 at 23:17

1 Answers1

0

I FIGURED IT OUT!

  1. used shell with an awk command to gather the keys. (Note: including an awk for RSA keys, and one for id_ed25519, which we use. RSA is commented out but others can comment if they wish to use.)
  2. Used loop control to iterate through the results.

Code below:

- name: copy public key to gitea
  hosts: localhost

  tasks:

          - name: include user to add as variable
            include_vars:
              file: users.yaml
              name: users

          - name: Gather users key contents and create variable
            # For RSA Keys
            # shell: "cat ../keys/ssh_keys/{{item.username}}.pub | awk '/-END PUBLIC KEY-/ { p = 0 }; p; /-BEGIN PUBLIC KEY-/ { p = 1 }'
            # For id_ed5519 Keys
            shell: "cat ../keys/ssh_keys/{{item.username}}.pub | awk '{print $1 FS $2}'"
            register: key
            with_items:
              - "{{users.user}}"

          - name: Add user's key to gitea
            uri:
              url: https://10.10.10.10/api/v1/admin/users/{{ item.username }}/keys
              headers:
                Authorization: "token {{ users.GiteaApiToken }}"
              validate_certs: no
              return_content: yes
              status_code: 201
              method: POST
              body: "{\"key\": \"{{ key.results[ndx].stdout }}\", \"read_only\": true, \"title\": \"{{ item.username }} shared VM\"}"
              body_format: json
            with_items:
              - "{{users.user}}"
            loop_control:
              index_var: ndx
Dave
  • 727
  • 1
  • 9
  • 20