【问题标题】:Ansible read multiple variables with same name from vars_fileAnsible 从 vars_file 读取多个同名变量
【发布时间】:2017-04-27 20:23:44
【问题描述】:

在我的~/ip_vars_file 中,我有

ip : 10.20.30
ip : 10.20.31
ip : 10.20.32

这是用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

我无法像 with_items 一样读取所有三个 ip。我只得到我剧本中的最后一个 IP。

---
- 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。我如何做到这一点?

基本上,我想在启动时存储实例的 ips,然后在部署期间使用它们。但是当我启动多个具有相同名称的实例时,我被卡住了

【问题讨论】:

    标签: ansible ansible-2.x ansible-inventory ansible-facts


    【解决方案1】:

    你想定义一个列表:

    ---
    ip:
      - 10.20.30
      - 10.20.31
      - 10.20.32
    

    【讨论】:

    • 我正在使用 ansible lineinfile 创建文件,因此我无法更改定义该文件的方式。
    • 我编辑了这个问题。基本上,我想在启动时存储实例的 ips 并在稍后部署期间使用它们。但是当我启动多个具有相同名称的实例时,我被卡住了。
    • 使用 Jinja2 模板并将其保存为答案中的列表。
    • 非常感谢@techraf,我将ips作为列表存储到文件中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 2016-09-14
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    相关资源
    最近更新 更多