使用ansible事实作为列表

时间:2016-07-18 19:07:08

标签: ansible jinja2 ansible-playbook rackspace ansible-2.x

我正在调试一组嵌套播放,将主机置于Rackspace负载均衡器中。

- include create_servers.yml
...
- include add_to_load_balancers.yml

在第一次播放中,我使用rax_clb模块来创建服务器。我们注册变量rax并使用其中的rax.success列表将这些主机添加到create_servers.yml中的组:

- name: create instances on Rackspace
  local_action:
    module: rax
    image: "{{ IMAGE }}"
    flavor: "{{ FLAVOR }}"
    wait: yes 
    count: "{{ COUNT }}"
    ...
    register: rax


- name: some other play
  local_action:
  ...
  with_items: rax.success


- name: register rax.success as rax_servers for later use
  set_fact:
    rax_servers: rax.success

使用with_items在其他游戏中使用rax.success时,它可以正常工作。但后来当我尝试在add_to_load_balancers.yml中使用rax_servers时:

- name: place new hosts in the load balancer
  rax_clb_nodes:
    address={{ item.rax_accessipv4 }}
    state=present
    ...
  with_items: rax_servers

我收到项目中没有rax_accessipv4的错误。不过,我应该这样,因为这是我在之前的游戏中使用它的方式(并且它有效)。所以我打印出rax_servers

TASK: [debug var=rax_servers] ************************************************* 
ok: [127.0.0.1] => {
    "var": {
        "rax_servers": "rax.success"
    }
}

我显然做错了什么,但我无法从文档中弄清楚在存储或引用此变量时我做错了什么。这两个游戏都是从localhost运行的,所以它应该给我相同的列表,不是吗?

感谢您对这位新手的支持,感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

应该是:

- name: register rax.success as rax_servers for later use
  set_fact:
    rax_servers: "{{ rax.success }}"

在这种情况下没有双括号,' rax.success'只是一个字符串。