代码之家  ›  专栏  ›  技术社区  ›  benice

Ansible Openstack(导入)包含字典

  •  0
  • benice  · 技术社区  · 6 年前

    我得到了一个文件,该文件创建了X个具有不同操作系统和风格的虚拟机。

    ---
    - 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正在创建VM和VAR。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 }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   techraf    6 年前

    你应该 使用错误缩进的 import_tasks 模块包含变量文件。任务就是任务,变量就是变量(“vars”)。

    阅读 Ansible docs: "Variable File Separation" 在随机输入内容之前。

    正确的方法之一是:

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

    with_items 是任务的指令,因此它应该保持在原来的位置。

    您需要为包含列表的变量命名,并在 带\u项 指令:

    ---
    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 }}"