Ansible ServiceNow动态Inventory中Compose字符串变量不生效问题
ServiceNow Ansible动态Inventory配置问题:WinRM变量仅数字值生效
我使用servicenow.servicenow.now插件配置Ansible动态Inventory,希望为从ServiceNow拉取的所有主机设置WinRM Kerberos连接类型。但通过compose选项配置相关变量时,发现仅纯数字的ansible_password生效,其他字母数字混合的字符串变量均未被正确解析。尝试使用单引号、无引号均无效,插件文档也未给出相关说明,附上配置及结果示例,请求帮助排查原因。
配置示例
# Simple Inventory Plugin example plugin: servicenow.servicenow.now instance: myamazingcompany username: username password: 'amazingpassword' filter_results: 'install_status=5' fields: [name,support_group] table: computers use_extra_vars: yes keyed_groups: - key: sn_support_group | lower prefix: '' separator: '' compose: ansible_user: "my_windows_user" ansible_password: "4567" connection: "winrm" ansible_winrm_transport: "kerberos" ansible_winrm_server_cert_validation: "ignore"
结果示例
[...] "windows_server_1": { "ansible_password": "4567", "sn_name": "windows_server_1", "sn_support_group": "windows_support" }, "windows_server_2": { "ansible_password": "4567", "sn_name": "windows_server_2", "sn_support_group": "windows_support" }, [...]
排查与解决思路
修正YAML语法与解析方式
确保compose中的字符串用单引号包裹,同时严格保证YAML文件的缩进为空格(禁止用tab),避免解析歧义:compose: ansible_user: 'my_windows_user' ansible_password: '4567' connection: 'winrm' ansible_winrm_transport: 'kerberos' ansible_winrm_server_cert_validation: 'ignore'强制Jinja2字符串声明
插件的compose字段支持Jinja2表达式,可能会将纯字符串误判为变量引用。用Jinja2语法强制声明字符串常量:compose: ansible_user: "{{ 'my_windows_user' }}" ansible_password: "{{ '4567' }}" connection: "{{ 'winrm' }}" ansible_winrm_transport: "{{ 'kerberos' }}" ansible_winrm_server_cert_validation: "{{ 'ignore' }}"检查变量覆盖情况
执行ansible-inventory --list -i your_inventory.yml查看最终生成的Inventory变量,确认是否有其他配置(如静态Inventory、group_vars/host_vars)覆盖了compose中设置的值。更新插件版本
旧版本的servicenow.servicenow插件可能存在compose字段解析bug,执行命令更新插件:ansible-galaxy collection install servicenow.servicenow --upgrade
内容的提问来源于stack exchange,提问作者Hoguss
相关产品推荐
相关产品推荐

