Ansible/EC2/Static Inventory
From charlesreid1
This page covers how to manage a static inventory file (hosts file) by hand.
Contents
Static vs dynamic inventory
Ansible/EC2/Static Inventory - static inventory requires the hosts file (containing the list of machines that Ansible is managing) be kept up to date by hand. This can be a burden if details are changing often or if resources are allocated automatically.
Ansible/EC2/Dynamic Inventory - dynamic inventory uses the AWS API to get information about machines that AWS is managing.
Managing a static inventory file using EC2
Edit playbooks/hosts
and include details about each AWS host. It is convenient to use the same SSH key for all machines.
If you are managing the hosts file by hand, you may want to create groups: Ansible/Groups
Specifying the hostname and port
The hosts file can contain domain names directly, grouped by bracketed group names - for example, if you already have DNS records for your subdomains set up to point to your EC2 nodes, you can do something like this:
[webservers] california.example.com newyork.example.com florida.example.com:222
If no port listed, Ansible assumes a default SSH port of 22. For non-standard ports (like florida.example.com) use a colon and the port where the SSH service is listening.
If you have a public IP address for the machine but no domain, you can use an alias for the machine:
[webservers] wolf ansible_port=222 ansible_host=8.9.10.11 ansible_user=ubuntu eagle ansible_port=22 ansible_host=8.9.10.12 ansible_user=ubuntu
The ansible_user
parameter can be used to set the user that Ansible logs in as, by default. On AWS Ubuntu images, the ubuntu user is already set up and has passwordless sudo access, and is a wise choice.
Specifying the SSH key
When you set up your nodes with AWS, you are required to set up an SSH key pair, for which you are given the private key by Amazon and the public key is stored on the remote server, allowing you to SSH into the server.
ansible_private_key_file
must be used to specify the location of this Amazon-provided private key, either on a machine-by-machine basis, or by setting a group variable.
Example:
[webservers] wolf ansible_port=222 ansible_host=8.9.10.11 ansible_user=ubuntu ansible_private_key_file=/home/charles/auth/aws/my-awesome-ssh-key.pem eagle ansible_port=22 ansible_host=8.9.10.12 ansible_user=ubuntu ansible_private_key_file=/home/charles/auth/aws/my-awesome-ssh-key.pem
Easier: modify ansible.cfg
to define default values for the private key:
private_key_file=/home/charles/auth/aws/my-awesome-ssh-key.pem
Flags