Ansible Openstack (import) include dictionary



我得到一个文件,该文件正在创建具有不同操作系统和风格的X数量的VM。

---
- hosts: localhost
  vars:
    http_port: 80
    max_clients: 200
  tasks:
  - name: create VM
    os_server:
      name: "{{ item.name }}"
      state: present
      image: "{{ item.image }}"
      boot_from_volume: True
      security_groups: ssh
      flavor: "{{ item.flavor }}"
      key_name: mykey
      region_name: "{{ lookup('env', 'OS_REGION_NAME') }}"
      nics:
        - net-name: private
      wait: yes
    register: instances
    with_items:
      - { name: Debian Jessie, image: Debian Jessie 8 , flavor: c1.small, loginame: debian }
      - { name: Debian Stretch, image: Debian Stretch 9 , flavor: c1.small, loginame: debian }
      - { name: ubuntu Xenial, image: Ubuntu Xenial 16.04 , flavor: c1.small, loginame: ubuntu }
      - { name: ubuntu Trusty, image: Ubuntu Trusty 14.04 , flavor: c1.small, loginame: ubuntu }
      - { name: Fedora, image: Fedora 25 , flavor: c1.small, loginame: fedora }
      - { name: CentOS, image: CentOS 7 , flavor: c1.small, loginame: centos }
      - { name: Rstudio, image: RStudio Appliance  , flavor: c1.small, loginame: ubuntu }
      - { name: Spark Zepellin, image: Spark Zeppelin , flavor: m1.medium, loginame: ubuntu }

现在我想把它分成两个文件:main.yaml 和 vars.yaml。Main.yaml正在创建VM,vars.yaml应该提供参数。我该怎么做?我试图导入它,但我无法让它工作。

****

**

---
- hosts: localhost
  vars:
    http_port: 80
    max_clients: 200
  tasks:
  - import_vars: vars.yaml
  - name: create VM
    os_server:
       name: "{{ item.name }}"
      state: present
      image: "{{ item.image }}"
      boot_from_volume: True
      security_groups: ssh
      flavor: "{{ item.flavor }}"
      key_name: mykey
      region_name: "{{ lookup('env', 'OS_REGION_NAME') }}"
      nics:
        - net-name: private
      wait: yes
    register: instances
****

**

---
  with_items:
 - { name: Debian Jessie, image: Debian Jessie 8 , flavor: c1.small, loginame: debian }
 - { name: Debian Stretch, image: Debian Stretch 9 , flavor: c1.small, loginame: debian }
 - { name: ubuntu Xenial, image: Ubuntu Xenial 16.04 , flavor: c1.small, loginame: ubuntu }
 - { name: ubuntu Trusty, image: Ubuntu Trusty 14.04 , flavor: c1.small, loginame: ubuntu }
 - { name: Fedora, image: Fedora 25 , flavor: c1.small, loginame: fedora }
 - { name: CentOS, image: CentOS 7 , flavor: c1.small, loginame: centos }
 - { name: Rstudio, image: RStudio Appliance  , flavor: c1.small, loginame: ubuntu }
 - { name: Spark Zepellin, image: Spark Zeppelin , flavor: m1.medium, loginame: ubuntu }

您不应该使用错误缩进import_tasks模块来包含变量文件。任务就是任务,变量就是变量("vars"(。

在随机输入内容之前,请阅读 Ansible 文档:"可变文件分离"。

正确的方法之一是:

- hosts: localhost
  vars:
    http_port: 80
    max_clients: 200
  vars_files:
    - vars.yaml
  tasks:
    # ...

with_items是任务的指令,所以它应该留在原地。

您需要为包含列表的变量命名,并在 with_items 指令中引用它:

---
instance_definitions:
  - { name: Debian Jessie, image: Debian Jessie 8 , flavor: c1.small, loginame: debian }
  - { name: Debian Stretch, image: Debian Stretch 9 , flavor: c1.small, loginame: debian }
  # ...

和:

- name: create VM
  os_server:
    name: "{{ item.name }}"
    # ...
  register: instances
  with_items: "{{ instance_definitions }}"

最新更新