写入自定义Ansible模块时类型bool的实现错误
我正在尝试用Ansible 2.3编写一个自定义模块,以便在我的剧本中使用。它的细节已经被抽象为运行下面一个最小的写入自定义Ansible模块时类型bool的实现错误,ansible,ansible-2.x,Ansible,Ansible 2.x,我正在尝试用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/pytho
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\u命令的.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
,它将继续运行date命令
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"
}
问:“在Ansible 2.3上,它向我抛出了一个错误……在v2.6中没有看到这个错误。为什么会发生这种情况?这是一个已知的Ansible功能缺陷,只有通过移动到新版本才能解决吗?”
A:是的。有必要转移到维护版本。即使这是一个已知的问题,它也不会再被修复。Ansible说:
“Ansible有一个分级维护结构,可扩展到三个主要版本…我们强烈建议您尽快升级…”
目前最新的Ansible版本是2.9。目前最新维护的版本是2.7
FWIW。命令
$ grep -r type /scratch/ansible/lib/ansible/modules/ | grep bool | grep defaul
显示用引号声明布尔类型的所有模块
type='bool'
或
虽然这是有道理的,但我想理解为什么会抛出错误,以及更新版本中修复了什么。我在这里看到了类似的问题-,但无法跟踪,修补程序修补到的Ansible版本修补程序似乎引用了值type='bool'
。
type='bool'
'type': 'bool'