我已经创build了本地configuration和Amazon自动缩放组。 通过使用这个代码。
--- - 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文件的库存。
所以现在创build本地configuration和自动缩放组,并根据我们定义的期望容量启动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
当我运行这个文件,我得到错误:
一个或多个未定义的variables:'ec2_publicIp'未定义
问题:
我想运行像重新启动nginx这样的任务,并在由亚马逊自动缩放组启动的实例上添加标签。
我做对了吗? 看来,我甚至不需要添加到主机,如果我只是想在asg发起的实例上运行一些任务。
在“将EP2实例添加到主机组”任务上方,使用
- debug: var=ec2
看看这个variables是什么 如果您查看每个实例的variables,您可能会发现您拼写了ec2_publicIP错误,或者它位于variables的不同位置。
因为我只是想在由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