いろいろ備忘録

雑記です。

ec2の鍵ペア生成→インスタンス生成playbook

なぜかcopyモジュールだけうまく動かない

どうしたもんか

 

- name: make a keypair

 hosts: localhost

 gather_facts: False

 become: yes

 tasks:

   - name: create mykey

     ec2_key: name={{keypair_name}} region=ap-northeast-1

     register: keypair

 

   - name: create target dir.

     file: path=/home/ec2-user/auth state=directory

 

   - name: write the key to a file

     copy:

       dest: "/home/ec2-user/auth/{{keypair_name}}.pem"

       content: "{{ keypair.key.private_key }}"

     when: keypair.changed

 

   - name: dump var

     debug: var=keypair

 

   - name: start the instances

     ec2:

       region: "ap-northeast-1"

       image: "ami-08847abae18baa040"

       instance_type: "t2.micro"

       key_name: "{{keypair_name}}"

       group: [launch-wizard-1]

       instance_tags: { Name: ansiblebook, type: web, env: production }