Ansible Dynamic Inventory fails to get the latest ec2 information
amazon-ec2, amazon-web-services, ansible, ansible-inventory
Solution
After researching I came to know that the dynamic inventory doesnt refresh between playbook calls, it will only refresh if you are executing the playbook seprately. However I was able to resolve the issue by using `add_host` command.
- name: Add Server to inventory
local_action: add_host hostname={{ item.public_ip }} groupname=webserver
with_items: webserver.instances
Problem
I am using `ec2.py` dynamic inventory for provisioning with ansible. I have placed the `ec2.py` in `/etc/ansible/hosts` file and marked it executable. I also have the `ec2.ini` file in `/etc/ansible/hosts`. ``` [ec2] regions = us-west-2 regions_exclude = us-gov-west-1,cn-north-1 destination_variable = public_dns_name vpc_destination_variable = ip_address route53 = False all_instances = True all_rds_instances = False cache_path = ~/.ansible/tmp cache_max_age = 0 nested_groups = False group_by_instance_id = True group_by_region = True group_by_availability_zone = True group_by_ami_id = True group_by_instance_type = True group_by_key_pair = True group_by_vpc_id = True group_by_security_group = True group_by_tag_keys = True group_by_tag_none = True group_by_route53_names = True group_by_rds_engine = True group_by_rds_parameter_group = True ``` Above is my `ec2.ini` file ``` --- - hosts: localhost connection: local gather_facts: yes vars_files: - ../group_vars/dev_vpc - ../group_vars/dev_sg - ../hosts_vars/ec2_info vars: instance_type: t2.micro tasks: - name: Provisioning EC2 instance local_action: module: ec2 region: "{{ region }}" key_name: "{{ key }}" instance_type: "{{ instance_type }}" image: "{{ ami_id }}" wait: yes group_id: ["{{ sg_npm }}", "{{sg_ssh}}"] vpc_subnet_id: "{{ PublicSubnet }}" source_dest_check: false instance_tags: '{"Name": "EC2", "Environment": "Development"}' register: ec2 - name: associate new EIP for the instance local_action: module: ec2_eip region: "{{ region }}" instance_id: "{{ item.id }}" with_items: ec2.instances - name: Waiting for NPM Server to come-up local_action: module: wait_for host: "{{ ec2 }}" state: started delay: 5 timeout: 200 - include: ec2-configure.yml ``` Now the configuring script is as follows ``` - name: Configure EC2 server hosts: tag_Name_EC2 user: ec2-user sudo: True gather_facts: True tasks: - name: Install nodejs related packages yum: name={{ item }} enablerepo=epel state=present with_items: - nodejs - npm ``` However when the configure script is called, the second script results into no hosts found. If I execute the `ec2-configure.yml` just alone and if the EC2 server is up & running then it is able to find it and configure it. I added the `wait_for` to make sure that the instance is in running state before the `ec2-configure.yml` is called. Would appreciate if anyone can point my error. Thanks