Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将完整主机名作为变量存储在ansible中?_Ansible - Fatal编程技术网

如何将完整主机名作为变量存储在ansible中?

如何将完整主机名作为变量存储在ansible中?,ansible,Ansible,我需要使用两个主机中的一个作为变量。我确实有inventory\u hostname\u,但我需要一个完整的主机作为变量。目前,对于测试,我使用硬编码值。我的playbook将同时在两台主机上运行,因此我可以如何识别并存储为变量 host_1_full = 123.abc.de.com host_2_full = 345.abc.de.com 以上都是主持人,我也有 --- - name: Ansible Script hosts: all vars: host1_s

我需要使用两个主机中的一个作为变量。我确实有inventory\u hostname\u,但我需要一个完整的主机作为变量。目前,对于测试,我使用硬编码值。我的playbook将同时在两台主机上运行,因此我可以如何识别并存储为变量

host_1_full = 123.abc.de.com

host_2_full = 345.abc.de.com
以上都是主持人,我也有

---
- name: Ansible Script 
  hosts: all
  
  vars:
    host1_short : '123'
    host2_short : '345'

  tasks:
    - name: set host
      set_fact:
        host1_full: "{{inventory_hostname}}"
      when: inventory_hostname_short == host1_short

    - name: print info
      debug:
        msg: "host - {{host1_full}}"

    - name: block1
      block:
      - name:running PS1 file
        win_shell: "script.ps1"
        register: host1_output
      
      when: inventory_hostname_short == host1_short  
      

    - name: block2
      block:
      
      - name: set host
        set_fact:
          IN_PARA: "{{ hostvars[host1_full]['host1_output']['stdout']}}"

      - name:running PS1 file
        win_shell: "main.ps1 -paramater {{ IN_PARA }}"
        register: output


      when: inventory_hostname_short == host2_short

因此,要从不同的主机访问任何文件,都需要完整的主机名。考虑到以下
inventory/test\u inventory.yml

---
全部:
主持人:
123.abc.de.com:
345.abc.de.com:
ansible将在
inventory\u hostname
中自动提供所需结果,如下
test.yml
playbook所示

---
-名称:打印长、短存货名称
主持人:全部
收集事实:错误
任务:
-名称:打印信息
调试:
msg:“主机全名是{{inventory\u hostname}}。短名是{{inventory\u hostname\u Short}”
其中:

$ ansible-playbook -i inventories/test_inventory.yml test.yml 

PLAY [print long and short inventory name] *********************************************************************************************************************************************************************************************

TASK [print info] **********************************************************************************************************************************************************************************************************************
ok: [345.abc.de.com] => {
    "msg": "Host full name is 345.abc.de.com. Short name is 345"
}
ok: [123.abc.de.com] => {
    "msg": "Host full name is 123.abc.de.com. Short name is 123"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
123.abc.de.com             : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
345.abc.de.com             : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

如前所述,
ansible_主机名
ansible_fqdn
是关于您的资源清册中主机的自动事实。但是,您的需求有点独特。因此,我们可能必须应用一种独特的方法来实现这一点。像这样的怎么样

考虑以下示例库存:

---
全部:
主持人:
192.168.1.101:#123.abc.de.com
192.168.1.102:#345.abc.de.com
我们可以演这样一出戏:

-主机:所有
变量:
host1_short:123
host2_short:345
任务:
-命令:“主机名-f”
寄存器:结果
-区块:
-设定事实:
主机1_已满:“{{result.stdout}”
-调试:
msg:“主机1全名:{{Host1_full}}”
时间:主机1\u在result.stdout中短路

您想使用主机的FQDN吗?如果您的基础结构中有带有FQDN的标准命名系统,您可以使用
ansible\u FQDN
变量。@Seshadri C感谢您的回复。我只想将其中一个主机名存储为变量。但唯一的一个特别的是,它缺少主机1。你知道我如何存储为变量吗?@SeshadriC我刚刚更新了一个问题。如果您能提供帮助,那就太好了。谢谢,Zeitounator,但我想将其中一个{{inventory_hostname}存储为变量,以便在下面的两个块中使用它。因为我有两个{{inventory\u hostname\u short}我在设置条件时尝试了{u facts},但是我得到了一个未定义变量的错误。它已经存储在varialbe
inventory\u hostname
为什么需要复制它?我只是更新了这个问题。我需要使用一个特殊的变量,以便在其中一个完整的主机名是必需的,这就是为什么。!很抱歉,我完全不明白你在用你当前的代码做什么。但是如果这是您需要的,那么代码的实际问题是什么?预期的结果是什么?你得到了什么?现在你可能会得到我想要说的。我刚刚更新了问题。所以在block2中,我需要完整的主机名。