AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 912235
Accepted
Gerald Schneider
Gerald Schneider
Asked: 2018-05-15 22:41:32 +0800 CST2018-05-15 22:41:32 +0800 CST 2018-05-15 22:41:32 +0800 CST

在多个任务中注册 ansible 变量

  • 772

我在一个在各种服务器上安装更新的 ansible 脚本中使用以下任务。这些任务适用于 CentOS 机器:

- name: Check for outstanding reboot
  shell: needs-restarting > /dev/null || echo Reboot required
  when: ansible_distribution_major_version|int < 7
  register: result
- name: Check for outstanding reboot
  shell: needs-restarting -r > /dev/null || echo Reboot required
  when: ansible_distribution_major_version|int >= 7
  register: result
- name: Report reboot
  debug: msg="{{ result.stdout_lines }}"

结果:

TASK [Check for outstanding reboot] ***********************************************************************************
skipping: [host1]
skipping: [host2]
skipping: [host5]
changed: [host3]
changed: [host4]

TASK [Check for outstanding reboot] ***********************************************************************************
skipping: [host3]
skipping: [host4]
changed: [host2]
changed: [host1]
changed: [host5]

TASK [Report reboot] **************************************************************************************************
fatal: [host3]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to have been in '/path/to/updates.yml': line 52, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    register: result\n  - name: Report reboot\n    ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'stdout_lines'"}
ok: [host1] => {
    "msg": [
        "Reboot required"
    ]
}
fatal: [host4]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to have been in '/path/to/updates.yml': line 52, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    register: result\n  - name: Report reboot\n    ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'stdout_lines'"}
ok: [host2] => {
    "msg": [
        "Reboot required"
    ]
}
ok: [host5] => {
    "msg": [
        "Reboot required"
    ]
}
        to retry, use: --limit @/path/to/updates.retry

如您所见,两个检查任务都将结果存储在一个名为“result”的变量中,但只有第二个任务中的主机变量(带有ansible_distribution_major_version|int >= 7)被填充,从而导致错误消息。似乎第二个检查任务取消了前一个任务的结果。

是否可以保留两个任务的结果?还是我必须复制报告任务并将版本检查添加到两者?我宁愿把报告放在一个地方。

Ansible 版本是 2.4.4.0

ansible
  • 1 1 个回答
  • 20363 Views

1 个回答

  • Voted
  1. Best Answer
    Henrik Pingel
    2018-05-17T05:00:40+08:002018-05-17T05:00:40+08:00

    发生这种情况是因为即使任务被跳过,Ansible 也会存储结果:

    如果任务失败或被跳过,变量仍会以失败或跳过状态注册,避免注册变量的唯一方法是使用标签。

    Ansible 注册变量用户指南

    因此,不,不可能保留这两个任务的结果。

    正如您已经建议的那样,我会复制报告任务并将版本检查添加到两者。

    • 4

相关问题

  • 重复的 Ansible 任务

  • 无法形成站点中的文件的链接,该链接可用于使用 ansible 在远程服务器中启用的目录站点?

  • 如何执行 ansible 的特定角色?

  • Ansible 和 rbash

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve