ansible - Ansible AWX/Tower - 访问另一个游戏中保存为工件的变量
问题描述
我有一本包含多个剧本的剧本。其中一个剧本生成一个变量,并使用 set_stats 模块将其存储为工件。后续播放需要访问该变量,但出现给定变量未定义的错误。如何访问工件中的变量?(顺便说一句,在这种情况下,使用将导致将变量保存在 extra_variables 而不是工件容器中的工作流不是选项)
详细问题:
我有以下剧本,其中包括 2 个在不同主机上执行的剧本:
---
- hosts: ansible
roles:
- role_parse_strings
- hosts: all, !ansible
roles:
- role_setup_basics
- role_create_accounts
第一场戏中的角色“role_parse_strings”会生成变量“users”,因为set_stats模块将其存储为工件。以下内容位于 ansible awx 的工件部分:
users:
- username: user1
admin: true
- username: user2
admin: false
当角色“role_create_accounts”被执行时,它试图通过以下方式访问变量“users”......
- user: name={{ item.username }}
shell=/bin/bash
createhome=yes
groups=user
state=present
with_items: "{{ users }}"
..显示此错误:
{
"msg": "'users' is undefined",
"_ansible_no_log": false
}
解决方案
您可以使用 set_fact 在主机之间共享变量。下面的示例展示了如何通过 set_fact 共享文件内容。
- hosts: host1
pre_tasks:
- name: Slurp the public key
slurp:
src: /tmp/ssh_key.pub
register: my_key_pub
- name: Save the public key
set_fact:
my_slave_key: >-
{{ my_key_pub['content'] | b64decode }}
- hosts: host2
vars:
slave_key: "{{ my_slave_key }}"
pre_tasks:
- set_fact:
my_slave_key: >-
{{ hostvars[groups["host1"][0]].my_slave_key | trim }}
我们将公钥的内容保存为名为 my_slave_key 的事实名称,并将其分配为 host2 中的另一个变量 slave_key:
hostvars[groups["host1"][0]].my_slave_key
推荐阅读
- javascript - 如何在页面加载时启动触摸屏
- c# - 如何修改已填充并出现在组合框中的对象列表?
- error-handling - 方法“verify_expiration”的jwt错误
- python - 张量列表成张量
- python - 如何修复我在 scikit-learn 中的线性回归中遇到的错误
- scala - 在 scala 中从 Try 中提取值时丢失路径相关类型
- android - 改造 onResponse 方法未执行
- javascript - 重复使用 clearInterval 和 clearTimeout 不起作用
- c# - 如何在运行时修改 .Net 程序集并保留这些更改
- python-2.7 - 用于 API 调用的 Python 库,比请求更快