Ansible Openstack(导入)包括字典
Ansible Openstack (import) include dictionary
我得到一个文件,该文件正在创建 X 数量的具有不同 OS 和风格的 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 正在创建虚拟机,vars.yaml 应该提供参数。
我怎样才能做到这一点?我尝试导入它,但无法正常工作。
******************************main.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
******************************vars.yaml*************** *********************************
---
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 docs: "Variable File Separation"。
其中一个正确的方法是:
- 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 }}"
我得到一个文件,该文件正在创建 X 数量的具有不同 OS 和风格的 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 正在创建虚拟机,vars.yaml 应该提供参数。 我怎样才能做到这一点?我尝试导入它,但无法正常工作。
******************************main.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
******************************vars.yaml*************** *********************************
---
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 docs: "Variable File Separation"。
其中一个正确的方法是:
- 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 }}"