Difference between revisions of "Ansible"

From Christoph's Personal Wiki
Jump to: navigation, search
(External links)
(Miscellaneous)
 
(3 intermediate revisions by the same user not shown)
Line 102: Line 102:
 
         credentials: ~/.raxpub
 
         credentials: ~/.raxpub
 
         name: ansible-rax-test-1
 
         name: ansible-rax-test-1
         region: DFW # one of: http://www.rackspace.com/about/datacenters/
+
         region: DFW # one of: <nowiki>http://www.rackspace.com/about/datacenters/</nowiki>
 
         flavor: general1-1 # "1 GB General Purpose v1"
 
         flavor: general1-1 # "1 GB General Purpose v1"
 
         image: a743dd3b-e409-4833-be55-d85f6192817e # "Ubuntu 12.04 LTS (Precise Pangolin) (PVHVM)"
 
         image: a743dd3b-e409-4833-be55-d85f6192817e # "Ubuntu 12.04 LTS (Precise Pangolin) (PVHVM)"
Line 141: Line 141:
  
 
  $ ansible-playbook -vvvv -i hosts rax_server_delete.yml
 
  $ ansible-playbook -vvvv -i hosts rax_server_delete.yml
 +
 +
==Operating System (OS) families==
 +
 +
See [https://github.com/ansible/ansible/blob/37ae2435878b7dd76b812328878be620a93a30c9/lib/ansible/module_utils/facts.py#L267 here] for the most up-to-date list.
 +
<pre>
 +
# A list with OS Family members
 +
OS_FAMILY = dict(
 +
    RedHat = 'RedHat', Fedora = 'RedHat', CentOS = 'RedHat', Scientific = 'RedHat',
 +
    SLC = 'RedHat', Ascendos = 'RedHat', CloudLinux = 'RedHat', PSBM = 'RedHat',
 +
    OracleLinux = 'RedHat', OVS = 'RedHat', OEL = 'RedHat', Amazon = 'RedHat',
 +
    XenServer = 'RedHat', Ubuntu = 'Debian', Debian = 'Debian', Raspbian = 'Debian', Slackware = 'Slackware', SLES = 'Suse',
 +
    SLED = 'Suse', openSUSE = 'Suse', SuSE = 'Suse', SLES_SAP = 'Suse', Gentoo = 'Gentoo', Funtoo = 'Gentoo',
 +
    Archlinux = 'Archlinux', Manjaro = 'Archlinux', Mandriva = 'Mandrake', Mandrake = 'Mandrake',
 +
    Solaris = 'Solaris', Nexenta = 'Solaris', OmniOS = 'Solaris', OpenIndiana = 'Solaris',
 +
    SmartOS = 'Solaris', AIX = 'AIX', Alpine = 'Alpine', MacOSX = 'Darwin',
 +
    FreeBSD = 'FreeBSD', HPUX = 'HP-UX'
 +
)
 +
</pre>
  
 
==Miscellaneous==
 
==Miscellaneous==
 +
 +
* List plugins that can generate an inventory:
 +
<pre>
 +
$ ansible-doc -t inventory -l
 +
advanced_host_list  Parses a 'host list' with ranges                                                                                                                                                 
 +
auto                Loads and executes an inventory plugin specified in a YAML config                                                                                                                 
 +
aws_ec2            ec2 inventory source                                                                                                                                                             
 +
aws_rds            rds instance source                                                                                                                                                               
 +
azure_rm            Azure Resource Manager inventory plugin                                                                                                                                           
 +
constructed        Uses Jinja2 to construct vars and groups based on existing inventory.                                                                                                             
 +
foreman            foreman inventory source                                                                                                                                                         
 +
gcp_compute        Google Cloud Compute Engine inventory source                                                                                                                                     
 +
generator          Uses Jinja2 to construct hosts and groups from patterns                                                                                                                           
 +
host_list          Parses a 'host list' string                                                                                                                                                       
 +
ini                Uses an Ansible INI file as inventory source.                                                                                                                                     
 +
k8s                Kubernetes (K8s) inventory source                                                                                                                                                 
 +
nmap                Uses nmap to find hosts to target                                                                                                                                                 
 +
openshift          OpenShift inventory source                                                                                                                                                       
 +
openstack          OpenStack inventory source                                                                                                                                                       
 +
scaleway            Scaleway inventory source                                                                                                                                                         
 +
script              Executes an inventory script that returns JSON                                                                                                                                   
 +
tower              Ansible dynamic inventory plugin for Ansible Tower.                                                                                                                               
 +
virtualbox          virtualbox inventory source                                                                                                                                                       
 +
vmware_vm_inventory VMware Guest inventory source                                                                                                                                                     
 +
vultr              Vultr inventory source                                                                                                                                                           
 +
yaml                Uses a specific YAML file as an inventory source.
 +
</pre>
 +
 
* Disable <code>`[[cowsay]]`</code> (I _hate_ that this is not the [http://michaelheap.com/cowsay-and-ansible/ default setting]!):
 
* Disable <code>`[[cowsay]]`</code> (I _hate_ that this is not the [http://michaelheap.com/cowsay-and-ansible/ default setting]!):
 
  $ sed -i 's/#\?nocows.*/nocows = 1/' /etc/ansible/ansible.cfg
 
  $ sed -i 's/#\?nocows.*/nocows = 1/' /etc/ansible/ansible.cfg
Line 151: Line 197:
  
 
==See also==
 
==See also==
 +
* [[Terraform]]
 +
* [[Pulumi]]
 
* [[Chef]]
 
* [[Chef]]
 
* [[Puppet]]
 
* [[Puppet]]

Latest revision as of 17:04, 10 April 2022

Ansible is an open-source 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 and requires Python (2.4 or later) 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.

Ansible is a DevOps tool for configuring, deploying, monitoring, and automating servers (among other things). This article will only discuss the Linux aspects. Most of the examples will use Vagrant. However, some of the examples/demos will include Rackspace, DigitalOcean, and Amazon's AWS as well.

Examples

Note: This article assumes you already have Vagrant and Ansible installed.

In the following examples, lines starting with "$" indicate a command to be run on the Ansible controlling machine (my laptop/local machine in most cases) and "[remote] $" indicates a command to be run on one of the nodes.

  • Setup your local Ansible environment:
$ mkdir -p $HOME/dev/ansible
$ cd $HOME/dev/ansible
$ git init
  • Create your own custom Ansible configuration file:
$ cat << EOF > ansible.cfg
[defaults]
#inventory=inventory/ec2.py
#vault_password_file = ~/.vault_pass.txt
host_key_checking = False
private_key_file = $HOME/.ssh/id_rsa
roles_path = roles

[ssh_connection]
ssh_args = -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s -o PreferredAuthentications=publickey
#control_path = $HOME/.ansible/cp/ansible-ssh-%%h-%%p-%%r
EOF
  • Create your inventory file and add it to your git repo (we will call our development group of hosts "dev"):
$ cat << EOF > hosts
[dev]
# Development host using a Ubuntu 14.04 vagrant box
192.168.1.10	ansible_ssh_user=vagrant
EOF
$ git add hosts

Note: The default inventory file lives at /etc/ansible/hosts. However, since I will be storing this file in my git repo, I like to keep it with the rest of my Ansible files.

  • Ansible command syntax:
$ ansible <host-pattern> [-f forks] [-m module_name] [-a args]
  • Test that you can communicate with your vagrant VM (I like to sometimes add "-vvvv" for extra verbosity whilst debugging/developing):
$ ansible dev -i hosts -u vagrant -vvvv -m command -a "cat /etc/issue"
10.11.1.103 | success | rc=0 >>
Ubuntu 14.04.2 LTS \n \l
$ ansible dev -i hosts -u vagrant -vvvv -m command -a "uptime"

Note: Since we defined our vagrant user in the inventory file ("hosts"), we no longer need to specify this user in the following examples. Also note that my vagrant box already has my SSH public key copied over (if not, make sure to add "-k" so Ansible prompts you for the password).

$ ansible dev -i hosts -m ping
10.11.1.103 | success >> {
    "changed": false, 
    "ping": "pong"
}
  • Update your remote (vagrant VM, in this case) machine ("-s or "--sudo" to run operation with `sudo`):
$ ansible dev -i hosts -s -m command -a "apt-get update"
[remote] $ tailf /var/log/syslog
Jun 10 18:52:36 vagrant ansible-command: Invoked with executable=None shell=True args=apt-get update  removes=None creates=None chdir=None
  • Get a list of "facts" about the vagrant VM:

Note: Ansible by default gathers "facts" about the machines under management, and these facts can be accessed in Playbooks and in templates. To see a list of all of the facts that are available about a machine, you can run the "setup" module as an ad-hoc action:

$ ansible dev -i hosts -m setup

This will print out a dictionary of all of the facts that are available for that particular host.

Using Ansible with Rackspace Cloud Servers

Note: See Category:Rackspace for more examples on how to interact with Rackspace's products and services.

  • Create your Ansible development directory for Rackspace (this can, of course, be anywhere you like):
$ mkdir -p $HOME/dev/ansible/rax
$ cd $HOME/dev/ansible/rax
  • Create your hosts inventory file:
$ cat << EOF > hosts
[localhost]
localhost ansible_connection=local
EOF
  • Create your Rackspace API credentials file:
$ cat << EOF > ~/.raxpub
[rackspace_cloud]
username = <RAX_USERNAME>
api_key = <RAX_API_KEY>
EOF
  • Create your Ansible playbook for creating a Rackspace Cloud Server:
$ cat << EOF > rax_server_create.yml
---
- name: Build a Rackspace Cloud Server
  hosts: localhost
  gather_facts: False
  tasks:
    - name: Server build request
      local_action:
        module: rax
        credentials: ~/.raxpub
        name: ansible-rax-test-1
        region: DFW # one of: http://www.rackspace.com/about/datacenters/
        flavor: general1-1 # "1 GB General Purpose v1"
        image: a743dd3b-e409-4833-be55-d85f6192817e # "Ubuntu 12.04 LTS (Precise Pangolin) (PVHVM)"
        key_name: my_rackspace_key # SSH key
        wait: yes
        state: present # CREATE
        networks:
          - private
          - public
      register: rax
EOF
  • Run the above playbook to create your Rackspace Cloud Server:
$ ansible-playbook -vvvv -i hosts rax_server_create.yml
  • Delete the above server:
$ sed -i 's/state: present/state: absent/' rax_server_create.yml
$ ansible-playbook -vvvv -i hosts rax_server_create.yml

A better method might be to create a separate playbook to delete a list of given Cloud Servers:

$ echo << EOF > rax_server_delete.yml
---
- name: Delete a list of Rackspace Cloud Servers
  hosts: localhost
  gather_facts: False
  tasks:
    - name: Server delete request
      local_action:
        module: rax
        credentials: ~/.raxpub
        region: ORD
        instance_ids: 959dbcaf-6145-4280-8206-78deaa364e4d,6eee1da0-a516-4059-828a-c0539a960324
        wait: yes
        state: absent # DELETE
      register: rax
EOF
$ ansible-playbook -vvvv -i hosts rax_server_delete.yml

Operating System (OS) families

See here for the most up-to-date list.

# A list with OS Family members
OS_FAMILY = dict(
    RedHat = 'RedHat', Fedora = 'RedHat', CentOS = 'RedHat', Scientific = 'RedHat',
    SLC = 'RedHat', Ascendos = 'RedHat', CloudLinux = 'RedHat', PSBM = 'RedHat',
    OracleLinux = 'RedHat', OVS = 'RedHat', OEL = 'RedHat', Amazon = 'RedHat',
    XenServer = 'RedHat', Ubuntu = 'Debian', Debian = 'Debian', Raspbian = 'Debian', Slackware = 'Slackware', SLES = 'Suse',
    SLED = 'Suse', openSUSE = 'Suse', SuSE = 'Suse', SLES_SAP = 'Suse', Gentoo = 'Gentoo', Funtoo = 'Gentoo',
    Archlinux = 'Archlinux', Manjaro = 'Archlinux', Mandriva = 'Mandrake', Mandrake = 'Mandrake',
    Solaris = 'Solaris', Nexenta = 'Solaris', OmniOS = 'Solaris', OpenIndiana = 'Solaris',
    SmartOS = 'Solaris', AIX = 'AIX', Alpine = 'Alpine', MacOSX = 'Darwin',
    FreeBSD = 'FreeBSD', HPUX = 'HP-UX'
)

Miscellaneous

  • List plugins that can generate an inventory:
$ ansible-doc -t inventory -l
advanced_host_list  Parses a 'host list' with ranges                                                                                                                                                   
auto                Loads and executes an inventory plugin specified in a YAML config                                                                                                                  
aws_ec2             ec2 inventory source                                                                                                                                                               
aws_rds             rds instance source                                                                                                                                                                
azure_rm            Azure Resource Manager inventory plugin                                                                                                                                            
constructed         Uses Jinja2 to construct vars and groups based on existing inventory.                                                                                                              
foreman             foreman inventory source                                                                                                                                                           
gcp_compute         Google Cloud Compute Engine inventory source                                                                                                                                       
generator           Uses Jinja2 to construct hosts and groups from patterns                                                                                                                            
host_list           Parses a 'host list' string                                                                                                                                                        
ini                 Uses an Ansible INI file as inventory source.                                                                                                                                      
k8s                 Kubernetes (K8s) inventory source                                                                                                                                                  
nmap                Uses nmap to find hosts to target                                                                                                                                                  
openshift           OpenShift inventory source                                                                                                                                                         
openstack           OpenStack inventory source                                                                                                                                                         
scaleway            Scaleway inventory source                                                                                                                                                          
script              Executes an inventory script that returns JSON                                                                                                                                     
tower               Ansible dynamic inventory plugin for Ansible Tower.                                                                                                                                
virtualbox          virtualbox inventory source                                                                                                                                                        
vmware_vm_inventory VMware Guest inventory source                                                                                                                                                      
vultr               Vultr inventory source                                                                                                                                                             
yaml                Uses a specific YAML file as an inventory source.
$ sed -i 's/#\?nocows.*/nocows = 1/' /etc/ansible/ansible.cfg
#~OR~
$ ANSIBLE_NOCOWS=1 ansible-playbook -i hosts -s foo.yml
#~OR~ add the following to your .bashrc file:
export ANSIBLE_NOCOWS=1

See also

External links