Search code examples
ansiblevagrantamazon-route53

How to get IP addresses by using Ansible?


I have two DigitalOcean Droplets with public and private IP addresses, created by Vagrant and Ansible playbook. I need to create was_route53 records for each address (two records per droplet) How I can get addresses into vars to use it in playbook?


Solution

  • I made a playbook this weekend to do something very similar. In my case, I create a droplet on digitalocean, save the IP address, and then add a DNS record to AWS.

    I hope this helps. Note that the playbook still has some things hardcoded, such as region and size because I have not polished it off yet.

    Also, this is the first time I have used ansible with DO and AWS, and it's the first time that I have used the "register" feature to save variables, so this is probably a long way from best practice.

    One thing that seems ugly is my hardcoding of my venv python interpreter. If you are happy to use your system python, then you don't need to worry about that. The problem is that when ansible sees connection: local, it uses the system python, which is a bit odd since the script is running in a venv on the same machine.

    You need to pip install boto3 and:

    ansible-galaxy collection install community.digitalocean
    ansible-galaxy collection install community.aws
    

    example playbook

    ---
    
    - hosts: all
      connection: local
      become_user: tim
      vars: #local connection defaults to using the system python
        ansible_python_interpreter: /home/tim/pycharm_projects/django_api_sync/ansible/venv/bin/python3
      vars_files:
        - includes/secret_variables.yml
    
      tasks:
        - name: create a DigitalOcean Droplet
          community.digitalocean.digital_ocean_droplet:
            state: present
            name: "{{droplet_name}}"
            oauth_token: "{{digital_ocean_token}}"
            size: "s-2vcpu-2gb"
            region: SGP1
            monitoring: yes
            unique_name: yes
            image: ubuntu-20-04-x64
            wait_timeout: 500
            ssh_keys: [ "{{digital_ocean_ssh_fingerprint}}"]
          register: my_droplet
    
        - name: Print IP address
          ansible.builtin.debug:
            msg: Droplet IP address is {{ my_droplet.data.ip_address }}
    
        - name: Add A record with route53
          community.aws.route53:
            aws_access_key: "{{aws_access_key}}"
            aws_secret_key: "{{aws_secret_key}}"
            state: present
            zone: growthpath.com.au
            record: "{{ ansible_host }}"
            type: A
            ttl: 7200
            value: "{{ my_droplet.data.ip_address }}"
            wait: yes
    

    Example inventory file:

    all:
      hosts:
        test-dear1.growthpath.com.au:
          droplet_name: test-dear1
          ansible_host: test-dear1.growthpath.com.au
         
    

    ansible-playbook -i inventory_aws_test.yml -v create_new_droplet.yml