Warning: Cannot modify header information - headers already sent by (output started at /data/web/virtuals/85063/virtual/www/domains/waldauf.org/lib/plugins/color/syntax.php:1) in /data/web/virtuals/85063/virtual/www/domains/waldauf.org/inc/actions.php on line 180
technology:ansible - WiKi
Table of Contents


Quick guide

Source: http://docs.ansible.com/ansible/intro_inventory.html#default-groups

Configuration file ~/.ansible.cfg:

inventory = ~/__TMP__/__KB__/Ansible/hosts


Hosts and Groups

In hosts file are define group of servers which are used in Ansible:



db.waldauf.org:2222                                 # You can specify remote port - 2222
jumper ansible_port=5555 ansible_host=    # Suppose you have just static IPs and want to set up some aliases that live in your host file, or you are connecting through tunnels

[targets]                                           # You can also select the connection type and user on a per host basis
other1.example.com     ansible_connection=ssh        ansible_user=user01
other2.example.com     ansible_connection=ssh        ansible_user=user02
  • In the above example, trying to ansible against the host alias “jumper” (which may not even be a real hostname) will contact on port 5555. Note that this is using a feature of the inventory file to define some special variables.

My first commands:

  • ping servers:
    $ ansible all -m ping                                                                                                                                                                                        
    localhost | SUCCESS => {
        "changed": false, 
        "ping": "pong"
  • echo:
    $ ansible local -a "echo HELLO HELL"                                                                                                                                                                         
    localhost | SUCCESS | rc=0 >>
  • sudo:
    # as bruce
    ansible all -m ping -u bruce
    # as bruce, sudoing to root
    ansible all -m ping -u bruce --sudo
    # as bruce, sudoing to batman
    ansible all -m ping -u bruce --sudo --sudo-user batman

Groups of Groups, and Group Variables

It is also possible to make groups of groups using the :children suffix. Just like above, you can apply variables using :vars.






Splitting Out Host and Group Specific Data

The preferred practice in Ansible is actually not to store variables in the main inventory file.

In addition to storing variables directly in the INI file, host and group variables can be stored in individual files relative to the inventory file.

Assuming the inventory file path is: /etc/ansible/hosts.

If the host is named foosball, and in groups raleigh and webservers, variables in YAML files at the following locations will be made available to the host:

/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'

For instance, suppose you have hosts grouped by datacenter, and each datacenter uses some different servers. The data in the groupfile /etc/ansible/group_vars/raleigh for the raleigh group might look like:

ntp_server: acme.example.org
database_server: storage.example.org

It is ok if these files do not exist, as this is an optional feature.

As an advanced use-case, you can create directories named after your groups or hosts, and Ansible will read all the files in these directories. An example with the raleigh group:


All hosts that are in the raleigh group will have the variables defined in these files available to them. This can be very useful to keep your variables organized when a single file starts to be too big, or when you want to use Ansible Vault on a part of a group’s variables.

Tip: In Ansible 1.2 or later the group_vars/ and host_vars/ directories can exist in the playbook directory OR the inventory directory. If both paths exist, variables in the playbook directory will override variables set in the inventory directory.