从文件中 Ansible 复制 ssh public 密钥,在 uri 调用中使用

Ansible copy ssh public key from file, use in uri call

我需要从本地文件复制 SSH public 密钥,然后在我的剧本中的 uri 任务中使用它。 请记住,我不能使用 "authorized_key" 模块,因为这是一个系统,我必须使用 API 为用户配置 public 键。

下面的代码总是失败,我 100% 确定这是因为我使用的过滤器。我包括了对 body 有效的注释掉的部分。 尝试使用带有 regex_search 的查找,我使用了 [^\s]\s[^\s] python。此外,密钥位于我本地主机的不同目录中 (../../ssh/ssh_key/key.pub)

有什么想法吗?

- 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  FS }'"
            shell: "cat ../keys/ssh_keys/{{item.username}}.pub | awk '{print  FS }'"
            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}}"

这是我在使用 -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"
}

我想通了!

  1. 使用 shell 和 awk 命令来收集密钥。 (注意:包括一个用于 RSA 密钥的 awk,以及一个用于我们使用的 id_ed25519。RSA 已被注释掉,但其他人如果想使用可以发表评论。)
  2. 使用循环控制遍历结果。

代码如下:

- 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  FS }'"
            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