Ansible 基于变量的易变库存

Ansible 基于变量的易变库存,ansible,Ansible,playbook能否从变量加载库存列表?所以我可以根据选择的环境轻松定制跑步 tasks: - name: include environment config variables include_vars: file: "{{ item }}" with_items: - "../../environments/default.yml" - "../../environments/{{ env_name }}.yml" - name

playbook能否从变量加载库存列表?所以我可以根据选择的环境轻松定制跑步

  tasks:
  - name: include environment config variables
    include_vars:
      file: "{{ item }}"
    with_items:
      - "../../environments/default.yml"
      - "../../environments/{{ env_name }}.yml"

- name: set inventory
  set_fact:
     inventory.docker_host = " {{ env_docker_host }}"

对。使用
添加主机
模块:

因为我在ansible 2.3中,我不能使用
添加主机
模块(参见Jack的答案和文档),这将是一个更好的解决方案。因此,我将使用不同的技巧来扩充现有的ansible清单文件,重新加载并使用它

hosts.inv main.yml 预任务处理环境yml以及所有变量替换等,并在通过
refresh\u inventory
重新加载之前,使用这些变量填充
hosts.inv

-hosts:remotehosts
下定义的任何任务都将在远程主机上执行

[remotehosts]
- hosts: localhost
  pre_tasks:
    - name: include environment config variables
      include_vars:
        file: "{{ item }}"
      with_items:
        - "../environments/default.yml"
        - "../environments/{{ env_name }}.yml"
    - name: inventory facts
      run_once: true
      set_fact:
        my_host: "{{ env_host_name }}"

    - name: update inventory for env
      local_action: lineinfile
        path=hosts.inv
        regexp={{ my_host }}
        insertafter="[remotehosts]" line={{ my_host }}

    - meta: refresh_inventory

- hosts: remotehosts
...