为啥 Ansible-Tower 忽略了额外的变量?
Posted
技术标签:
【中文标题】为啥 Ansible-Tower 忽略了额外的变量?【英文标题】:Why Ansible-Tower is ignoring extra variables?为什么 Ansible-Tower 忽略了额外的变量? 【发布时间】:2019-10-01 07:40:27 【问题描述】:尝试通过 REST API 为工作流程提供午餐并传递额外的变量供 playbook 使用,但返回的正文显示提供的变量已放入 ignored_fields
部分。
使用 POSTMAN 和 CURL 运行模板都返回相同的结果
CURL 命令
curl -X POST http://172.16.0.97/api/v2/job_templates/8/launch/ -H "Content-Type: application/json" -H "Authorization: Bearer Je
gxwfQrdKQXoRUtNWtWFz62FX5bTy" -d "\"extra_vars\": \"vendor\":\"juniper\""
返回的正文
"job":34,"ignored_fields":"extra_vars":"vendor":"juniper","id":34,"type":"job","url":"/api/v2/jobs/34/","related":"created_by":"/api/v2/users/1/","modified_by":"/api/v2/users/1/","labels":"/api/v2/jobs/34/labels/","inventory":"/api/v2/inventories/1/","project":"/api/v2/projects/7/","extra_credentials":"/api/v2/jobs/34/extra_credentials/","credentials":"/api/v2/jobs/34/credentials/","unified_job_template":"/api/v2/job_templates/8/","stdout":"/api/v2/jobs/34/stdout/","job_events":"/api/v2/jobs/34/job_events/","job_host_summaries":"/api/v2/jobs/34/job_host_summaries/","activity_stream":"/api/v2/jobs/34/activity_stream/","notifications":"/api/v2/jobs/34/notifications/","job_template":"/api/v2/job_templates/8/","cancel":"/api/v2/jobs/34/cancel/","create_schedule":"/api/v2/jobs/34/create_schedule/","relaunch":"/api/v2/jobs/34/relaunch/","summary_fields":"inventory":"id":1,"name":"Demo Inventory","description":"","has_active_failures":true,"total_hosts":1,"hosts_with_active_failures":1,"total_groups":0,"groups_with_active_failures":0,"has_inventory_sources":false,"total_inventory_sources":0,"inventory_sources_with_failures":0,"organization_id":1,"kind":"","project":"id":7,"name":"Cox-Phase3","description":"","status":"successful","scm_type":"git","job_template":"id":8,"name":"Port Flap","description":"","unified_job_template":"id":8,"name":"Port Flap","description":"","unified_job_type":"job","created_by":"id":1,"username":"admin","first_name":"","last_name":"","modified_by":"id":1,"username":"admin","first_name":"","last_name":"","user_capabilities":"delete":true,"start":true,"labels":"count":0,"results":[],"extra_credentials":[],"credentials":[],"created":"2019-05-14T09:43:16.115516Z","modified":"2019-05-14T09:43:16.177517Z","name":"Port Flap","description":"","job_type":"run","inventory":1,"project":7,"playbook":"main.yml","forks":0,"limit":"","verbosity":1,"extra_vars":"","job_tags":"","force_handlers":false,"skip_tags":"","start_at_task":"","timeout":0,"use_fact_cache":false,"unified_job_template":8,"launch_type":"manual","status":"pending","failed":false,"started":null,"finished":null,"elapsed":0.0,"job_args":"","job_cwd":"","job_env":,"job_explanation":"","execution_node":"","controller_node":"","result_traceback":"","event_processing_finished":false,"job_template":8,"passwords_needed_to_start":[],"ask_diff_mode_on_launch":false,"ask_variables_on_launch":false,"ask_limit_on_launch":false,"ask_tags_on_launch":false,"ask_skip_tags_on_launch":false,"ask_job_type_on_launch":false,"ask_verbosity_on_launch":false,"ask_inventory_on_launch":false,"ask_credential_on_launch":false,"allow_simultaneous":false,"artifacts":,"scm_revision":"","instance_group":null,"diff_mode":false,"job_slice_number":0,"job_slice_count":1,"credential":null,"vault_credential":null
【问题讨论】:
你没有指定你的 Tower 版本,但是3.0 has madeextra_vars
more strict;这些条件是否适用于您(3.0、启用调查和/或启动时询问 = true)?
另外,虽然您没有问这个问题,但如果您在 shell 中使用单引号和双引号之间的区别,您的生活会好很多,因为您的 curl
命令使用 @987654327 会不那么冗长@
@MatthewLDaniel - 感谢您的评论。老实说,我使用的是 Ansible AWX 而不是 Tower,而且我正在运行可能的最新版本。关于单引号和双引号,这是我第三次尝试调用 API。在那之前我使用了不同的格式。
嗯,Tower 只是 AWX-for-money,所以如果您使用的是最新的 AWX,那么您使用的是 4,这可能会受到该更改的影响。您跳过了其他两部分:您是否对该模板启用了调查,和/或您是否像文档所说的那样拥有ask_variables_on_launch=True
?
@MatthewLDaniel - 打开ask_variables_on_launch=True
后它起作用了。请提交答案,我会将其标记为正确。感谢您的帮助!
【参考方案1】:
根据精美手册,AWX(以及 Tower)3.0 版及更高版本使 extra_vars
更加严格:https://docs.ansible.com/ansible-tower/latest/html/userguide/job_templates.html#ug-jobtemplates-extravars
如果您运行的版本高于 3.0,则需要打开剧本调查或为该模板设置 ask_variables_on_launch=True
【讨论】:
【参考方案2】:就我而言,我使用的是curl -L ...
,并且有效负载在重定向后丢失了。请务必仔细检查是否在确保 ask_variables_on_launch=True
后发现 extra_vars
仍然被忽略。
【讨论】:
以上是关于为啥 Ansible-Tower 忽略了额外的变量?的主要内容,如果未能解决你的问题,请参考以下文章
为啥 TensorFlow 会在 TensorBoard 可视化中为我的变量创建额外的命名空间?