0

我有这个变量

  - debug:
      var: hostvars{{ls_ip}}.host_name

输出是:

ok: [40.53.52.135] => {
    "hostvars['40.53.52.136'].host_name": "SRV0026"
}

我需要将字符串SRV0026保存在新的 var 中以在另一个任务中使用此变量,但使用 set_fact 显示 var 名称但不显示内容。我试过“var”和“msg”

  - set_fact:
      ls_name: hostvars{{ls_ip}}.host_name

  - name: Debug ls_name whit var
    debug:
      var: ls_name

  - name: Debug ls_name whit msg
    debug:
      msg: "{{ls_name}}"

输出是:

TASK [elasticsearch : Debug ls_name con var] ************************************************************
Tuesday 10 March 2020  15:02:24 -0300 (0:00:00.152)       0:00:04.630 ********* 
ok: [40.53.52.135] => {
    "ls_name": "hostvars['40.53.52.136'].host_name"
}

TASK [elasticsearch : Debug ls_name whit msg] ***********************************************************
Tuesday 10 March 2020  15:02:24 -0300 (0:00:00.098)       0:00:04.729 ********* 
ok: [40.53.52.135] => {
    "msg": "hostvars['40.53.52.136'].host_name"
}

你能帮助我吗?

4

1 回答 1

0

问:“我只看到字符串 SRV0026 使用 debug with var”

A: 要使用 debug/msg 显示变量的值,试试这个

  - debug:
      msg: "{{ hostvars[ls_ip].host_name }}"

声明变量试试这个

  - set_fact:
      ls_name: "{{ hostvars[ls_ip].host_name }}"
于 2020-03-10T18:50:50.620 回答