使用ansible使用ec2实例启动多个卷

时间:2016-09-22 13:20:19

标签: amazon-web-services amazon-ec2 ansible

我正在配置一个附加了多少卷的ec2实例。以下是我的剧本也是如此。

---
- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    instance_type: 't2.micro'
    region: 'my-region'
    aws_zone: 'myzone'
    security_group: my-sg
    image: ami-sample
    keypair: my-keypair
    vpc_subnet_id: my-subnet
  tasks:
  - name: Launch instance
    ec2:
      image: "{{ image }}"
      instance_type: "{{ instance_type }}"
      keypair: "{{ keypair}}"
      instance_tags: '{"Environment":"test","Name":"test-provisioning"}'
      region: "{{region}}"
      aws_zone: "{{ region }}{{ aws_zone }}"
      group: "{{ security_group }}"
      vpc_subnet_id: "{{vpc_subnet_id}}"
      wait: true
      volumes:
        - device_name: "{{ item }}"
          with_items:
             - /dev/sdb
             - /dev/sdc
          volume_type: gp2
          volume_size: 100
          delete_on_termination: true
          encrypted: true
    register: ec2_info

但是得到以下错误

  

致命:[localhost]:失败! => {"失败":是的," msg":"字段' args'具有无效值,似乎包含未定义的变量。错误是:' item'未定义

如果我将{{item}}替换为/dev/sdb,则可以轻松地使用特定卷启动实例。但我想用指定的项目列表创建多个卷 - / dev / sdb,/ dev / sdc等 有任何可能的方法来实现这一目标吗?

1 个答案:

答案 0 :(得分:3)

你不能将{ "name": "redux-simple-starter", "version": "1.0.0", "description": "Simple starter package for Redux with React and Babel support", "main": "index.js", "repository": "git@github.com:StephenGrider/ReduxSimpleStarter.git", "scripts": { "start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js" }, "author": "", "license": "ISC", "devDependencies": { "babel-core": "^6.2.1", "babel-loader": "^6.2.0", "babel-preset-es2015": "^6.1.18", "babel-preset-react": "^6.1.18", "chai": "^3.5.0", "chai-jquery": "^2.0.0", "css-loader": "^0.25.0", "jquery": "^2.2.1", "jsdom": "^8.1.0", "mocha": "^2.4.5", "node-sass": "^3.10.0", "react-addons-test-utils": "^0.14.7", "sass-loader": "^4.0.2", "style-loader": "^0.13.1", "webpack": "^1.13.2", "webpack-dev-server": "^1.14.0" }, "dependencies": { "axios": "^0.13.1", "babel-preset-stage-1": "^6.1.18", "lodash": "^3.10.1", "material-ui": "^0.15.4", "raw-loader": "^0.5.1", "react": "^15.3.2", "react-dom": "^15.3.2", "react-redux": "^4.4.5", "react-router": "^2.0.1", "redux": "^3.5.2", "redux-promise": "^0.5.0", "redux-simple-promise": "^2.0.2" } } 与变量和参数一起使用 - 它只适用于任务 您需要提前构建卷列表:

with_items

然后exec - name: Populate volumes list set_fact: vol: device_name: "{{ item }}" volume_type: gp2 volume_size: 100 delete_on_termination: true encrypted: true with_items: - /dev/sdb - /dev/sdc register: volumes 模块:

ec2

更新:另一种没有volumes: "{{ volumes.results | map(attribute='ansible_facts.vol') | list }}" 的方法:

定义变量 - 卷的模板字典(不含set_fact):

device_name

然后在vol_default: volume_type: gp2 volume_size: 100 delete_on_termination: true encrypted: true 模块中,您可以使用:

ec2