Ansible tips and tricks


Ansible is a server management tool that gets installed on a single Linux hosts, then uses playbooks to communicate out via SSH to servers and run commands.

Installation:

This will install Ansible on a CentOS host, then use a custom hosts file to ping all the hosts using a custom login key:

<bash>
> yum install ansible
> ansible --version
> ansible all -i ~/.ansible/hosts -m ping --user=centos --private-key ~/tests.pem

Hosts:

Place in /etc/ansible/hosts or in a custom file:

[name]
ip1
ip2
ip3

Running a playbook:

This example will run a playbook with a custom login key and custom host file:

<bash>
> ansible-playbook -v -i ~/.ansible/hosts --private-key ~/tests.pem ~/git/scripts/centos-initial.playbook --limit prod
> play centos-initial.playbook --limit prod

Run on a single node: -i ip.

Provisioning a full load balanced, auto-scaling environment:

  • Create certificate for elb.dendory.net, *.test.dendory.net using Certficiate Manager in AWS console.
  • Create a load balancer through the AWS console, assign the correct subnets, add 80 and 443 listener.
  • Create a DNS entry, set it to ALIAS to the load balancer's DNS name.

Example run, using both ansible and aws.py available from my scripts folder:

<bash>
> ~/git/scripts/aws.py create-vm test-% small-centos

* Creating instance...
Instance ID: i-782c5960
Private IP: 10.0.0.2

> ~/git/scripts/aws.py create-vm test-% small-centos
* Creating instance...
Instance ID: i-b92c59a1
Private IP: 10.0.0.3

> ~/git/scripts/aws.py dump-inventory ~/.ansible/hosts test

> play ~/git/scripts/centos-initial.playbook

> play ~/git/scripts/centos-website.playbook

> play ~/git/scripts/centos-healthstone.playbook

> ~/git/scripts/aws.py list-load-balancers
test-lb (test-lb-572946456.us-west-2.elb.amazonaws.com): No instance attached

> ~/git/scripts/aws.py attach-balanced-vm test-lb i-782c5960

> ~/git/scripts/aws.py attach-balanced-vm test-lb i-b92c59a1

> ~/git/scripts/aws.py list-load-balancers
test-lb (test-lb-572946456.us-west-2.elb.amazonaws.com): [{'InstanceId': 'i-782c5960'}, {'InstanceId': 'i-b92c59a1'}]

Inventory:

Here is a sample environment for the playbooks:

 [test:vars]
env=test
dashboard=https://mydomain.com/healthstone
template=linux
rootpwd=Test1234
replipwd=GDFgdgwerG436346
syspwd=htreERer75376734
dbname=tests
master=172.30.1.21
ansible_user=centos

[prod]
1.2.3.4

Windows:

Install winrm module:

<bash>
> pip install pywinrm

Add to inventory:

<bash>
ansible_user=Administrator
ansible_password=12345
ansible_port=5985
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

Enable wnrm on the host:

<powershell>
$admin = [adsi]("WinNT://./administrator, user")
$admin.PSBase.Invoke("SetPassword", "12345")
Invoke-Expression ((New-Object System.Net.Webclient).DownloadString('https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1'))

Test connection:

<bash>
> ansible all -i ~/.ansible/hosts -m win_ping


© 2008-2017 Patrick Lambert - All resources on this site are provided under the MIT License - You can contact me at: dendory@live.ca