Amazon ec2 使用ansible在不同的AZ中创建ec2实例
我已经创建了一个剧本来在不同的AZ中部署ec2实例。当我为计数区域和子网添加“with_items”时失败。如何克服这个问题 我遵循了这一点,但它没有工作,所以创建了一个新的oone,因为我无法更新它 剧本在这里Amazon ec2 使用ansible在不同的AZ中创建ec2实例,amazon-ec2,ansible,Amazon Ec2,Ansible,我已经创建了一个剧本来在不同的AZ中部署ec2实例。当我为计数区域和子网添加“with_items”时失败。如何克服这个问题 我遵循了这一点,但它没有工作,所以创建了一个新的oone,因为我无法更新它 剧本在这里 - name: Deploy EC2 Master node hosts: localhost gather_facts: False vars_files: - ~/aws-common/automation/ansible/config/var_input.ym
- name: Deploy EC2 Master node
hosts: localhost
gather_facts: False
vars_files:
- ~/aws-common/automation/ansible/config/var_input.yml
tasks:
- name: Get AWS credentials
sts_assume_role:
role_arn: "{{ role_arn }}"
role_session_name: "{{ role_session_name }}"
register: assumed_role
- name: Provision a set of instances
ec2:
key_name: "{{ key_name }}"
group: "{{ group }}"
instance_type: "{{ instance_type }}"
region: "{{ region }}"
image: "{{ image }}"
wait: "{{ wait }}"
wait_timeout: "{{ wait_timeout }}"
#count: "{{ count }}"
count: "{{ item.ec2_count }}"
instance_profile_name: "{{ instance_profile_name }}"
instance_tags:
Name: "{{ Name }}"
deployer: "{{ deployer }}"
resourceowner: "{{ resourceowner }}"
monitoring: "{{ monitoring }}"
ec2_zone: "{{ item.zone }}"
vpc_subnet_id: "{{ item.subnet }}"
assign_public_ip: "{{ assign_public_ip }}"
aws_access_key: "{{ assumed_role.sts_creds.access_key }}"
aws_secret_key: "{{ assumed_role.sts_creds.secret_key }}"
security_token: "{{ assumed_role.sts_creds.session_token }}"
volumes:
- device_name: /dev/sda1
volume_type: gp2
encrypted: true
volume_name: HadoopMaster-rootvolume /
volume_size: 100
delete_on_termination: true
- device_name: /dev/sdf
volume_type: gp2
encrypted: true
volume_name: HadoopMaster /edw/xxx
volume_size: 70
delete_on_termination: true
with_items: "{{ ec2_zone_subnet }}"
register: ec2
- name: Add new instance to host group
add_host:
hostname: "{{ item.public_ip }}"
groupname: launched
loop: "{{ ec2.instances }}"
- name: Wait for SSH to come up
delegate_to: "{{ item.public_dns_name }}"
wait_for_connection:
delay: 60
timeout: 320
loop: "{{ ec2.instances }}"
- hosts: launched
name: Mounting the attached EBS volumes
user: ec2-user
become: yes
gather_facts: no
tasks:
- name: Run a script with arguments (free form)
script: ~/files/format.sh
这是我在变量文件中的条目
ec2_zone_subnet: [{"zone":"us-east-1a", "subnet":"subnet-5f87a23b", "ec2_count":1},{"zone":"us-east-1b", "subnet":"subnet-a9d2bd86","ec2_count":1},{"zone":"us-east-1c", "subnet":"subnet-0cab05cf89718e652","ec2_count":0},{"zone":"us-east-1d", "subnet":"subnet-079c66d30dcd53ce7","ec2_count":0}]
我犯了一个错误
ok: [localhost] => (item={u'subnet': u'subnet-0cab05cf89718e652', u'ec2_count': 0, u'zone': u'us-east-1c'}) => {"changed": false, "instance_ids": [], "instances": [], "item": {"ec2_count": 0, "subnet": "subnet-0cab05cf89718e652", "zone": "us-east-1c"}, "tagged_instances": []}
ok: [localhost] => (item={u'subnet': u'subnet-079c66d30dcd53ce7', u'ec2_count': 0, u'zone': u'us-east-1d'}) => {"changed": false, "instance_ids": [], "instances": [], "item": {"ec2_count": 0, "subnet": "subnet-079c66d30dcd53ce7", "zone": "us-east-1d"}, "tagged_instances": []}
TASK [Add new instance to host group] **********************************************************************************************************************
task path: /home/desind/aws-common/automation/ansible/files/testnew.yaml:51
fatal: [localhost]: FAILED! => {"msg": "'dict object' has no attribute 'instances'"}
这些项目现在只有
ec2计数
子网
和主机
。我看不到其他属性。有人能帮我使用register:ec2和_项吗?这些是我对playbook所做的以下更改
我不知道为什么有人投了反对票,这个问题是明确而有效的。
- name: Add all instance private IPs to host group
add_host: hostname={{ item.instances[0].private_ip }} instance_id={{ item.instance_ids[0] }} groups=launched
with_items: '{{ ec2.results }}'
- name: Wait for SSH to come up
delegate_to: "{{ item.instances[0].private_ip }}"
wait_for_connection:
delay: 100
timeout: 320
loop: "{{ ec2.results }}"