jinja2ansiblerackspaceansible-2.x

Using ansible facts as lists


I'm debugging a set of nested plays that puts hosts in a Rackspace load balancer.

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

in the first play, I am using the rax_clb module to create the servers. We register the variable rax and use the rax.success list within it to add those hosts to a group in 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

When using rax.success within the other play using with_items, it works. But later on when I try to use rax_servers in add_to_load_balancers.yml:

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

I get an error that there is no rax_accessipv4 in item. I should, though, since this is how I use it in the previous play (and it works). So I print out rax_servers:

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

I'm obviously doing something wrong, but I can't seem to figure out from the documentation what I am doing wrong when either storing or referencing this variable. Both plays are run from and on localhost, so it should be giving me the same list, no?

Thanks for bearing with this newbie, any help is appreciated :)


Solution

  • It should be:

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

    Without double braces in this case, 'rax.success' is just a string.