Ansible read multiple variables with same name fro

2019-08-01 09:41发布

In my ~/ip_vars_file , I have

ip : 10.20.30
ip : 10.20.31
ip : 10.20.32

This is created with lineinfile,

lineinfile: line="ip{{':'}} {{item.public_ip}}"
              dest="{{ansible_env.HOME}}/ip_vars_file}}"
with_items: servers.tagged_instances #servers is registered in previous task

I am unable to read all three ips as with_items. I get only the last IP in my playbook.

---
- hosts: localhost
  tasks:
  - name: print ips
    debug:
      var: item 
    with_items: "{{ ip }}"

  vars_files:
  - ~/ip_vars_file

The output I am getting is,

TASK [print ips] ***************************************************************
ok: [localhost] => (item=10.20.32) => {
    "item": "10.20.32"
}

The output I want is something like this

TASK [print ips] ***************************************************************
ok: [localhost] => (item=10.20.32) => {
    "item": "10.20.30"
    "item": "10.20.31"
    "item": "10.20.32"
}

I want to iterate over the ips one by one. How do I achieve this?

Basically, I want to store the ips of instances when launching and use them later during deployment. But I am stuck when I launch multiple instances with same name

1条回答
何必那么认真
2楼-- · 2019-08-01 10:05

You want to define a list:

---
ip:
  - 10.20.30
  - 10.20.31
  - 10.20.32
查看更多
登录 后发表回答