Regex Ansible从文件复制ssh公钥,用于uri调用

Regex Ansible从文件复制ssh公钥,用于uri调用,regex,api,ansible,jinja2,public-key,Regex,Api,Ansible,Jinja2,Public Key,我需要从本地文件复制SSH公钥,然后在我的剧本中的uri任务中使用它。 请记住,我不能使用“授权密钥”模块,因为这是一个系统,我必须使用API为用户配置公钥 下面的代码不断失败,我100%确定这是因为我使用的过滤器。我包括注释掉的部分,它确实对身体有用。 尝试将查找与正则表达式搜索结合使用时,我使用了在python中工作的[^\s]\s[^\s]。此外,密钥位于本地主机的不同目录中(../../ssh/ssh\u key/key.pub) 有什么想法吗 - name: copy public k

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

下面的代码不断失败,我100%确定这是因为我使用的过滤器。我包括注释掉的部分,它确实对身体有用。 尝试将查找与正则表达式搜索结合使用时,我使用了在python中工作的[^\s]\s[^\s]。此外,密钥位于本地主机的不同目录中(../../ssh/ssh\u 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 $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}}"
这是我在使用-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"
}

我想出来了

  • 使用shell和awk命令收集密钥。(注意:包括一个用于RSA密钥的awk和一个用于我们使用的id_ed25519的awk。RSA已注释掉,但其他人可以在希望使用时进行注释。)
  • 使用循环控制对结果进行迭代
  • 代码如下:

    - 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
    

    regex_搜索(…)
    中的内容不应该用引号括起来吗?我更新了我的代码,同样的问题也发生了。也许我在这件事上做错了。我需要找到一种方法,使用正则表达式从本地文件中提取字符串,然后在uri模块的主体中使用它。我尝试使用regex_搜索是否正确?现在您添加了另一个输入:
    。/../keys/ssh_-keys/regex_-search
    应该是
    。/../keys/ssh_-keys | regex_-search
    。很好!我再次更新了代码。我还移动了我试图搜索的特定文件,该playbook正在运行的目录中,我还删除了行中的其他变量。我得到了一个新的错误,我在上面也更新了…是变量还是文件?它需要是一个变量。如果是文件,则需要将
    zz456z.pub
    替换为
    lookup('file','zz456z.pub')