本文介绍了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?

基本上,我想在启动时存储实例的 ips,并在稍后部署期间使用它们.但是当我启动多个同名实例时我卡住了

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 中读取多个同名变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 20:46