Search code examples
ansiblerds

Finding ansible RDS instances


I am trying to configure with ansible my EC2 instances dynamically. I am having a problem working out how to find my RDS instances. I can set key tags but ansible ec2.py doesn't pick them up (https://github.com/ansible/ansible/issues/7564). Does any one have any suggestions?

So for instance I want an RDS instance for production, staging and for just for testing.


Solution

  • If you mean the ansible ec2.py inventory script doesn't pick up RDS instances then yes I believe you're right, it will only find EC2 instances.

    We have a similar setup with a seperate RDS instance for staging and production environments. The way we solved it was for any playbooks/roles that need to run against the mysql database, we run them against the magic host "localhost", and have the RDS endpoints set in variables. We use a separate variable file per environment and load them in at the beginning of the play.

    e.g.

    |--vars/
    |    |--staging.yml
    |    |--production.yml
    |    
    |--playbook.yml
    

    Example "production.yml" file:

    ---
    DB_SERVER: database-endpoint.cls4o6q35lol.eu-west-1.rds.amazonaws.com
    DB_PORT: 3306
    DB_USER: dbusername
    DB_PASSWORD: dbpassword
    

    Example playbook that creates a database

    - name: Playbook name
      hosts: localhost
      vars_files:
        - vars/{{ env }}.yml
      tasks:
    
        - mysql_db: login_host={{ DB_SERVER }}
                    login_user={{ DB_USER }}
                    login_password={{ DB_PASSWORD }}
                    login_port={{ DB_PORT }}
                    collation=utf8_general_ci
                    encoding=utf8
                    name=databasename
                    state=present
    

    Then you can just specifiy the envionrment variable when you run the playbook.

    ansible-playbook playbook.yml --extra-vars "env=production"