编写自定义 Ansible 模块时 bool 类型的实现错误
Implementation error for type bool when writing custom Ansible module
我正在尝试在 Ansible 2.3 中编写自定义模块以在我的剧本中使用。它的详细信息已被抽象为 运行 下面的最小 date
命令,但我正在开发一个非常复杂的示例。
[dev@foobar Gist]$ tree ansible_version_err/
ansible_version_err/
├── dev_command.yml
└── library
└── dev_command.py
库文件的内容是使用ansible.module
包来实现自定义命令。
#!/usr/bin/python
from ansible.module_utils.basic import *
from subprocess import Popen, PIPE
import time
if __name__ == '__main__':
module = AnsibleModule(
argument_spec={
'command': { 'type':'str', 'required': True },
'print_elapsed': { 'type':bool, 'required': False },
'print_time': { 'type':bool, 'required': False } } )
stdout = None
stderr = None
rc = None
try:
time_start = time.time()
proc = Popen( module.params['command'], shell = True)
stdout, stderr = proc.communicate()
time_end = time.time()
rc = proc.returncode
if rc != 0:
raise Exception("error while obtaining date")
time_elapsed = time_end - time_start
if 'print_elapsed' in module.params and module.params['print_elapsed']:
stdout = stdout + "\nelapsed time: " + str(time_elapsed)
if 'print_time' in module.params and module.params['print_time']:
stdout = stderr + "\ntime: " + time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(time_start))
module.exit_json( changed = False, stdout = stdout, stderr = stderr, rc = rc, time_elapsed = time_elapsed )
except Exception:
pass
并且使用此 dev_command
的 .yml
文件称为
---
- hosts: localhost
gather_facts: no
tasks:
- name: testing a custom command
vars:
print_stdout: True
dev_command: command="{{cmd}}" print_elapsed=True print_time=True
我只是用 date
调用 cmd
,它将继续,运行 日期命令。
ansible-playbook dev_command.yml -vvv -e "cmd=date"
虽然我希望这项工作可以在 Ansible 2.3 上运行,但它向我抛出一个错误
fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"invocation": {
"module_args": {
"command": "date",
"print_elapsed": "True",
"print_time": "True"
}
},
"msg": "implementation error: unknown type <type 'bool'> requested for print_time"
}
此错误在 v2.6 中未出现。为什么会这样?这是一个已知的 Ansible 功能错误吗,只能通过移动到更新版本来解决。版本详情
$ ansible --version
ansible 2.3.0.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
Q: "On Ansible 2.3, it is throwing me an error ... This error is not seen in v2.6. Why is this happening? Is this a known Ansible feature bug that can only be resolved by moving to a newer version?"
答:是的。有必要移动到维护版本。即使这是一个已知问题,也不会再修复。 Ansible Release cycle 说:
"Ansible has a graduated maintenance structure that extends to three major releases ... we strongly encourage you to upgrade as soon as possible ..."
目前最新的Ansible版本是2.9。目前最后维护的版本是2.7。
FWIW。命令
$ grep -r type /scratch/ansible/lib/ansible/modules/ | grep bool | grep defaul
显示所有模块用引号声明布尔类型
type='bool'
或
'type': 'bool'
我正在尝试在 Ansible 2.3 中编写自定义模块以在我的剧本中使用。它的详细信息已被抽象为 运行 下面的最小 date
命令,但我正在开发一个非常复杂的示例。
[dev@foobar Gist]$ tree ansible_version_err/
ansible_version_err/
├── dev_command.yml
└── library
└── dev_command.py
库文件的内容是使用ansible.module
包来实现自定义命令。
#!/usr/bin/python
from ansible.module_utils.basic import *
from subprocess import Popen, PIPE
import time
if __name__ == '__main__':
module = AnsibleModule(
argument_spec={
'command': { 'type':'str', 'required': True },
'print_elapsed': { 'type':bool, 'required': False },
'print_time': { 'type':bool, 'required': False } } )
stdout = None
stderr = None
rc = None
try:
time_start = time.time()
proc = Popen( module.params['command'], shell = True)
stdout, stderr = proc.communicate()
time_end = time.time()
rc = proc.returncode
if rc != 0:
raise Exception("error while obtaining date")
time_elapsed = time_end - time_start
if 'print_elapsed' in module.params and module.params['print_elapsed']:
stdout = stdout + "\nelapsed time: " + str(time_elapsed)
if 'print_time' in module.params and module.params['print_time']:
stdout = stderr + "\ntime: " + time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(time_start))
module.exit_json( changed = False, stdout = stdout, stderr = stderr, rc = rc, time_elapsed = time_elapsed )
except Exception:
pass
并且使用此 dev_command
的 .yml
文件称为
---
- hosts: localhost
gather_facts: no
tasks:
- name: testing a custom command
vars:
print_stdout: True
dev_command: command="{{cmd}}" print_elapsed=True print_time=True
我只是用 date
调用 cmd
,它将继续,运行 日期命令。
ansible-playbook dev_command.yml -vvv -e "cmd=date"
虽然我希望这项工作可以在 Ansible 2.3 上运行,但它向我抛出一个错误
fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"invocation": {
"module_args": {
"command": "date",
"print_elapsed": "True",
"print_time": "True"
}
},
"msg": "implementation error: unknown type <type 'bool'> requested for print_time"
}
此错误在 v2.6 中未出现。为什么会这样?这是一个已知的 Ansible 功能错误吗,只能通过移动到更新版本来解决。版本详情
$ ansible --version
ansible 2.3.0.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
Q: "On Ansible 2.3, it is throwing me an error ... This error is not seen in v2.6. Why is this happening? Is this a known Ansible feature bug that can only be resolved by moving to a newer version?"
答:是的。有必要移动到维护版本。即使这是一个已知问题,也不会再修复。 Ansible Release cycle 说:
"Ansible has a graduated maintenance structure that extends to three major releases ... we strongly encourage you to upgrade as soon as possible ..."
目前最新的Ansible版本是2.9。目前最后维护的版本是2.7。
FWIW。命令
$ grep -r type /scratch/ansible/lib/ansible/modules/ | grep bool | grep defaul
显示所有模块用引号声明布尔类型
type='bool'
或
'type': 'bool'