在Ansible中注册变量

时间:2013-06-14 18:12:45

标签: ubuntu ansible

嗨,我是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已定义且source.stdout == a    only_if:sources.stdout已定义且source.stdout == a

这给了我同样的错误。

我在Ubuntu 13.04中使用Ansible 1.3 为了解决这个问题,我使用了ansible --version

希望你能帮助我。 问候

3 个答案:

答案 0 :(得分:1)

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

现在似乎已经修复了当前的ansible版本。

答案 1 :(得分: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

您可以参考official example:register_logic

答案 2 :(得分:-3)

可能是你有:

  gather_facts: false

在你的剧本中?