Ansible-从一个主机中获取一些可变值,然后在另一个主机中使用它们

发布于 2025-01-20 02:54:23 字数 548 浏览 0 评论 0原文

我有一本包含两个独立主机的剧本是“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱自由 2025-01-27 02:54:23

重现问题不需要 vars_promptroles。请改用简单的变量任务。例如

- hosts: localhost
  vars:
    test_var: foo
  tasks:
    - debug:
        var: test_var

- hosts: xyz
  vars:
    test_var: "{{ hostvars.localhost.test_var }}"
  tasks:
    - debug:
        var: test_var

给出(仅调试输出)

ok: [localhost] => 
  test_var: foo

ok: [xyz] => 
  test_var: VARIABLE IS NOT DEFINED!

变量 test_var 在第一次播放中声明并分配给主机 localhost。要在另一个主机中使用该变量,字典 需要主机变量。但是,问题是变量 test_var 没有“实例化”。这意味着该变量未包含在主机变量中。让我们测试它

- hosts: localhost
  vars:
    test_var: foo
  tasks:
    - debug:
        var: test_var
    - debug:
        var: hostvars.localhost.test_var

- hosts: xyz
  gather_facts: false
  tasks:
    - debug:
        var: hostvars.localhost.test_var

给出(仅调试输出)

ok: [localhost] => 
  test_var: foo

ok: [localhost] => 
  hostvars.localhost.test_var: VARIABLE IS NOT DEFINED!

ok: [xyz] => 
  hostvars.localhost.test_var: VARIABLE IS NOT DEFINED!

使用 -e, --extra-vars 时问题将消失

shell> ansible-playbook playbook.yml -e test_var=bar

给出(仅调试输出)

ok: [localhost] => 
  test_var: bar

ok: [localhost] => 
  hostvars.localhost.test_var: bar

ok: [xyz] => 
  hostvars.localhost.test_var: bar

解决 变量的其他来源 “实例化”变量第一部戏。使用set_fact

- hosts: localhost
  vars:
    test_var: foo
  tasks:
    - debug:
        var: test_var
    - debug:
        var: hostvars.localhost.test_var
    - set_fact:
        test_var: "{{ test_var }}"
    - debug:
        var: hostvars.localhost.test_var

- hosts: xyz
  vars:
    test_var: "{{ hostvars.localhost.test_var }}"
  tasks:
    - debug:
        var: test_var

给出(仅调试输出)

ok: [localhost] => 
  test_var: foo

ok: [localhost] => 
  hostvars.localhost.test_var: VARIABLE IS NOT DEFINED!

ok: [localhost] => 
  hostvars.localhost.test_var: foo

ok: [xyz] => 
  test_var: foo

测试其他源,例如

shell> cat host_vars/localhost 
test_var: foo
- hosts: localhost
  tasks:
    - debug:
        var: test_var
    - set_fact:
        test_var: "{{ test_var }}"

- hosts: xyz
  vars:
    test_var: "{{ hostvars.localhost.test_var }}"
  tasks:
    - debug:
        var: test_var

给出(仅调试输出)

ok: [localhost] => 
  test_var: foo

ok: [xyz] => 
  test_var: foo

Neither vars_prompt nor roles is needed to reproduce the problem. Use simple vars and tasks instead. For example

- hosts: localhost
  vars:
    test_var: foo
  tasks:
    - debug:
        var: test_var

- hosts: xyz
  vars:
    test_var: "{{ hostvars.localhost.test_var }}"
  tasks:
    - debug:
        var: test_var

gives (debug output only)

ok: [localhost] => 
  test_var: foo

ok: [xyz] => 
  test_var: VARIABLE IS NOT DEFINED!

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

- hosts: localhost
  vars:
    test_var: foo
  tasks:
    - debug:
        var: test_var
    - debug:
        var: hostvars.localhost.test_var

- hosts: xyz
  gather_facts: false
  tasks:
    - debug:
        var: hostvars.localhost.test_var

gives (debug output only)

ok: [localhost] => 
  test_var: foo

ok: [localhost] => 
  hostvars.localhost.test_var: VARIABLE IS NOT DEFINED!

ok: [xyz] => 
  hostvars.localhost.test_var: VARIABLE IS NOT DEFINED!

The problem will disappear when -e, --extra-vars is used

shell> ansible-playbook playbook.yml -e test_var=bar

gives (debug output only)

ok: [localhost] => 
  test_var: bar

ok: [localhost] => 
  hostvars.localhost.test_var: bar

ok: [xyz] => 
  hostvars.localhost.test_var: bar

To fix the problem for other sources of variables "instantiate" the variable in the first play. Use set_fact

- hosts: localhost
  vars:
    test_var: foo
  tasks:
    - debug:
        var: test_var
    - debug:
        var: hostvars.localhost.test_var
    - set_fact:
        test_var: "{{ test_var }}"
    - debug:
        var: hostvars.localhost.test_var

- hosts: xyz
  vars:
    test_var: "{{ hostvars.localhost.test_var }}"
  tasks:
    - debug:
        var: test_var

gives (debug output only)

ok: [localhost] => 
  test_var: foo

ok: [localhost] => 
  hostvars.localhost.test_var: VARIABLE IS NOT DEFINED!

ok: [localhost] => 
  hostvars.localhost.test_var: foo

ok: [xyz] => 
  test_var: foo

Test other sources, e.g.

shell> cat host_vars/localhost 
test_var: foo
- hosts: localhost
  tasks:
    - debug:
        var: test_var
    - set_fact:
        test_var: "{{ test_var }}"

- hosts: xyz
  vars:
    test_var: "{{ hostvars.localhost.test_var }}"
  tasks:
    - debug:
        var: test_var

gives (debug output only)

ok: [localhost] => 
  test_var: foo

ok: [xyz] => 
  test_var: foo
萌辣 2025-01-27 02:54:23

第一部剧本中,您可以使用创建虚拟主机variable_holder并使用共享var的任务:

- name: add variables to dummy host
  add_host:
    name: "variable_holder"
    shared_variable:  "{{ value }}"

在 var共享的

- name: Define xyz
  hosts: xyz
  vars:
    value: "{{ hostvars['variable_holder']['shared_variable'] }}"

两个戏剧必须在您启动的同一剧本中。

in first playbook, you could use a task which creates a dummy host variable_holder and use a shared var: (use the module add_host)

- name: add variables to dummy host
  add_host:
    name: "variable_holder"
    shared_variable:  "{{ value }}"

and in second play, you just recall the var shared

- name: Define xyz
  hosts: xyz
  vars:
    value: "{{ hostvars['variable_holder']['shared_variable'] }}"

the 2 plays have to be in same playbook you launch....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文