Illusionist Illusionist - 12 days ago 7
Python Question

Connect with aws cloudformation stack with ansible

I am learning ansible.
How do I connect with aws EC2 machine with ansible? I created this machine with Cloudformation . Can I do something like (the ip is stored in bash as an environment variable my_db )

- name: Test
hosts: "{{ lookup('env','my_db') }}"
user: ec2-user
vars_files:
- vars/test.yml
tasks:
- name: install tarball


(and do stuff here on machine)

Answer

You can try it with a sample script to see if it works.

To answer your question: yes, you can do it if you set the environment variable my_db to the instance IP. But the recommended way is to pass the IP or use EC2 dynamic inventory script and instance name so that you can use the same playbook for multiple hosts.

- name: Test 
  hosts: "{{ my_host }}"

You can pass the IP as command line argument

ansible-playbook --extra-vars "my_host=<my_db_ip>" my_playbook.yml

or pass hostname (tag:Name)

ansible-playbook -i /etc/ansible/ec2.py --extra-vars "my_host=tag_Name_HOST_NAME" my_playbook.yml