Your ec2_instance_tag 'name:webserver' is being evaluated as a string
because there's no space, it needs to be a YAML dictionary.

If you read the docs on the module (ansible-doc ec2), they state:

- instance_tags
>         *a hash/dictionary of tags* to add to the new instance or for
> starting/stopping instance by tag; '{"key":"value"}' and
>         '{"key":"value","key":"value"}'
>         [Default: None]
>         version_added: 1.0
>

And if you pay attention to:
http://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html

* A dictionary is represented in a simple key: value form (the colon must
> be followed by a space): *
>

You have:

    ec2_instance_tag:
     name:webserver

This should be:

    ec2_instance_tag:
     name: webserver
          ^




On 23 April 2018 at 05:09, Saikrishna <[email protected]> wrote:

> Hi Will,
>
> Updated changes but see an error, could you please let me know what is
> missing? Inserted code for your reference.
>
> fatal: [localhost]: FAILED! => {"changed": false, "msg": "argument
> instance_tags is of type <type 'str'> and we were unable to convert to
> dict: dictionary requested, could not parse JSON or key=value"}
>
>
>
> ---
>
> - name: Create a sandbox instance
>
>   hosts: localhost
>
>   connection: local
>
>   gather_facts: True
>
>   vars:
>
>     aws_access_key: aabbccdd
>
>     aws_secret_key: aabbccddeeffgghhiijjkk
>
>     key_name: mykeypair
>
>     ec2_instance_count: 5
>
>     ec2_instance_tag:
>
>      name:webserver
>
>     instance_type: 't2.micro'
>
> #    instance_type: 'c4.2xlarge'
>
>     security_group: my_securitygroup
>
>     group: default
>
>     image: ami-00000000
>
> #    region: us-west-2a
>
>     ec2_url: https://ec2.us-west-2.amazonaws.com
>
>   tasks:
>
>     - name: Launch instance
>
>       ec2:
>
>          aws_access_key: "{{ aws_access_key }}"
>
>          aws_secret_key: "{{ aws_secret_key }}"
>
>          count_tag: "{{ ec2_instance_tag }}"
>
>          ec2_url: "{{ ec2_url }}"
>
>          exact_count: "{{ ec2_instance_count }}"
>
>          group: "{{ security_group }}"
>
>          key_name: "{{ key_name }}"
>
>          image: "{{ image }}"
>
>          instance_type: "{{ instance_type }}"
>
>          instance_tags: "{{ ec2_instance_tag }}"
>
>          wait: true
>
> #         region: "{{ region }}"
>
>          vpc_subnet_id: subnet-buiuuyyt
>
> #         vpc_id: vpc-iuuytgfxds
>
>          assign_public_ip: yes
>
>       register: ec2
>
>
>     - name: Add new instance to host group
>
>       add_host:
>
>         hostname: "{{ item.public_ip }}"
>
>         groupname: launched
>
>       with_items: "{{ ec2.instances }}"
>
>
>     - name: Wait for SSH to come up
>
>       wait_for:
>
>         host: "{{ item.public_dns_name }}"
>
>         port: 22
>
>         delay: 60
>
>         timeout: 320
>
>         state: started
>
>       with_items: "{{ ec2.instances }}"
>
>
>
> On Sun, Apr 22, 2018 at 9:26 AM, Will McDonald <[email protected]>
> wrote:
>
>> I think this is because you've defined the desired count in your vars but
>> not actually in the invocation of the ec2 module.
>>
>> Something like this should work as you expect:
>>
>> - name: create webserver instances
>>   hosts: localhost
>>   connection: local
>>   gather_facts: false
>>
>>   vars:
>>     ec2_instance_count: 2
>>     ec2_instance_tag:
>>       name: webserver
>>
>>   tasks:
>>     - name: provision ec2 webserver instances
>>       ec2:
>>         aws_access_key: "{{ aws_access_key }}"
>>         aws_secret_key: "{{ aws_secret_key }}"
>>         count_tag: "{{ ec2_instance_tag }}"
>>         ec2_url: "{{ ec2_url }}"
>>         exact_count: "{{ ec2_instance_count }}"
>>         groups: "{{ ec2_security_group }}"
>>         key_name: "{{ ec2_key_name }}"
>>         image: "{{ ec2_image }}"
>>         instance_tags: "{{ ec2_instance_tag }}"
>>         instance_type: "{{ ec2_instance_type }}"
>>         region: "{{ ec2_region }}"
>>         wait: true
>>       register: ec2
>>
>> Cheers,
>>
>> Will.
>>
>>
>>
>> On 22 April 2018 at 16:12, Love <[email protected]> wrote:
>>
>>> Dear Experts,
>>>
>>>
>>>
>>> How to launch multiple EC2 instances using ansible?
>>>
>>> I want to create an environment in AWS using below playbook but I see
>>> only one instance is being created when I execute below playbook.
>>>
>>>
>>> Could you please help with missing code/syntax here?
>>>
>>>
>>> Note: I'm using free account with AWS.
>>>
>>>
>>>
>>>
>>> ---
>>>
>>> - name: Create sandbox instances
>>>
>>>   hosts: localhost
>>>
>>>   gather_facts: True
>>>
>>>   vars:
>>>
>>>     aws_access_key: aabbccddeeffgghh
>>>
>>>     aws_secret_key: aabbccddeeffgghhiijjkkllmmnnoopp
>>>
>>>     key_name: mykeypair
>>>
>>>     count: 5
>>>
>>>     exact_count: 'count_tag'
>>>
>>>     instance_type: 't2.micro'
>>>
>>>     security_group: my_securitygroup
>>>
>>>     group: default
>>>
>>>     image: ami-00125160
>>>
>>>     ec2_url: https://ec2.us-west-2.amazonaws.com
>>>
>>>   tasks:
>>>
>>>     - name: Launch instance
>>>
>>>       ec2:
>>>
>>>          aws_access_key: "{{ aws_access_key }}"
>>>
>>>          aws_secret_key: "{{ aws_secret_key }}"
>>>
>>>          key_name: "{{ key_name }}"
>>>
>>>          group: "{{ security_group }}"
>>>
>>>          instance_type: "{{ instance_type }}"
>>>
>>>          count_tag: "{{ exact_count }}"
>>>
>>>          image: "{{ image }}"
>>>
>>>          wait: true
>>>
>>>          ec2_url: "{{ ec2_url }}"
>>>
>>>          vpc_subnet_id: subnet-edcf00db
>>>
>>>          assign_public_ip: yes
>>>
>>>       register: ec2
>>>
>>>
>>>     - name: Add new instance to host group
>>>
>>>       add_host:
>>>
>>>         hostname: "{{ item.public_ip }}"
>>>
>>>         groupname: launched
>>>
>>>       with_items: "{{ ec2.instances }}"
>>>
>>>
>>>     - name: Wait for SSH to come up
>>>
>>>       wait_for:
>>>
>>>         host: "{{ item.public_dns_name }}"
>>>
>>>         port: 22
>>>
>>>         delay: 60
>>>
>>>    timeout: 320
>>>
>>>         state: started
>>>
>>>       with_items: "{{ ec2.instances }}"
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Ansible Project" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>> To view this discussion on the web visit https://groups.google.com/d/ms
>>> gid/ansible-project/51b431bb-e0dd-4765-9051-cf5dbc8d624c%40g
>>> ooglegroups.com
>>> <https://groups.google.com/d/msgid/ansible-project/51b431bb-e0dd-4765-9051-cf5dbc8d624c%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Ansible Project" group.
>> To unsubscribe from this topic, visit https://groups.google.com/d/to
>> pic/ansible-project/4twB7g9pdNk/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected].
>> To post to this group, send email to [email protected].
>> To view this discussion on the web visit https://groups.google.com/d/ms
>> gid/ansible-project/CAKtKohSbX8%2B-1b0HmpUFkW-BksE4teRnSyvBb
>> KCcoeu%3D4zUEzg%40mail.gmail.com
>> <https://groups.google.com/d/msgid/ansible-project/CAKtKohSbX8%2B-1b0HmpUFkW-BksE4teRnSyvBbKCcoeu%3D4zUEzg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/ansible-project/CALSWnawmPCBboRPvBSd4-6Ra%2Bkh76ajZonxd%3DQq%2Bs%3D%
> 3Da4dxEsg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CALSWnawmPCBboRPvBSd4-6Ra%2Bkh76ajZonxd%3DQq%2Bs%3D%3Da4dxEsg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAKtKohRsGesCsLGCphFM7%3Dq74LQPjzUzc8BqsObj%2BWg%3DsvUJCg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to