Ansible read multiple variables with same name from vars_file

岁酱吖の 提交于 2020-01-05 13:01:11

问题


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:


You want to define a list:

---
ip:
  - 10.20.30
  - 10.20.31
  - 10.20.32


来源:https://stackoverflow.com/questions/43656563/ansible-read-multiple-variables-with-same-name-from-vars-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!