This post illustrates how you use a DNS forwarder to manage wildcard subdomains so that you don’t have to explicitly list each subdomain in /etc/host file.
PROBLEM
When trying to map multiple subdomains (ex: a.localhost, b.localhost, c.localhost, d.localhost) to the same IP, it is not possible to do the following in /etc/hosts:
# /etc/hosts
1.2.3.4 *.localhost
Rather, each subdomain has to be explicitly defined:
To run Ansible playbook in multiple hosts via SSH.
SOLUTION
Configuring SSH environment
Ensure SSH keypair exists on the current machine (ex: ~/.ssh/id_rsa for private key and ~/.ssh/id_rsa.pub for public key). If you do not have one, create one:
ssh-keygen
Copy the public key (ex: ~/.ssh/id_rsa.pub) to each remote host’s ~/.ssh/authorized_keys. If this file doesn’t exist, create it.
Ensure the current machine’s .ssh/ directory and file have correct permission.
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
In the current machine’s /etc/hosts, add all remote hosts.
In each remote host, enable the remote login and grant yourself the access to this service.
Enabling Remote Login on Mac
Test SSH connection to remote host to ensure they work first before working on Ansible playbook.
ssh user@donkeykong
ssh user@supermario
Creating Ansible Playbook
Create ansible.cfg and define the location of inventory file.
[defaults]
inventory = inventory.yml
Create inventory.yml and define both localhost and remote hosts.
all:
hosts:
localhost:
ansible_connection: local
donkeykong:
ansible_user: user
ansible_ssh_private_key_file: ~/.ssh/id_rsa
supermario:
ansible_user: user
ansible_ssh_private_key_file: ~/.ssh/id_rsa
Run a test to ensure the connection to remote hosts are successful.
ansible all -i inventory.yml -m ping
If successful, the output looks something like this: