【发布时间】:2020-02-26 02:45:56
【问题描述】:
我需要从本地文件复制 SSH 公钥,然后在我的剧本中的 uri 任务中使用它。 请记住,我不能使用“authorized_key”模块,因为这是一个系统,我必须使用 API 为用户配置公钥。
下面的代码一直失败,我 100% 确定它是因为我使用的过滤器。我包括了对身体有用的注释掉的部分。 尝试使用带有 regex_search 的查找,我使用了在 python 中工作的 [^\s]\s[^\s]。此外,密钥位于我的本地主机(../../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 $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"
}
【问题讨论】:
-
regex_search(...)中的内容不应该用引号引起来吗? -
我更新了我的代码,同样的问题发生了。也许我对这一切都错了。我需要找到一种使用正则表达式从本地文件中提取字符串的方法,然后在 uri 模块的主体中使用它。我是否正在尝试使用 regex_search?
-
现在你又添加了一个错字:
../../keys/ssh_keys/regex_search应该是../../keys/ssh_keys | regex_search。 -
好收获!我再次更新了我的代码。我还将我试图搜索的特定文件移动到该剧本正在运行的同一目录中,我还删除了其他变量。我在上面也更新了一个新错误...
-
zz456z.pub是变量还是文件?它必须是一个变量。如果是文件,需要将zz456z.pub替换为lookup('file', 'zz456z.pub')。
标签: regex api ansible jinja2 public-key