簡體   English   中英

Ansible 動態清單腳本 - 奇怪的行為

[英]Ansible dynamic inventory script - odd behaviour

我正在嘗試基於 JSON 輸出為 ansible 創建一個基本的動態清單腳本。 我是 jq 的新手,但我遇到了一個問題,即 ansible v2.9.14 和 2.9.15 上的動態腳本不喜歡輸出,但是如果我將輸出發送到文件然后針對輸出運行 Ansible該文件,ansible 作品。

這是發生的事情:

動態清單腳本輸出:

{
  "all": {
      "hosts": {
"ip-172-31-39-30.eu-west-1.compute.internal": null,
"ip-172-31-44-224.eu-west-1.compute.internal": null,
"ip-172-31-42-6.eu-west-1.compute.internal": null,
"ip-172-31-32-68.eu-west-1.compute.internal": null,
    }
  }
}

Ansible 運行和錯誤:

$ ansible -i ./dynamic1.sh all -m ping -u ubuntu
[WARNING]:  * Failed to parse /home/ubuntu/dynamic1.sh with script plugin: failed to parse executable inventory script results from /home/ubuntu/dynamic1.sh:
Expecting property name enclosed in double quotes: line 8 column 5 (char 242)
[WARNING]:  * Failed to parse /home/ubuntu/dynamic1.sh with ini plugin: /home/ubuntu/dynamic1.sh:2: Expected key=value host variable assignment, got: {
[WARNING]: Unable to parse /home/ubuntu/dynamic1.sh as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

現在,如果我將動態腳本輸出到一個文件,然后再次運行 ansible,它會起作用:

$ ./dynamic1.sh > output.json

$ cat output.json
{
  "all": {
      "hosts": {
"ip-172-31-39-30.eu-west-1.compute.internal": null,
"ip-172-31-44-224.eu-west-1.compute.internal": null,
"ip-172-31-42-6.eu-west-1.compute.internal": null,
"ip-172-31-32-68.eu-west-1.compute.internal": null,
    }
  }
}

$ ansible -i output.json all -m ping -u ubuntu
[DEPRECATION WARNING]: Distribution Ubuntu 16.04 on host ip-172-31-42-6.eu-west-1.compute.internal should use /usr/bin/python3, but is using /usr/bin/python for
backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ip-172-31-42-6.eu-west-1.compute.internal | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
ip-172-31-39-30.eu-west-1.compute.internal | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
ip-172-31-32-68.eu-west-1.compute.internal | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
ip-172-31-44-224.eu-west-1.compute.internal | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

所以它有效...

這是dynamic1.sh的內容。 我知道會有更好的方法來做到這一點,但我只需要一個基於 JSON 輸出中 ansible 可以使用的匹配變量的服務器列表。

$ cat dynamic1.sh
#!/bin/bash
echo "{"
echo "  \"all\": {"
echo "      \"hosts\": {"
curl --silent -X GET https://url.com/api/servers -H "Authorization: Token $token" -H "Content-Type: text/json"  -H "Accept:application/json" | jq -r '.Result.servers[] | select(.ansible_local.local.local_facts.instance_type | tostring | contains("t2.micro")) | (.ansible_fqdn+"\": null,")' | sed 's/^/"/g'
echo "    }"
echo "  }"
echo "}"

任何人都可以幫助我解釋為什么 ansible 接受文件而不是腳本的輸出?

Ansible 清單格式相反,清單插件script.py期望屬性hosts是一個列表(例如hosts:[ host1, host2, host3 ] )而不是字典(例如hosts:{ host, host2, host3 } )。


清單插件yaml.py與主機字典一起使用

JSON(或 YAML,因為 JSON 是 YAML 的子集)清單工作正常

shell> cat hosts.json
{
    "all": {
        "hosts": {
            "ip-172-31-39-30.eu-west-1.compute.internal",
            "ip-172-31-44-224.eu-west-1.compute.internal",
            "ip-172-31-42-6.eu-west-1.compute.internal",
            "ip-172-31-32-68.eu-west-1.compute.internal"
        }
    }
}
shell> ansible-inventory -i hosts.json --list -vvv
...
Parsed /scratch/tmp/hosts.json inventory source with yaml plugin
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    },
    "ungrouped": {
        "hosts": [
            "ip-172-31-32-68.eu-west-1.compute.internal",
            "ip-172-31-39-30.eu-west-1.compute.internal",
            "ip-172-31-42-6.eu-west-1.compute.internal",
            "ip-172-31-44-224.eu-west-1.compute.internal"
        ]
    }
}

但是,腳本提供的相同文件將失敗

shell> cat hosts.sh 
#!/bin/bash
cat hosts.json
shell> ansible-inventory -i hosts.sh --list -vvv
...
Parsed /scratch/tmp/hosts.sh inventory source with script plugin

[警告]:無法使用腳本插件解析 /scratch/tmp/hosts.sh:您為主機列表定義了一個包含錯誤數據的組“all”:{'hosts': {'ip-172-31-39-30 .eu-west-1.compute.internal':無,'ip-172-31-44-224.eu-west-1.compute.internal':無,'ip-172-31-42-6.eu -west-1.compute.internal':無,'ip-172-31-32-68.eu-west-1.compute.internal':無}} ...

{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    }
}

庫存插件script.py適用於主機列表

當屬性 hosts 是一個列表時,清單插件script.py按預期工作

shell> cat hosts.json
{
    "all": {
        "hosts": [
            "ip-172-31-39-30.eu-west-1.compute.internal",
            "ip-172-31-44-224.eu-west-1.compute.internal",
            "ip-172-31-42-6.eu-west-1.compute.internal",
            "ip-172-31-32-68.eu-west-1.compute.internal"
        ]
    }
}
shell> ansible-inventory -i hosts.sh --list -vvv
...
Parsed /scratch/tmp/hosts.sh inventory source with script plugin
{
    "_meta": {
       ...
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    },
    "ungrouped": {
        "hosts": [
            "ip-172-31-32-68.eu-west-1.compute.internal",
            "ip-172-31-39-30.eu-west-1.compute.internal",
            "ip-172-31-42-6.eu-west-1.compute.internal",
            "ip-172-31-44-224.eu-west-1.compute.internal"
        ]
    }
}

筆記

  • 腳本hosts.sh未正確實現,僅用於此示例的目的。 script.py引用:

描述: - 提供的源必須是返回 Ansible 清單 JSON 的可執行文件 - 源必須接受 C(--list) 和 C(--host ) 作為參數。 C(--host) 僅在不存在 C(_meta) 鍵時使用。

暫無
暫無

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

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