将日志存储在文件中时出现ansible AnsibleUndefinedVariable错误

时间:2017-12-15 05:13:26

标签: ansible

我正在使用剧本来卸载软件程序并且该剧本有效。 该剧本首先使用stat模块确认软件的二进制路径,并使用when条件检查它是否存在并基于它执行操作..

我也在使用shell模块来运行卸载。下面是我的剧本

---
- name: Un-Install altris
  hosts: all
  gather_facts: False
  strategy: free
  serial: 100
  tasks:
  - name: Check if altris stop/start path exists
    stat: path=/opt/altiris/notification/nsagent/bin/rcscript
    register: altris_status

  - name: stop the altris if path exits
    shell: |
      echo `hostname`
      echo "-------------------------------"
      /opt/altiris/notification/nsagent/bin/rcscript stop
    when: altris_status.stat.exists == True
    register: altris_stop

  - name: Check if uninstall binary ptah exists
    stat: path=/opt/altiris/notification/nsagent/bin/aex-uninstall
    register: altris_uninstall

  - name: un-installing altris
    shell: |
      echo `hostname`
      echo "-------------------------------"
      yes | /opt/altiris/notification/nsagent/bin/aex-uninstall
    when: altris_uninstall.stat.exists == True
    register: altris_trim

#############  Storing the logs locally for the STDOUT ############
  - name: Storing the remote Hosts Output locally for altris
    #lineinfile: create=yes dest=/Karn/altris_uninstall.logs  line="{{ altris_trim.stdout }}"
    lineinfile: create=yes dest=/Karn/test_uninstall.logs  line="{{ altris_trim.stdout }}"
    delegate_to: 127.0.0.1

  - name: Storing the remote Hosts Output locally for altris
    #lineinfile: create=yes dest=/Karn/altris1_stop.logs  line="{{ altris_stop.stdout }}"
    lineinfile: create=yes dest=/Karn/test_stop.logs  line="{{ altris_stop.stdout }}"
    delegate_to: 127.0.0.1
  

注意:当我运行playbook时,它工作正常,但错误只出现在它的位置   没有找到卸载的二进制路径并抛出以下内容   mesg errror:

[root@dev-ops Karn]# ansible-playbook altris_unistall2.yml

PLAY [Un-Install altris] ********************************************************************************************************************************************
Friday 15 December 2017  10:35:50 +0530 (0:00:00.112)       0:00:00.112 *******

TASK [Check if altris stop/start path exists] ***********************************************************************************************************************
ok: [dev-oracle]
Friday 15 December 2017  10:36:01 +0530 (0:00:10.179)       0:00:10.292 *******

TASK [stop the altris if path exits] ********************************************************************************************************************************
skipping: [dev-oracle]
Friday 15 December 2017  10:36:01 +0530 (0:00:00.019)       0:00:10.311 *******

TASK [Check if uninstall binary ptah exists] ************************************************************************************************************************
ok: [dev-oracle]
Friday 15 December 2017  10:36:11 +0530 (0:00:10.483)       0:00:20.795 *******

TASK [un-installing altris] *****************************************************************************************************************************************
skipping: [dev-oracle]
Friday 15 December 2017  10:36:11 +0530 (0:00:00.022)       0:00:20.817 *******

TASK [Storing the remote Hosts Output locally for altris] ***********************************************************************************************************
fatal: [dev-oracle]: FAILED! => {
    "failed": true
}

MSG:

The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'

The error appears to have been in '/Karn/altris_unistall2.yml': line 33, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

#############  Storing the logs locally for the STDOUT ############
  - name: Storing the remote Hosts Output locally for altris
    ^ here

exception type: <class 'ansible.errors.AnsibleUndefinedVariable'>
exception: 'dict object' has no attribute 'stdout'

        to retry, use: --limit @/Karn/altris_unistall2.retry

PLAY RECAP **********************************************************************************************************************************************************
dev-oracle                : ok=2    changed=0    unreachable=0    failed=1

Friday 15 December 2017  10:36:11 +0530 (0:00:00.037)       0:00:20.854 *******
===============================================================================
Check if uninstall binary ptah exists ----------------------------------------------------------------------------------------------------------------------- 10.48s
Check if altris stop/start path exists ---------------------------------------------------------------------------------------------------------------------- 10.18s
Storing the remote Hosts Output locally for altris ----------------------------------------------------------------------------------------------------------- 0.04s
un-installing altris ----------------------------------------------------------------------------------------------------------------------------------------- 0.02s
stop the altris if path exits -------------------------------------------------------------------------------------------------------------------------------- 0.02s

如果我需要在我的剧本中使用任何chnage,请确认... 像ingore_error ..

1 个答案:

答案 0 :(得分:0)

un-installing altris任务被跳过,因此altris_trim不包含stdout密钥。

您需要将when: stdout in altris_trim添加到Storing the remote Hosts Output locally for altris或使用与un-installing altris相同的条件。

这同样适用于下一个任务altris_stop.stdout