Ansible-从一个主机中获取一些可变值,然后在另一个主机中使用它们
我有一本包含两个独立主机的剧本是“ localhost”,另一个是“ xyz”。
- hosts: localhost
connection: local
gather_facts: False
vars_prompt:
- name: "value"
prompt: "Enter your value"
private: yes
roles:
- a
###
- name: Define xyz
hosts: xyz
roles:
- a
- b
当我们运行上面的剧本时,然后在“ localhost” var_promot中获取用户的值,此后,我想在shell命令中的“ xyz”主机角色中使用这些值。
当前,在“ A”角色内,我有这种类型的代码,但它给了我一个错误。
- name: echo
shell: " echo {{ value | urlencode }}"
register: sout
知道该怎么做或其他任何方法吗?
I have a playbook that contains the two separate hosts one is 'localhost' and another is 'xyz'.
- hosts: localhost
connection: local
gather_facts: False
vars_prompt:
- name: "value"
prompt: "Enter your value"
private: yes
roles:
- a
###
- name: Define xyz
hosts: xyz
roles:
- a
- b
When we run the above playbook then in 'localhost' var_promot take the values from the user and after that, I want to use those values in the 'xyz' host roles in shell commands.
Currently, Inside 'a' role I have this type of code but it gives me an error.
- name: echo
shell: " echo {{ value | urlencode }}"
register: sout
Any idea how to do that or any other way for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重现问题不需要 vars_prompt 和 roles。请改用简单的变量和任务。例如
给出(仅调试输出)
变量 test_var 在第一次播放中声明并分配给主机 localhost。要在另一个主机中使用该变量,字典 需要主机变量。但是,问题是变量 test_var 没有“实例化”。这意味着该变量未包含在主机变量中。让我们测试它
给出(仅调试输出)
使用 -e, --extra-vars 时问题将消失
给出(仅调试输出)
解决 变量的其他来源 “实例化”变量第一部戏。使用set_fact
给出(仅调试输出)
测试其他源,例如
给出(仅调试输出)
Neither vars_prompt nor roles is needed to reproduce the problem. Use simple vars and tasks instead. For example
gives (debug output only)
The variable test_var was declared in the first play and assigned to the host localhost. To use the variable in another host the dictionary hostvars is needed. But, the problem is that the variable test_var was not "instantiated". This means the variable was not included in the hostvars. Let's test it
gives (debug output only)
The problem will disappear when -e, --extra-vars is used
gives (debug output only)
To fix the problem for other sources of variables "instantiate" the variable in the first play. Use set_fact
gives (debug output only)
Test other sources, e.g.
gives (debug output only)
第一部剧本中,您可以使用创建虚拟主机
variable_holder
并使用共享var的任务:在 var共享的
两个戏剧必须在您启动的同一剧本中。
in first playbook, you could use a task which creates a dummy host
variable_holder
and use a shared var: (use the moduleadd_host
)and in second play, you just recall the var shared
the 2 plays have to be in same playbook you launch....