如何解决如何将上一个任务的输出存储在 Ansible 中的变量中并将该变量用于下一个后续任务
我是 Ansible 的新手,需要您对以下问题的专家建议:
我有一个场景,我想使用 ansible playbook 运行一些 API。其中第一个 API 是获取 auth 令牌。 我创建了一个角色来获取 auth 令牌,并且在任务中我传递了一个 curl 命令,该命令将为我提供 authToken 作为任务的结果,如下所示: 任务(main.yaml)
- name: Execute shell command
# shell: uptime;id
shell: |
/usr/bin/curl -k --header "Content-Type: application/json" --request POST --data '{"username":"auth_user","password":"{{ user_pass }}"}' https://example.com/webgui/v1/tokens | python -m json.tool | grep -i authToken | cut -d ":" -f2 | cut -d '"' -f2
register: output
- name: Print the output of shell command
debug: var=output.stdout_lines
- set_fact:
api_token={{ output.stdout_lines }}
- debug: var=api_token
结果:
TASK [get_api_token : debug] ***************************************** ****************************************************** ****************************************************** ************************************** 好的:[目标 1] => { “api_token”:[ “AhreaehhGhGggghnbvfdv” ] }
我有两个问题: 1:如何将第一个任务的结果(在本例中为加密字符串)存储为变量以实现可重用性目的? 2:如何在另一个需要传递此令牌的剧本中使用相同的变量? 对于第二个查询,我在 main.yaml 中尝试了以下任务:
- name: Get project data for scalable run
uri:
url: https://example.com/webgui/v1/projects
method: GET
return_content: yes
body_format: json
headers:
fake-api-key: "{{ api_token }}"
请推荐?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。