Thursday, December 24, 2015

ansible up

update python to 2.7.11 on ubuntu


apt_repository  need python >2.7.9
ansible_galaxy  has bug on ansible1.9.4 on proxy

$sudo -E apt-add-repository ppa:fkrull/deadsnakes-python2.7
$sudo -E apt-get install --only-upgrade python2.7
$python --version
Python 2.7.11




Install

$ sudo -E apt-get install software-properties-common
$ sudo  -E apt-add-repository ppa:ansible/ansible
$ sudo -E apt-get update
$ sudo -E apt-get install --only-upgrade ansible

Install latest ansible

  $git clone git://github.com/ansible/ansible.git --recursive
  $virtualenv -p /home/whg/python2.7.11/bin/python python2.7.11
$. ./python2.7.11/bin/activate
$sudo pip install PyYAML Jinja2 httplib2 six
Setting up Ansible to run out of checkout(~/.bashrc)
export PATH=/home/ubuntu/ansible/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export export PYTHONPATH=/home/ubuntu/ansible/lib:
sudo PYTHONPATH=/home/ubuntu/ansible/lib ansible
or /etc/sudoers
Defaults        env_reset
Defaults        env_keep += "PYTHONPATH" 

ansible-galaxy(ansible > 2 otherwise of proxy bug)

use role installed from galaxy

$ansible-galaxy install angstwad.docker_ubuntu
#create a play book to use the role
---
- hosts: all
  sudo: yes
roles:
angstwad.docker_ubuntu
#test to run the book
$ansible-playbook t1.yml

Building Role Scaffolding

$ansible-galaxy init rolename



Best Practices:

  • Create a dedicated ansible node on the local net in the cloud to play book
  • copy directory to remote node using scp dir remote:~/dir or using synchronize module instead of copy

facts about any system 

$ansible <hostname>-m setup | less
#facter comes as part of extra modules. to use the facter module, the "facter" and #"ruby-json" packages preinstalled on the target host.
$ansible <hostname> -m facter | less


variables

The following are the places from where Ansible accepts variables:
  • The default directory inside a role
  • Inventory variables
    • The host_vars and group_vars parameters defined in separate directories
    • The host/group vars parameter defined in an inventory file
  • Variables in playbooks and role parameters
  • The vars directory inside a role and variables defined inside a play
  • Extra variables provided with the -e option at runtime



Patterns

Patterns in Ansible are how we decide which hosts to manage.The following patterns address one or more groups. Groups separated by a colon indicate an “OR” configuration. This means the host may be in either one group or the other:

hosts: name1:name2:group1:group2

Disable SSH Host Key Checking For All Hosts

set these options permanently in ~/.ssh/config (for the current user) or in /etc/ssh/ssh_config (for all users), either for all hosts or for a given set of IP addresses
Host *
   StrictHostKeyChecking no
   UserKnownHostsFile=/dev/null


Ansible looks for an ansible.cfg file in the following places, in this order:
  1. File specified by the ANSIBLE_CONFIG environment variable
  2. ./ansible.cfg (ansible.cfg in the current directory)
  3. ~/.ansible.cfg (.ansible.cfg in your home directory)
  4. /etc/ansible/ansible.cfg




Ansible will then move to the next task in the list, and go through these same four steps. It’s important to note that:
  • Ansible runs each task in parallel across all hosts.
  • Ansible waits until all hosts have completed a task before moving to the next task.
  • Ansible runs the tasks in the order that you specify them.
Ansible supports the ssh-agent program, so you don’t need to explicitly specify SSH key files in your inventory files. See “SSH Agent” for more details if you haven’t used ssh-agent before.



YAML to get started with your first playbook:
  • The first line of a playbook should begin with "--- " (three hyphens) which indicates the beginning of the YAML document.
  • Lists in YAML are represented with a hyphen followed by a white space. A playbook contains a list of plays; they are represented with "- ". Each play is an associative array, a dictionary, or a map in terms of key-value pairs.
  • Indentations are important. All members of a list should be at the same indentation level.
  • Each play can contain key-value pairs separated by ":" to denote hosts, variables, roles, tasks, and so on.

role dependencies

pecify role dependency inside the meta subdirectory

Safely limiting Ansible playbooks to a single machine

There's also a cute little trick that lets you specify a single host on the command line (or multiple hosts, I guess), without an intermediary inventory:
ansible-playbook -i "imac1-local," user.yml
Note the comma (,) at the end; this signals that it's a list, not a file

No comments:

Post a Comment