使用循环变量在 Ansible 中仅运行一次任务

问题描述

我遇到了这种情况,我在 Ansible 中使用 with_items 将变量加载到 Shell 命令。代码如下。

     - name: Create a valid SCM credential from a private_key file
   shell:
     cmd: tower-cli credential create --organization "Default" --name "DevOps User" --credential-type "type2" --inputs "{'user':'devops','{{ item.name }}_ssh_key':\"$( sed -z 's/\n/\\n/g' {{ item.file }} )\" }"
   no_log: false
   with_items:
     - { name: test1,file: test.pem }
     - { name: test2,file: test2.pem }
     - { name: test3,file: test3.pem }

我面临的问题是,这个命令将运行三遍,因为我在循环中有三个项目。但是我只需要使用循环变量运行 shell 命令一次。我期望运行的 shell 命令如下。

tower-cli credential create --organization "Default" --name "DevOps User" --credential-type "type2" --inputs "{'user':'devops','test_ssh_key':\"$( sed -z 's/\n/\\n/g' test.pem )\",'test2_ssh_key':\"$( sed -z 's/\n/\\n/g' test2.pem )\",'test3_ssh_key':\"$( sed -z 's/\n/\\n/g' test3.pem )\" }"

任何人都可以在这方面指导我,我如何在剧本中使用 Ansible 循环来实现我的期望。

我也尝试了 Tower_credential 模块,我无法创建自定义凭据,收到以下错误消息

fatal: [127.0.0.1]: FAILED! => {"changed": false,"msg": "Unsupported parameters for (tower_credential) module: credential_type,inputs Supported parameters include: authorize,authorize_password,become_method,become_password,become_username,client,description,domain,host,kind,name,organization,password,project,secret,security_token,ssh_key_data,ssh_key_unlock,state,subscription,team,tenant,tower_config_file,tower_host,tower_password,tower_username,user,username,validate_certs,vault_id,vault_password"}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...