使用变量输入的Ansible命令无法执行



下面我试图运行一个涉及以前输出的输入的命令,在本例中为copy flash:file01 flash:file022源自REGEX_FILTERING任务。

- name: REGEX_FILTERING
command: "cat /home/ubuntu/show_switch.txt"
register: line1
- debug: var="{{ line1.stdout_lines | regex_replace('(?<!*).?') }}"
register: number
- name: COMMAND_INPUT
cli_command:
command: 'copy flash:file01 flash:file0 {{ number }}'
prompt: '[confirm]'
answer: ''

这是错误消息:

<ommited>
"stdout_lines": [
"Switch/Stack Mac Address : d4a0.2ae9.ec00",
"                                           H/W   Current",
"----------------------------------------------------------",
" 1       Member 001f.9df6.2d80     5      0       Ready               ",
"*2       Master d4a0.2ae9.ec00     15     0       Ready               ",
" 3       Member 0016.c733.cd00     5      0       Ready"
]
}
TASK [debug] *****************************************************************************************************************************************************************************
task path: /home/ubuntu/test1.yml:109
ok: [CSR1] => {
"2": "2"
}
TASK [COMMAND_INPUT] *********************************************************************************************************************************************************************
task path: /home/ubuntu/test1.yml:112
<192.168.255.133> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
<192.168.255.133> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" && echo ansible-tmp-1542986468.48-164922344690120="` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" ) && sleep 0'
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/network/cli/cli_command.py
<192.168.255.133> PUT /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/tmpkozHVx TO /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py
<192.168.255.133> EXEC /bin/sh -c 'chmod u+x /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
<192.168.255.133> EXEC /bin/sh -c '/usr/bin/python /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
<192.168.255.133> EXEC /bin/sh -c 'rm -f -r /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
File "/tmp/ansible_cli_command_payload_U1p385/__main__.py", line 150, in main
response = connection.get(**module.params)
File "/tmp/ansible_cli_command_payload_U1p385/ansible_cli_command_payload.zip/ansible/module_utils/connection.py", line 173, in __rpc__
raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
fatal: [CSR1]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"answer": [
""
],
"check_all": false,
"command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",
"prompt": [
"[confirm]"
],
"sendonly": false
}
},
"msg": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}rn                                   ^rn% Invalid input detected at '^' marker.rnrnCSR1#rnCSR1#"

"command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",

之所以发生,是因为register: number实际上不是数字,而是debug:输出的可靠结果。由于number是一个pythondict,当它被注入到您的命令中时,它只是被序列化,就好像str()是在dict上调用的一样

更有可能的是,您想要的是在任务中本地捕获该变量,使其只是是jinja2表达式的结果,而不是可解析任务的结果:

- cli_command:
command: 'copy flash:file01 flash:file0 {{ number }}'
vars:
number: "{{ line1.stdout_lines | regex_replace('(?<!*).?') }}"

我还怀疑你会更喜欢line1.stdout,它是整个输出的str,而line1.stdout_linesstrlist,这使得list上的regex_replace有点奇怪——老实说,甚至让你这么做,我很惊讶

最新更新