Ansible で ASG をセットアップしましたが、インベントリに ec2.py を使用できません

Ansible で ASG をセットアップしましたが、インベントリに ec2.py を使用できません

このコードを使用して、ローカル構成と Amazon Auto-scaling グループを作成しました。

--- 
- 
  local_action: 
    image_id: ami-61f9324
    instance_type: t2.micro
    module: ec2_lc
    name: nice-lc
    region: us-west-2
    security_groups: launch-wizard-1
  name: "Create lc"
- 
  local_action: 
    desired_capacity: 2
    health_check_period: 5
    health_check_type: ELB
    launch_config_name: nice-lc
    load_balancers: nice-lb1
    max_size: 10
    min_size: 2
    module: ec2_asg
    name: nice-asg
    region: us-west-2
  name: "Create asg"

ec2.py在庫管理にはファイルを使用していますec2.ini

これで、ローカル構成と自動スケーリング グループが作成され、定義した希望の容量に応じて 2 つのインスタンスが起動されます。

ここで、これら 2 つのインスタンスでタスクを実行したいと思います。

そのために私はこの yaml ファイルを使用しています。

---

- name: Example of provisioning servers
  hosts: 127.0.0.1
  connection: local
  tasks:
    - name: Add EP2 instances to host group
      local_action: add_host hostname={{ ec2_publicIp }} groupname=launch-wizard-1
      with_items: ec2.instances

    - name: Add tag to instances
      local_action: ec2_tag resource={{ item.id }} region=us-west-2 state=present
      with_items: ec2.instances
      args:
        tags:
          Name: nice-ec2

    - name: Wait for SSH to be available
      pause: minutes=1

- name: Configure provisioned servers
  hosts: tag_aws_autoscaling_groupName_nice_asg
  user: ubuntu
  sudo: True
  gather_facts: True
  tasks:
    - name: restart nginx
      service: name=nginx state=restarted

このファイルを実行すると、エラーが発生します:

1 つ以上の未定義の変数: 'ec2_publicIp' は未定義です

質問:

  1. nginx の再起動や、Amazon Auto Scaling グループによって起動されたインスタンスへのタグの追加などのタスクを実行したいと考えています。

  2. 私のやり方は正しいでしょうか? asg によって起動されたインスタンスで何らかのタスクを実行するだけであれば、ホストに追加する必要はないようです。

答え1

「EP2インスタンスをホストグループに追加」タスクの上に、

- debug: var=ec2

変数に何が含まれているかを確認します。各インスタンスの変数を確認すると、ec2_publicIP のスペルが間違っているか、変数内の別の場所にあることがわかります。

答え2

asg によって起動されたインスタンスでコマンドを実行したいだけです。

---
# I have removed that code and just using this. 
# This works with instance launched by asg.
- name: Configure provisioned servers
  hosts: tag_aws_autoscaling_groupName_nice_asg
  user: ubuntu
  sudo: True
  gather_facts: True
  tasks:
    - name: restart nginx
      service: name=nginx state=restarted

関連情報