Re: [ansible-project] add_host scope from inside roles
Hi Brian, thanks a lot, that was indeed one part of the problem. I also had to remove the meta: refresh_inventory at the end of the first play. WORKING VERSION --- - 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 - name: "config service inside EC2" hosts: launched tasks: - name: niente debug: var: tag_service EC2 ROLE --- - 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: host: "{{ item.network_interfaces[0].private_ip_address }}" groups: launched with_items: "{{ ec2_result.instances }}" On Wednesday, May 6, 2020 at 4:20:52 PM UTC+2, Brian Coca wrote: > > this is just a variable named 'groupname' > groupname: launched > > Per the docs > https://docs.ansible.com/ansible/latest/modules/add_host_module.html, > if you actually want to add it to the 'launched' group: > > groups: launched > > > > -- > -- > Brian Coca > > -- 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 ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4358d237-4aa7-4eab-9901-4a8ab5552932%40googlegroups.com.
Re: [ansible-project] add_host scope from inside roles
this is just a variable named 'groupname' groupname: launched Per the docs https://docs.ansible.com/ansible/latest/modules/add_host_module.html, if you actually want to add it to the 'launched' group: groups: launched -- -- Brian Coca -- 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 ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7d%2B6Uq%3DmXjAkeESHwag80Yk8vgyT1iM5mEnKiv5tCWguw%40mail.gmail.com.
Re: [ansible-project] add_host scope from inside roles
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 - 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=2changed=2unreachable=0 failed=0skipped=0
Re: [ansible-project] add_host scope from inside roles
On Tue, 5 May 2020 09:48:44 -0700 (PDT) Nicola Limongi 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 ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20200505202833.116a4701%40gmail.com. pgp20KYuFKLDt.pgp Description: OpenPGP digital signature
[ansible-project] add_host scope from inside roles
Hi all, I'm a bit confused about the behavior of add_host when used from inside a role or directly in the playbook. Am I doing something wrong or am I just hitting some role's scope boundary? 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) Here I'm just encapsulating the EC2 provisioning logic in a role called 'ec2'. The second play ("config service inside EC2") is skipped because the host is not present in the in memory inventory. --- - name: "create ec2 common linux" hosts: localhost connection: local gather_facts: false roles: - ec2 tasks: - meta: refresh_inventory - name: "config service inside EC2" hosts: launched tasks: [perform config of inside the EC2 ...] ./roles/ec2/tasks/main.yml --- - 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 }}" -- 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 ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7866f762-0d10-4438-9ba1-0d1c12181b1f%40googlegroups.com.