LoginSignup
1
4

More than 5 years have passed since last update.

はじめに

先にAnsibleのインストールとディレクトリ作成のとAnsibleでVPCの作成の投稿を見てもらったほうがよいかと
今回は、AnsibleでCloudFormationを実行します。

・・・ってうまくいってません。期待した方ごめんなさい。
解決してないですが、投稿させてもらいますよ。

プレイブックの作成

以下のファイルを作ってください。
CloudFormationの実行には、cloudformationモジュールを使います。

./roles/aws/vars/main.yml
---

- name: "create stack"
  include: create_cfl.yml
./roles/aws/tasks/create_cfl.yml
---

- name: "create LAMP Stack"
  cloudformation:
    stack_name: "ansible-cloudformation"
    state: "present"
    region: "northeast-1"
    disable_rollback: true
    template_format: "json"
    template_url: "https://s3-ap-northeast-1.amazonaws.com/cloudformation-templates-ap-northeast-1/LAMP_Single_Instance.template"
    template_parameters:
      KeyName: "TestKey"
      DBName: "TESTDB"
      DBUser: "root"
      DBPassword: "Password"
      DBRootPassword: "Password"
      InstanceType: "t2.small"
      SSHLocation: "0.0.0.0/0"
    tags:
      Stack: "ansible-cloudformation"
  register: stack_regst

- debug: var=stack_regst
  when: stack_regst | success


template_urlオプションで、CloudFormationのテンプレートファイルを指定します。
指定しているのは、AWSで公開しているサンプルのLAMPテンプレートです。
ファイルの場合は、以下のようにtemplateオプションででroleから場所を指定してください。
あと、template_parametersオプションで、テンプレートファイルで指定するパラメータをいれます。

./roles/aws/tasks/create_cfl.yml
---
- name: "create LAMP Stack"
  cloudformation:
    stack_name: "ansible-cloudformation"
    state: "present"
    region: "northeast-1"
    disable_rollback: true
    template_format: "json"
    template: "roles/cloudformation/files/cloudformation-example.json"
    template_parameters:
      KeyName: "TestKey"
      DBName: "TESTDB"
      DBUser: "root"
      DBPassword: "Password"
      DBRootPassword: "Password"
      InstanceType: "t2.small"
      SSHLocation: "0.0.0.0/0"
    tags:
      Stack: "ansible-cloudformation"
  register: stack_regst

- debug: var=stack_regst
  when: stack_regst | success

プレイブックの実行

プレイブックの実行は前回と同じコマンドでできます。

$ ansible-playbook lamp.yml
PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [aws : create stack] ******************************************************
included: /Users/xxxx/ansible/roles/aws/tasks/create_cfl.yml for localhost

TASK [aws : create LAMP Stack] *************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "'NoneType' object has no attribute 'create_stack'"}
    to retry, use: --limit @lamp.retry

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1

create_stackのパラメータがなくてエラーだそうです。。。
色々試した結果、template_parametersオプションで指定しているパラメータが足りないのかな???
んーーテンプレートファイルにあるパラメータは全部指定したつもりですが。

まとめ

結果、うまくいってません。
次は、テンプレートファイルを変えて実行してないので、それを試してみようかと。
うまくいったらまたこの記事更新します!

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4