簡體   English   中英

Playbook中的Ansible動態變量

[英]Ansible Dynamic Variables in Playbook

我正在物理主機上配置一堆系統(VM)。 我已經做好了運行VM的步驟。 現在,我需要通過其DHCP地址進入VM。 我可以從服務器提取IP地址,但是我需要一種將它們設置為host_vars的方法。 這是我的小組:

ok: [kvm01] => {
    "msg": {
        "all": [
            "kvm01",
            "dcos-master-1",
            "dcos-agent-1",
            "dcos-agent-2",
            "dcos_bootstrap"
        ],
        "dcos": [
            "dcos-master-1",
            "dcos-agent-1",
            "dcos-agent-2",
            "dcos_bootstrap"
        ],
        "dcos-agents": [
            "dcos-agent-1",
            "dcos-agent-2"
        ],
        "dcos-bootstraps": [
            "dcos_bootstrap"
        ],
        "dcos-masters": [
            "dcos-master-1"
        ],
        "kvm": [
            "kvm01"
        ],
        "ungrouped": []
    }
}

這是我的命令:

- name: Get the IP of the VM (DHCP)
  command: "/getip.sh {{ item }}"
  register: "result"
  with_items: "{{ groups['dcos'] }}"

- name: List the output of the variables
  debug: msg="{{item.stdout}}"
  with_items: "{{result.results}}"

當我運行劇本時,會得到結果,但它們是命令的完整JSON結果,而不是標准輸出。 可能有一種方法可以提取標准輸出並將其分配給事實,但這是一個復雜的哈希。 這是最后的結果:

TASK [vm_install : Get the IP of the VM (DHCP)] ***************************************************************************
changed: [kvm01] => (item=dcos-master-1)
changed: [kvm01] => (item=dcos-agent-1)
changed: [kvm01] => (item=dcos-agent-2)
changed: [kvm01] => (item=dcos_bootstrap)

TASK [vm_install : List the output of the variables] **********************************************************************
......
    ok: [kvm01] => (item={'_ansible_parsed': True, 'stderr_lines': [u'] => {
    "item": {
        "changed": false,
        "cmd": [
            "/getip.sh",
            "dcos_bootstrap"
        ],
        "delta": "0:00:00.056193",
        "end": "2017-09-18 15:45:45.409693",
        "invocation": {
            "module_args": {
                "_raw_params": "/getip.sh dcos_bootstrap",
                "_uses_shell": false,
                "chdir": null,
                "creates": null,
                "executable": null,
                "removes": null,
                "warn": true
            }
        },
        "item": "dcos_bootstrap",
        "rc": 0,
        "start": "2017-09-18 15:45:45.353500",
        "stderr": " ",
        "stdout": "192.168.1.130",
        "stdout_lines": [
            "192.168.1.130"
        ]
    },
    "msg": "192.168.1.130"
}

如何將命令的輸出放入數組中,以便以后在劇本中使用它?

因此,就像我在評論中說的那樣,您已經設法將所需的信息提取到數組中。 您可以使用with_items遍歷那些項目,如以下任務所示,它將為每個主機創建一個ip_address

- set_fact:
    ip_address:  "{{ item.stdout }}"
  with_items: "{{ results.results }}"
  delegate_to: "{{ item.item }}"
  delegate_facts: true

或者,您可以使用Jinja過濾器創建一個包含所有地址的單個數組:

- set_fact:
    all_ip_addresses: "{{ results.results|map(attribute='stdout')|list }}"

或者,您可以創建具有相同信息的字典:

- set_fact:
    all_ip_addresses: >
      {{ all_ip_addresses
         |default({})
         |combine({ item.item: item.stdout })}}
  with_items: "{{ results.results }}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM