title: store multiple ec2 intance details in host file Ansible?
body: <p>i had launched 2 instances using the following code</p>
  <pre><code>    - name: Launch the new EC2 Instance
          ec2:
            key_name : &quot;{{keypair}}&quot;
  
            group: &quot;{{ security_group }}&quot;
            instance_type: &quot;{{ instance_type }}&quot;
            image: &quot;{{ image }}&quot;
            wait: true
            region: &quot;{{ region }}&quot;
  
            count: &quot;{{count}}&quot;
            volumes:
             - device_name: /dev/sda1
               volume_type: gp2
               volume_size: 8
            instance_tags:
              Name : awslab
  
            # assign_public_ip : yes
          register: ec2
  
  </code></pre>
  <p>i want to save details in the form of</p>
  <pre><code>[webservers]
  ipofinstance1
  [monitoring-servers]
  ipofinstance2
  </code></pre>
  <p>how to store the above format to hosts file</p>
  <p>with same instance for both groups i have used the following code and it worked now i want to add different ip in different groups.</p>
  <p>i tried this</p>
  <pre><code>- name: Add the newly created EC2 instance(s) to the  host file
          blockinfile:
            path: hosts
  
            
  
            create: yes
            block: |
              [webservers]
              {{item.public_ip}}
              [monitoring-servers]
              {{item.public_ip}}
  
  
  
          with_items: '{{ec2.instances}}'
          become: yes
  
  </code></pre>
  <p>Any suggestions for making the output in the above specified format
  Thanks:)</p>