Install Ansible on Arch Linux and Run Playbooks Locally

Installation is pretty simple….

$ sudo pacman -Sy ansible sshpass

Useful dependencies:

Optional dependencies for ansible
    sshpass: for ssh connections with password
    python2-passlib: crypt values for vars_prompt
    python2-netaddr: for the ipaddr filter
    python2-systemd: log to journal
    python2-pywinrm: connect to Windows machines
    python2-dnspython: for dig lookup
    python2-ovirt-engine-sdk: ovirt support
    python2-boto: aws_s3 module
    python2-jmespath: json_query support

We need sshpass for password-based authentication using Ansible.

Run Ansible Locally

Create an inventory hosts file:

$ sudo vim /etc/ansible/hosts

My one currently has just localhost as the control machine:

[control]
127.0.0.1

You’ll also need to make sure the SSH daemon process is running:

$ sudo systemctl start sshd
$ sudo systemctl enable sshd

Also, because by default SSH uses Host key checking which is not supported by sshpass, we first need to SSH locally so we add our fingerprint to our known_hosts file.

[andy@home-pc ~]$ ssh andy@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:cynRa21eyQcfrYWjn4ajYiT1E7HBVn7mjfnDDsveiUE.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
andy@127.0.0.1's password: 
[andy@home-pc ~]$ exit
Connection to 127.0.0.1 closed.
[andy@home-pc ~]$
[andy@home-pc ~]$ cat .ssh/known_hosts 
127.0.0.1 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPu9ZWP54Dd5gkCllxGqlYyiwO+ZfHTmCLEJW0K9NR7ThLMxu3yTiQgzjWijN80sEs14U4GHNoc8Qv4DujaVmzU=

Now we can test it works by using the ansible ping module:

[andy@home-pc ~]$ ansible control -m ping -u andy --ask-pass
SSH password: 
127.0.0.1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

Now finally you can run ansible commands locally on your machine.

[andy@home-pc ~]$ ansible control -u andy --ask-pass -a "free -h"
SSH password: 
127.0.0.1 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           15Gi       732Mi        14Gi        48Mi       638Mi        14Gi
Swap:          34Gi          0B        34Gi

Installing Ansible on Ubuntu 14.04 LTS

Take a look at the official installation guide. The simplest way to install Ansible on Ubuntu is add the PPA repository and install via apt-get.

If not already installed, you will need the software-properties-common package.

sudo apt-get install software-properties-common

Then add the repository and install ansible.

sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible

Presumably you’ve already got an external server that you want to configure with Ansible. You will need SSH access, and if you’ve not already done so, you’re gonna want to setup key-based authentication. Assuming you’ve done that, you can test things are working with:

su -
mv -v /etc/ansible/hosts{,.original}
echo 134.213.48.162 > /etc/ansible/hosts
exit

I also need to tell ansible to connect as the root user.

sudo mkdir /etc/ansible/group_vars
sudo vim /etc/ansible/group_vars/all

Enter the following. The three dashes at the top indicate this is a yaml file.

---
ansible_ssh_user: root

You should now be able to test with the following.

ansible -m ping all

You should see output similar to the below if all went well.

andy@bastion:~$ ansible -m ping all
134.213.48.162 | success >> {
"changed": false,
"ping": "pong"
}