由于在多个任务中注册了相同的变量,因此未找到错误错误属性 [英] Ansible error attribute not found due to same variable registration in multiple tasks
问题描述
下面是我的剧本,它仅执行两个 raw
模块之一.
Below is my playbook that executes only one of the two raw
modules.
- name: Get Process Dump for tomcat on non-Solaris
ignore_errors: yes
block:
- raw: "ps -ef | grep java | grep -i tomcat | grep -v grep; awk -vrc=$? 'BEGIN{print \"rc=\"rc}'"
register: tomjavadump
ignore_errors: yes
- debug:
msg: "Tomcat DEBUGGG1: tomjavadump:{{ tomjavadump }}"
when: ansible_distribution != 'Solaris'
- name: Get Process Dump for tomcat on Solaris
ignore_errors: yes
block:
- raw: "ps auxwww | grep java | grep -i tomcat | grep -v grep; awk -vrc=$? 'BEGIN{print \"rc=\"rc}'"
register: tomjavadump
ignore_errors: yes
- debug:
msg: "Tomcat DEBUGGG2: tomjavadump:{{ tomjavadump }}"
when: ansible_distribution == 'Solaris'
- debug:
msg: "Process found"
when: tomjavadump.stdout is regex('rc=0')
具有完整调试 -vvv
的输出:
ansible-playbook -i=all, blocktest.yml -vvv
ansible-playbook 2.9.4
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/user1/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.5 (default, Sep 26 2019, 13:23:47) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /etc/ansible/ansible.cfg as config file
Parsed all, inventory source with host_list plugin
[WARNING]: Found both group and host with same name: all
PLAYBOOK: blocktest.yml ************************************************************************************************************************************************
1 plays in blocktest.yml
PLAY [Play 3- check telnet nodes] **************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
task path: /app/playbook/blocktest.yml:1
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
Pipelining is enabled.
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: user1
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 && sleep 0'
ok: [localhost]
META: ran handlers
TASK [raw] *************************************************************************************************************************************************************
task path: /app/playbook/blocktest.yml:8
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: user1
<127.0.0.1> EXEC ps -ef | grep tomcat
changed: [localhost] => {
"changed": true,
"rc": 0,
"stderr": "",
"stderr_lines": [],
"stdout": "user1 46691 46690 0 05:15 pts/106 00:00:00 /bin/sh -c ps -ef | grep tomcat\nuser1 46693 46691 0 05:15 pts/106 00:00:00 grep tomcat\n",
"stdout_lines": [
"user1 46691 46690 0 05:15 pts/106 00:00:00 /bin/sh -c ps -ef | grep tomcat",
"user1 46693 46691 0 05:15 pts/106 00:00:00 grep tomcat"
]
}
TASK [debug] ***********************************************************************************************************************************************************
task path: /app/playbook/blocktest.yml:10
ok: [localhost] => {
"msg": "TOMCAT DEBUGGGG1: {'stderr_lines': [], 'stdout': u'user1 46691 46690 0 05:15 pts/106 00:00:00 /bin/sh -c ps -ef | grep tomcat\\nuser1 46693 46691 0 05:15 pts/106 00:00:00 grep tomcat\\n', 'changed': True, 'failed': False, 'stderr': u'', 'rc': 0, 'stdout_lines': [u'user1 46691 46690 0 05:15 pts/106 00:00:00 /bin/sh -c ps -ef | grep tomcat', u'user1 46693 46691 0 05:15 pts/106 00:00:00 grep tomcat']}"
}
TASK [raw] *************************************************************************************************************************************************************
task path: /app/playbook/blocktest.yml:19
skipping: [localhost] => {
"changed": false,
"skip_reason": "Conditional result was False"
}
TASK [debug] ***********************************************************************************************************************************************************
task path: /app/playbook/blocktest.yml:21
skipping: [localhost] => {}
TASK [debug] ***********************************************************************************************************************************************************
task path: /app/playbook/blocktest.yml:26
fatal: [localhost]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to be in '/app/playbook/blocktest.yml': line 26, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n"
}
PLAY RECAP *************************************************************************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=1 skipped=2 rescued=0 ignored=0
不幸的是,我收到没有这样的Atrribute错误".最后一个 debug
任务的.stdout
Unfortunately, i get "no such Atrribute error" .stdout for the last debug
task
您能建议我们如何处理这种情况,以便无论操作系统是solaris还是非solarris,最后的调试都不会失败,并且我们不必为 tomjavadump使用新的变量名代码>?
Can you please suggest how can we take care of such situation so that the the last debug does not fail whether the OS is solaris or non-solaris and we dont have to use a new variable name for tomjavadump
?
推荐答案
以下是 ansible-doc :
如果任务失败或被跳过,Ansible仍将变量注册为失败或跳过状态,除非根据标签跳过了任务.有关添加和使用标签的信息,请参见标签.
If a task fails or is skipped, Ansible still registers a variable with a failure or skipped status, unless the task is skipped based on tags. See Tags for information on adding and using tags.
跳过第一个任务时,它会用值覆盖它.
When you are skipping the first task it is overwriting it with the values.
简单的例子:
---
- hosts: localhost
gather_facts: no
tasks:
- name: "Running date command expecting 2020 in stdout and storing to var1"
raw: date
args:
executable: /bin/bash
register: var1
when: True
- debug: var=var1.stdout
- name: "Running date command expecting 2020 in stdout and storing to var1"
raw: date
args:
executable: /bin/bash
register: var1
when: False
- debug: var=var1.stdout
- debug: var=var1
在上面的示例中,第二次调试将产生与您报告的错误相同的错误.但是,第三次调试将产生以下错误,因为可以看出其中没有 stdout
.
In the above example, 2nd debug would produce the same error as reported by you. However 3rd debug would produce below error, as it can be seen that there is no stdout
in it.
ok: [localhost] => {
"var1": {
"changed": false,
"skip_reason": "Conditional result was False",
"skipped": true
}
}
解决方案:
- 该解决方案由ansible文档本身建议,使用标签或使用两个不同的变量.
- 使用Jina2表达式对于
Solaris
和non-Solaris
远程服务器仅具有一项任务.这样,您就不必处理跳过或失败的变量.
- The solution is suggested by the ansible doc itself, Use tags or use two different variables.
- Use Jina2 expression to have only one task for
Solaris
andnon-Solaris
remote. By doing this, you do not have to deal with the skipped or failed variable.
示例:
- raw: "{{ 'ps -eaf' if ansible_distribution != 'Solaris' else 'ps auxwww'}} |grep ....bla ...bla...bla"
这篇关于由于在多个任务中注册了相同的变量,因此未找到错误错误属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!