Ansible从vars_file中读取多个具有相同名称的变量 [英] Ansible read multiple variables with same name from vars_file

查看:406
本文介绍了Ansible从vars_file中读取多个具有相同名称的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的~/ip_vars_file中,我有

ip : 10.20.30
ip : 10.20.31
ip : 10.20.32

这是使用lineinfile创建的,

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

我无法将所有三个IP读为with_items.我只获得剧本中的最后一个IP.

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

我得到的输出是

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

我想要的输出是这样的

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

我想逐个迭代ips.我该如何实现?

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

基本上,我想在启动时存储实例的ip,并在以后的部署中使用它们.但是当我启动多个具有相同名称的实例时,我会陷入困境

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

推荐答案

您要定义列表:

---
ip:
  - 10.20.30
  - 10.20.31
  - 10.20.32

这篇关于Ansible从vars_file中读取多个具有相同名称的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆