【发布时间】:2021-01-07 16:11:09
【问题描述】:
我编写了一个使用自定义凭据创建凭据的剧本,下面是 ansible 剧本
---
- name: Trigger an Atower API
hosts: localhost
connection: local
tasks:
- name: Create a valid SCM credential from a private_key file
command: tower-cli credential create --organization "Default" --name "DevOps User" --credential-type "csa-test2" --inputs "{'user':'devops', 'stg01_ssh_key':\"$( sed -z 's/\n/\\n/g' test.pem )\" }"
no_log: false
我收到这个与报价有关的错误
name", "DevOps User", "--credential-type", "csa-test2", "--inputs", "{'user':'devops', 'stg01_ssh_key':\"$( sed -z 's/\\n/\\n/g' test.pem )\" }"], "delta": "0:00:01.319114", "end": "2021-01-07 16:00:22.763388", "msg": "non-zero return code", "rc": 40, "start": "2021-01-07 16:00:21.444274", "stderr": "Error: The Tower server claims it was sent a bad request.\n\nPOST http://x.x.x.x:13080/api/v2/credentials/\nParams: None\nData: {\"inputs\": {\"stg01_ssh_key\": \"$( sed -z 's/\\n/\\n/g' test.pem )\", \"user\": \"devops\"}, \"credential_type\": 36, \"organization\": 1, \"name\": \"DevOps User\"}\n\nResponse: {\"inputs\":{\"stg01_ssh_key\":[\"Invalid certificate or key: $( sed -z 's/\\n/\\n/g' test.pem )...\"]}}", "stderr_lines": ["Error: The Tower server claims it was sent a bad request.", "", "POST http://x.x.x.x:13080/api/v2/credentials/", "Params: None", "Data: {\"inputs\": {\"stg01_ssh_key\": \"$( sed -z 's/\\n/\\n/g' test.pem )\", \"user\": \"devops\"}, \"credential_type\": 36, \"organization\": 1, \"name\": \"DevOps User\"}", "", "Response: {\"inputs\":{\"stg01_ssh_key\":[\"Invalid certificate or key: $( sed -z 's/\\n/\\n/g' test.pem )...\"]}}"], "stdout": "", "stdout_lines": []}
另外,当我尝试手动“tower-cli create”命令时,它工作正常。任何人都可以在这方面帮助我。我不知道这里出了什么问题。
【问题讨论】:
-
尝试使用 shell 代替命令,文档说:命令不会通过 shell 处理,所以像 $HOSTNAME 这样的变量和像 "*"、" 这样的操作", "|", ";"而“&”将不起作用。如果您需要这些功能,请使用 ansible.builtin.shell 模块。
-
感谢@CyrilleMODIANO。你说的对。我改为shell命令。成功了!!
-
@Mathan 说谢谢的方式就是接受正确答案(绿色勾号)。这也是让其他人知道有有效解决方案的一种方式。您也可以为答案投票。
标签: ansible ansible-2.x ansible-inventory ansible-facts