2

嗨,我是 Ansible 的新手,我在使用注册变量时遇到了一些问题。

问题是我有多个具有不同操作系统的服务器。

我使用角色来分隔它们,但是在没有一种服务器的情况下,条件会给我一个错误,说我必须放置一个计算结果为 True 或 False 的表达式。

这是有问题的代码。

 - name: Checking if Sources are Available
   action: shell echo a$(cat /etc/apt/sources.list | grep $(echo   'http://url/${ansible_distribution}/stable' | tr "[:upper:]" "[:lower:]"  ))
   register: sources
   ignore_errors: True

 - name: Adding source.
   action: shell echo "deb http://url/${ansible_distribution}/stable      ${ansible_lsb.codename} main" | tr "[:upper:]" "[:lower:]" >> /etc/apt/sources.list
   when: "ansible_os_family == 'RedHat' and sources.stdout == 'a'"

给我的错误是这个:

fatal: [192.168.1.114] => Conditional expression must evaluate to True or False: ({% if     ansible_os_family == 'RedHat' and sources.stdout == 'a' %} True {% else %} False {% endif   %}) and ({% if ansible_os_family == 'Debian' %} True {% else %} False {% endif %})
fatal: [192.168.1.141] => Conditional expression must evaluate to True or False: ({% if ansible_os_family == 'RedHat' and sources.stdout == 'a' %} True {% else %} False {% endif   %}) and ({% if ansible_os_family == 'Debian' %} True {% else %} False {% endif %})

 FATAL: all hosts have already failed -- aborting

我已经尝试过这些: when: sources.stdout is defined and source.stdout == a only_if: sources.stdout is defined and source.stdout == a

这给了我同样的错误。

我在 Ubuntu 13.04 中使用 Ansible 1.3 为了检查这一点,我使用了 ansible --version

希望您能够帮助我。问候

4

3 回答 3

1

我认为这与这个问题有关: https ://github.com/ansible/ansible/issues/3460

现在似乎已在当前开发版本的 ansible 中修复。

于 2013-08-03T15:56:42.890 回答
0

我在我的 mbp 上使用 ansible 1.2 测试代码,它工作正常:

---
- hosts: local
  tasks:
    - name: Checking if Sources are Available
      action: shell echo a$(cat /etc/apt/sources.list | grep $(echo   'http://url/${ansible_distribution}/stable' | tr "[:upper:]" "[:lower:]"  ))
      register: sources
      ignore_errors: True

    - debug: msg="sources value is ${sources.stdout} "
    - name: Adding source.
      action: shell echo "deb http://url/${ansible_distribution}/stable      ${ansible_lsb.codename} main" | tr "[:upper:]" "[:lower:]" >> /etc/apt/sources.list
      when: "ansible_os_family == 'RedHat' and sources.stdout == 'a'"

结果:

ansible-playbook test.yml

PLAY [local] ******************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [Checking if Sources are Available] *************************************
changed: [localhost]

TASK: [debug msg="sources value is ${sources.stdout} "] ***********************
ok: [localhost] => {"msg": "sources value is a "}

TASK: [Adding source.] ********************************************************
skipping: [localhost]

PLAY RECAP ********************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=0

可以参考官方例子:register_logic

于 2013-06-29T12:35:25.010 回答
-3

可能是你有:

  gather_facts: false

在你的剧本中?

于 2013-07-09T23:36:19.350 回答