问题
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