Showing posts with label ansible. Show all posts
Showing posts with label ansible. Show all posts

Monday, 7 November 2016

Ansible in a Virtualenv

Tested on Arch Linux. First install support for the Virtualenv

$ pip2 install virtualenv

Create the virtualenv to host ansible :

$ virtualenv --system-site-packages ansible_env

Activate the environment

$ source ansible_env/bin/activate

Install a specific version

$ pip2 install ansible==2.1.3.0

When complete deactivate

$ deactivate

Monday, 2 May 2016

Ansible Roles variables that differ between OS

Using Ansible roles you may wish to set the package and service name per OS. Within roles we can import them within tasks from the appropriate vars.

tasks/main.yml

- name: Obtain OS Specific Variables
  include_vars: "{{ item }}"
  with_first_found:
    - "../vars/{{ ansible_distribution }}-{{ ansible_distribution_version }}.yml"
    - "../vars/{{ ansible_distribution }}.yml"
    - "../vars/{{ ansible_os_family }}.yml"
    - "../vars/defaults.yml"
Then you can set values within /vars as appropriate e.g. Apache

vars/default.yml

apache_package_name: apache2
apache_service_name: apache2

/vars/RedHat.yml

apache_package_name: httpd
apache_service_name: httpd

/vars/Debian.yml

apache_package_name: apache2
apache_service_name: apache2

Monday, 29 February 2016

Vagrant Change default ssh forwarded port

Vagrant is a great tool for quickly setting up and destroying a development test environment. By default it will define a local port on your workstation to port forward ssh traffic to the Vagrant box. For a multi server set-up environment I prefer to be able to specify the exact port to use for ssh port forwarding.
For example to set the ssh port forwarding as 4001 in the Vagrantfile you can define :
db.vm.network "forwarded_port", guest: 22, host: 4001, id: 'ssh'
It does require the id: section to be set for ssh. I find this really useful for Ansible so I can define the exact ssh port and IP address within my Ansible File.

Sunday, 28 February 2016

Ansible Facts Caching With Redis

Ansible is an amazing tool for automation of tasks. Facts are details about the host that Ansible gathers when it connect to the machine. It will gather a significant amount of useful information that can be used in Playbooks e.g. OS version, IPv4/IPv6 addresses. To make Ansible go faster we can store the facts in a cache the first time in connects to the hosts. On subsequent Playbook executions Ansible will only look up the facts in the cache instead of gathering them again. There is a cache option fact_caching_timeout that can be modified to define how long the cache should be valid for in seconds.

Setup Redis for Ansible Caching

On Arch Linux install redis and enable it
  $ pacman -S redis
  $ systemctl enable redis
  $ systemctl start redis
Install the Python Redis packages
$ pacman -S python2-redis python-redis
Check redis is responding
$ redis-cli ping
PONG
Update /etc/ansible/ansible.cfg in a text editor
gathering = smart
fact_caching = redis
fact_caching_timeout = 86400
fact_caching_connection = localhost:6379:0
After running a Playbook redis-cli can be used to view the keystore e.g.
$ redis-cli 
127.0.0.1:6379> keys *
1) "ansible_cache_keys"
2) "ansible_factsDB1"