Hi Vladimir,
thanks for pointing out that mistake.
I fixed the role, but I still have the same behaviour.
You can find below the full example with the output.
PLAYBOOK
---
- name: "provision EC2"
hosts: localhost
connection: local
vars_files:
- vars/vars_infrastracture.yml
- vars/vars-ec2-ami-linux.yml
- vars/vars-ec2-name.yml
gather_facts: false
roles:
- ec2
tasks:
- meta: refresh_inventory
- name: "config service inside EC2"
hosts: launched
tasks:
- name: niente
debug:
var: tag_service
EC2 ROLE
./roles/ec2/tasks/main.yml
---
- name: "Provision EC2"
ec2_instance:
name: "{{ ec2_name }}"
security_groups:
- "{{ security_group }}"
instance_type: "{{ instance_type }}"
image_id: "{{ image_id }}"
wait: yes
region: "{{ region }}"
key_name: "{{ ec2_keypair }}"
vpc_subnet_id: "{{ subnet_id }}"
volumes:
- device_name: /dev/xvda
ebs:
volume_size: "{{ disk_size }}"
delete_on_termination: true
register: ec2_result
- name: Add new instance to host group
add_host:
hostname: "{{ item.network_interfaces[0].private_ip_address }}"
groupname: launched
with_items: "{{ ec2_result.instances }}"
OUTPUT
$ ansible-playbook test-playbook.yml
[WARNING]: Invalid characters were found in group names and automatically
replaced, use -vvvv to see details
PLAY [provision EC2]
***************************************************************************************************************************************************************
TASK [ec2 : Provision EC2]
*********************************************************************************************************************************************************
changed: [localhost]
TASK [ec2 : Add new instance to host group]
****************************************************************************************************************************************
changed: [localhost] => (item={u'root_device_type': u'ebs',
u'private_dns_name': u'ip-10-69-122-181.eu-west-1.compute.internal',
u'cpu_options': {u'threads_per_core': 1, u'core_count': 1},
u'source_dest_check': True, u'monitoring': {u'state': u'disabled'},
u'subnet_id': u'subnet-065072b7239148454', u'ebs_optimized': False,
u'state': {u'code': 16, u'name': u'running'}, u'security_groups':
[{u'group_id': u'sg-0fc0a74cc52d80915', u'group_name':
u'common-webapp-secgroup'}], u'client_token':
u'c3cd3468213d4701b137c7529ba2296a', u'virtualization_type': u'hvm',
u'root_device_name': u'/dev/xvda', u'tags': {u'Name':
u'demo_ec2_deletemeplease'}, u'key_name': u'lsa-common-key', u'image_id':
u'ami-04d5cc9b88f9d1d39', u'ena_support': True, u'hibernation_options':
{u'configured': False}, u'capacity_reservation_specification':
{u'capacity_reservation_preference': u'open'}, u'public_dns_name': u'',
u'block_device_mappings': [{u'ebs': {u'status': u'attached',
u'delete_on_termination': True, u'attach_time':
u'2020-05-06T07:41:42+00:00', u'volume_id': u'vol-078f8b905e3818cb7'},
u'device_name': u'/dev/xvda'}], u'metadata_options': {u'http_endpoint':
u'enabled', u'state': u'applied', u'http_tokens': u'optional',
u'http_put_response_hop_limit': 1}, u'placement': {u'availability_zone':
u'eu-west-1b', u'tenancy': u'default', u'group_name': u''},
u'ami_launch_index': 0, u'hypervisor': u'xen', u'network_interfaces':
[{u'status': u'in-use', u'description': u'', u'subnet_id':
u'subnet-065072b7239148454', u'interface_type': u'interface',
u'ipv6_addresses': [], u'network_interface_id': u'eni-0168283f7fda6cca7',
u'private_dns_name': u'ip-10-69-122-181.eu-west-1.compute.internal',
u'attachment': {u'status': u'attached', u'device_index': 0,
u'attachment_id': u'eni-attach-054ae489dc970a477',
u'delete_on_termination': True, u'attach_time':
u'2020-05-06T07:41:41+00:00'}, u'private_ip_addresses':
[{u'private_ip_address': u'10.69.122.181', u'private_dns_name':
u'ip-10-69-122-181.eu-west-1.compute.internal', u'primary': True}],
u'mac_address': u'06:19:c8:8b:88:d2', u'private_ip_address':
u'10.69.122.181', u'vpc_id': u'vpc-07dd9cf9b9deca621', u'groups':
[{u'group_id': u'sg-0fc0a74cc52d80915', u'group_name':
u'common-webapp-secgroup'}], u'source_dest_check': True, u'owner_id':
u'933762258141'}], u'launch_time': u'2020-05-06T07:41:41+00:00',
u'instance_id': u'i-07464f7a1b6593fbd', u'instance_type': u't2.micro',
u'architecture': u'x86_64', u'state_transition_reason': u'',
u'private_ip_address': u'10.69.122.181', u'vpc_id':
u'vpc-07dd9cf9b9deca621', u'product_codes': []})
[WARNING]: Could not match supplied host pattern, ignoring: launched
PLAY [config service inside EC2]
***************************************************************************************************************************************************
*skipping: no hosts matched*
PLAY RECAP
*************************************************************************************************************************************************************************
localhost : ok=2 changed=2 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0
Nicola
On Tuesday, May 5, 2020 at 8:28:51 PM UTC+2, Vladimir Botka wrote:
>
> On Tue, 5 May 2020 09:48:44 -0700 (PDT)
> Nicola Limongi <[email protected] <javascript:>> wrote:
>
> > THIS WORKS (from the playbook)
> > ---
> > - name: "provision Ec2"
> > hosts: localhost
> > connection: local
> > gather_facts: false
> > tasks:
> > - ec2_instance:
> > name: "{{ ec2_name }}"
> > security_groups:
> > [ecc ecc....]
> > register: ec2_result
> >
> > - name: Add new instance to launched group
> > hosts: localhost
> > tasks:
> > - add_host:
> > hostname: "{{ item.network_interfaces[0].private_ip_address }}"
> > groupname: launched
> > with_items: "{{ ec2_result.instances }}"
> >
> > - name: "config service inside EC2"
> > hosts: launched
> > tasks:
> > [perform config of inside the EC2 ...]
> >
> > THIS DOES NOT WORK (from inside a role)
>
> The code is one playbook with three plays. It's not possible to put a
> playbook "inside a role".
>
> In particular, it's not possible to apply "hosts" in a "Role"
>
> https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html#role
>
>
> HTH,
>
> -vlado
>
--
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 view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/c59789b6-0ab0-4a81-b6a8-b3a50d2d4ae4%40googlegroups.com.