JSON格式的Ansible动态清单没有被解析。

问题描述 投票:-1回答:1

我试着用python写了一个动态库存脚本,内容如下。

[root@satellite code]# python3 dynamic_inventory.py --list > hosts
[root@satellite code]# cat hosts
{"k8s-master": ["k8s-master.example.com"], "k8s-worker": ["worker-1.example.com", "worker-2.example.com"], "satellite": ["satellite.example.com"], "all": ["satellite.example.com", "worker-2.example.com", "k8s-master.example.com", "worker-1.example.com"], "Centos7": ["k8s-master.example.com", "worker-1.example.com", "worker-2.example.com", "satellite.example.com"]}

现在,当我试图使用库存时,我得到了以下错误。

[root@satellite code]# ansible-playbook -i hosts sample.yaml
[WARNING]: Skipping 'Centos7' as this is not a valid group definition
[WARNING]: Skipping 'k8s-worker' as this is not a valid group definition
[WARNING]: Skipping 'satellite' as this is not a valid group definition
[WARNING]: Skipping 'k8s-master' as this is not a valid group definition
[WARNING]: Skipping 'all' as this is not a valid group definition
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [all] *************************************************************************************************************************************************************
skipping: no hosts matched

PLAY RECAP *************************************************************************************************************************************************************

我也试过这个

[root@satellite code]# ansible-playbook -i dynamic_inventory.py sample.yaml
[WARNING]:  * Failed to parse /root/code/dynamic_inventory.py with ini plugin: /root/code/dynamic_inventory.py:1: Expected key=value host variable assignment, got: os
[WARNING]: Unable to parse /root/code/dynamic_inventory.py 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'

PLAY [all] *************************************************************************************************************************************************************
skipping: no hosts matched

PLAY RECAP *************************************************************************************************************************************************************

脚本从MySQL数据库中获取主机名变成JSON格式。我在ansible官方文档中找不到任何插件。有谁能帮忙解决这个问题吗?有没有什么方法可以将脚本输出渲染成ini格式。

python json ansible ini
1个回答
0
投票

我试着把脚本改成可执行的,得到的结果是这样的。

[root@satellite code]# ansible-playbook -i dynamic_inventory.py sample.yaml
[WARNING]:  * Failed to parse /root/code/dynamic_inventory.py with script plugin: problem running /root/code/dynamic_inventory.py --list ([Errno 8] Exec format error)
[WARNING]:  * Failed to parse /root/code/dynamic_inventory.py with ini plugin: /root/code/dynamic_inventory.py:1: Expected key=value host variable assignment, got: os

注意,这次用的是脚本插件,"Exec格式错误 "是因为我的系统默认为python2.在文件中添加了shebang(#!usrbinpython3),就成功了。

[root@satellite code]# ansible-playbook -i dynamic_inventory.py sample.yaml
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this will change, but still be user
configurable on deprecation. This feature will be removed in version 2.10. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [all] *************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
fatal: [worker-1.example.com]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname worker-1.example.com: Name or service not known", "unreachable": true}
fatal: [worker-2.example.com]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname worker-2.example.com: Name or service not known", "unreachable": true}
fatal: [k8s-master.example.com]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname k8s-master.example.com: Name or service not known", "unreachable": true}
fatal: [satellite.example.com]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}

PLAY RECAP *************************************************************************************************************************************************************
k8s-master.example.com    : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0
satellite.example.com     : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0
worker-1.example.com      : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0
worker-2.example.com      : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

© www.soinside.com 2019 - 2024. All rights reserved.