0

i had launched 2 instances using the following code

    - name: Launch the new EC2 Instance
        ec2:
          key_name : "{{keypair}}"

          group: "{{ security_group }}"
          instance_type: "{{ instance_type }}"
          image: "{{ image }}"
          wait: true
          region: "{{ region }}"

          count: "{{count}}"
          volumes:
           - device_name: /dev/sda1
             volume_type: gp2
             volume_size: 8
          instance_tags:
            Name : awslab

          # assign_public_ip : yes
        register: ec2

i want to save details in the form of

[webservers]
ipofinstance1
[monitoring-servers]
ipofinstance2

how to store the above format to hosts file

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.

i tried this

- 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

Any suggestions for making the output in the above specified format Thanks:)

avsr
  • 143
  • 3
  • 15
  • 1
    Are you creating host file to use it further with other ansible playbooks? If that is the case for creating host file as "Inventory", I suggest use Ansible Dynamic Inventory using ec2.py script. – saurabh14292 Jun 25 '20 at 06:57
  • or the handy [`add_host:`](https://docs.ansible.com/ansible/2.9/modules/add_host_module.html) task, if you are able to avoid launching `ansible-playbook` again – mdaniel Jun 25 '20 at 15:46

0 Answers0