由于在多个任务中注册了相同的变量,找不到 Ansible 错误属性 [英] Ansible error attribute not found due to same variable registration in multiple tasks

查看:21
本文介绍了由于在多个任务中注册了相同的变量,找不到 Ansible 错误属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的剧本,它只执行两个 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

不幸的是,我收到没有这样的属性错误";.stdout 用于最后一个 debug 任务

Unfortunately, i get "no such Atrribute error" .stdout for the last debug task

您能否建议我们如何处理这种情况,以便无论操作系统是solaris还是非solaris,最后一次调试都不会失败,并且我们不必为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
    }
}

解决方案:

  1. 解决方案是由 ansible 文档本身提出的,使用标签或使用两个不同的变量.
  2. 使用 Jina2 表达式对 Solarisnon-Solaris 远程只有一项任务.通过这样做,您不必处理跳过或失败的变量.
  1. The solution is suggested by the ansible doc itself, Use tags or use two different variables.
  2. Use Jina2 expression to have only one task for Solaris and non-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" 

这篇关于由于在多个任务中注册了相同的变量,找不到 Ansible 错误属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆