Ansible

From XennisWiki
Jump to: navigation, search

Ansible is a free software platform for configuring and managing computers. It combines multi-node software deployment, ad hoc task execution, and configuration management. It manages nodes over SSH or PowerShell and requires Python to be installed on them. Modules work over JSON and standard output and can be written in any programming language. The system uses YAML to express reusable descriptions of systems. (Wikipedia)

Installation

Further information: Ansible Documentation - Installation

Using pip
pip install ansible

Arch Linux

pacman -S ansible

Debian/Ubuntu

apt-get install software-properties-common
apt-add-repository ppa:ansible/ansible
apt-get update
apt-get install ansible

Modules

Packages

Install Linux packages (apt)

Further information: apt module

name: Install java(7-jdk)
apt: pkg={{item}} state=installed
with_items:
  - openjdk-7-jdk
  - openjdk-7-jre-lib

Install Perl packages (cpanm)

Further information: cpanm module

name: Install perl dependencies
cpanm: name={{item}}
with_items:
  - Class::Struct
  - File::Basename

Install Python packages (pip)

Further information: pip module

name: Install Python dependencies
pip: name={{item}}
with_items:
  - subprocess32
  - requests

Files

Download files (get_url)

Further information: get_url module

name: Download example.pdf
get_url: url=https://example.org/example.pdf
        dest={{SAMPLE_PATH}}

Unpack archives (unarchive)

Download and extract a archive

- name: Download and extract archive
  unarchive:
    src: https://example.org/example.zip
    dest: /tmp
    remote_src: True

Parametrize a file (template)

Specify a source and a destination

- name: Copy robots.txt file
  template:
    src: "local/path/robots.txt.j2"
    dest: "remote/path/robots.txt"

And in the file local/path/robots.txt.j2 just insert variables

User-agent: *
Disallow: /wiki/{{ mediawiki_sitename }}:*

Execute commands (command)

Further information: command module

name: Maven install
command: mvn clean install
         chdir={{SAMPLE_PATH}}

Clone repositories (git)

Further information: git module

name: Clone example repository
git: repo=https://github.com/user/example.git
     dest={{SAMPLE_PATH}}

Tips

Install modules from Ansible Galaxy

ansible-galaxy install username.role_name

Usage with Vagrant

see Vagrant

Simulate run and show changes

Use --check --diff (or -C -D).

Configure global hosts

Global hosts are configures in /etc/ansible/hosts

[example]
example.org

Configure the Python interpreter

example.org ansible_python_interpreter=/usr/bin/python2

Output a variable (debug module)

- debug:
    var: example_var.files

See also

External links