Starting up with Ansible
Let’s play with Ansible a bit. We will install a web server in an automated way.
First things first - I have to configure my host. It’s a VM I’m running locally but I wanted an isolated sandbox to install stuff via Ansible. That’s why you won’t see me running things against localhost even so that’s easier. I prefer things clean.
$ cat /etc/ansible/hosts
[vm]
node1
$
$ grep node1 /etc/hosts
192.168.124.134 node1
$
Now let’s start simple by creating one yml file.
$ cat nginx.yml
---
- hosts: vm
tasks:
- name: install httpd
yum: name=httpd update_cache=yes state=latest
$
Here I did several things.
- I configured the recipe to run on the VM host I configured previously. I achieved that via the hosts entry.
- I defined a task to install httpd via yum.
Note that for this to work you should have yum installed. For example I’m running Fedora VM which normally uses dnf. I had to install yum first. Here are the dependencies I had to pre-install on the VM:
- sudo dnf groupinstall ‘Ansible node’
- sudo dnf install yum
That’s it! Now I’m good to go. Let’s see what happens.
$ ansible-playbook -vvvv -s nginx.yml
No config file found; using defaults
Loaded callback default of type stdout, v2.0
1 plays in nginx.yml
PLAY ***************************************************************************
TASK [setup] *******************************************************************
<node1> ESTABLISH SSH CONNECTION FOR USER: None
<node1> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/kalin/.ansible/cp/ansible-ssh-%h-%p-%r -tt node1 '/bin/sh -c '"'"'( umask 22 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1511531629.3-224558910699084 `" && echo "` echo $HOME/.ansible/tmp/ansible-tmp-1511531629.3-224558910699084 `" )'"'"''
<node1> PUT /tmp/tmpQUiOst TO /home/kalin/.ansible/tmp/ansible-tmp-1511531629.3-224558910699084/setup
<node1> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/kalin/.ansible/cp/ansible-ssh-%h-%p-%r '[node1]'
<node1> ESTABLISH SSH CONNECTION FOR USER: None
<node1> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/kalin/.ansible/cp/ansible-ssh-%h-%p-%r -tt node1 '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-ecqewgxgjdiicfdetllakdipvozofrfl; /bin/sh -c '"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/kalin/.ansible/tmp/ansible-tmp-1511531629.3-224558910699084/setup; rm -rf "/home/kalin/.ansible/tmp/ansible-tmp-1511531629.3-224558910699084/" > /dev/null 2>&1'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"''"'"'"'"'"'"'"'"''"'"''
ok: [node1]
TASK [install httpd] ***********************************************************
task path: /home/kalin/Documents/Projects/ansible_sandbox/nginx.yml:4
<node1> ESTABLISH SSH CONNECTION FOR USER: None
<node1> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/kalin/.ansible/cp/ansible-ssh-%h-%p-%r -tt node1 '/bin/sh -c '"'"'( umask 22 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1511531629.83-167115064302742 `" && echo "` echo $HOME/.ansible/tmp/ansible-tmp-1511531629.83-167115064302742 `" )'"'"''
<node1> PUT /tmp/tmpRGLraG TO /home/kalin/.ansible/tmp/ansible-tmp-1511531629.83-167115064302742/yum
<node1> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/kalin/.ansible/cp/ansible-ssh-%h-%p-%r '[node1]'
<node1> ESTABLISH SSH CONNECTION FOR USER: None
<node1> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/kalin/.ansible/cp/ansible-ssh-%h-%p-%r -tt node1 '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-gkynhmxpywporqedociysvghmwssqrcr; /bin/sh -c '"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python -tt /home/kalin/.ansible/tmp/ansible-tmp-1511531629.83-167115064302742/yum; rm -rf "/home/kalin/.ansible/tmp/ansible-tmp-1511531629.83-167115064302742/" > /dev/null 2>&1'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"''"'"'"'"'"'"'"'"''"'"''
changed: [node1] => {"changed": true, "invocation": {"module_args": {"conf_file": null, "disable_gpg_check": false, "disablerepo": null, "enablerepo": null, "exclude": null, "install_repoquery": true, "list": null, "name": ["httpd"], "state": "latest", "update_cache": true}, "module_name": "yum"}, "msg": "", "rc": 0, "results": ["Last metadata expiration check: 0:48:38 ago on Fri 24 Nov 2017 03:05:24 PM EET.\nDependencies resolved.\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.29-1.fc26 updates 1.3 M\nInstalling dependencies:\n apr x86_64 1.6.3-1.fc26 updates 118 k\n apr-util x86_64 1.5.4-6.fc26 updates 95 k\n fedora-logos-httpd noarch 26.0.1-1.fc26 fedora 33 k\n httpd-filesystem noarch 2.4.29-1.fc26 updates 28 k\n httpd-tools x86_64 2.4.29-1.fc26 updates 88 k\n mod_http2 x86_64 1.10.12-1.fc26 updates 148 k\n\nTransaction Summary\n================================================================================\nInstall 7 Packages\n\nTotal download size: 1.8 M\nInstalled size: 4.8 M\nDownloading Packages:\n(1/7): httpd-filesystem-2.4.29-1.fc26.noarch.rp 163 kB/s | 28 kB 00:00 \n(2/7): httpd-tools-2.4.29-1.fc26.x86_64.rpm 389 kB/s | 88 kB 00:00 \n(3/7): apr-1.6.3-1.fc26.x86_64.rpm 903 kB/s | 118 kB 00:00 \n(4/7): apr-util-1.5.4-6.fc26.x86_64.rpm 1.1 MB/s | 95 kB 00:00 \n(5/7): fedora-logos-httpd-26.0.1-1.fc26.noarch. 391 kB/s | 33 kB 00:00 \n(6/7): mod_http2-1.10.12-1.fc26.x86_64.rpm 1.0 MB/s | 148 kB 00:00 \n(7/7): httpd-2.4.29-1.fc26.x86_64.rpm 2.7 MB/s | 1.3 MB 00:00 \n--------------------------------------------------------------------------------\nTotal 826 kB/s | 1.8 MB 00:02 \nRunning transaction check\nTransaction check succeeded.\nRunning transaction test\nTransaction test succeeded.\nRunning transaction\n Preparing : 1/1 \n Installing : apr-1.6.3-1.fc26.x86_64 1/7 \n Running scriptlet: apr-1.6.3-1.fc26.x86_64 1/7 \n Installing : apr-util-1.5.4-6.fc26.x86_64 2/7 \n Running scriptlet: apr-util-1.5.4-6.fc26.x86_64 2/7 \n Installing : httpd-tools-2.4.29-1.fc26.x86_64 3/7 \n Installing : fedora-logos-httpd-26.0.1-1.fc26.noarch 4/7 \n Running scriptlet: httpd-filesystem-2.4.29-1.fc26.noarch 5/7 \n Installing : httpd-filesystem-2.4.29-1.fc26.noarch 5/7 \n Installing : mod_http2-1.10.12-1.fc26.x86_64 6/7 \n Installing : httpd-2.4.29-1.fc26.x86_64 7/7 \n Running scriptlet: httpd-2.4.29-1.fc26.x86_64 7/7 \n Verifying : httpd-2.4.29-1.fc26.x86_64 1/7 \n Verifying : httpd-filesystem-2.4.29-1.fc26.noarch 2/7 \n Verifying : httpd-tools-2.4.29-1.fc26.x86_64 3/7 \n Verifying : apr-1.6.3-1.fc26.x86_64 4/7 \n Verifying : apr-util-1.5.4-6.fc26.x86_64 5/7 \n Verifying : mod_http2-1.10.12-1.fc26.x86_64 6/7 \n Verifying : fedora-logos-httpd-26.0.1-1.fc26.noarch 7/7 \n\nInstalled:\n httpd.x86_64 2.4.29-1.fc26 apr.x86_64 1.6.3-1.fc26 \n apr-util.x86_64 1.5.4-6.fc26 fedora-logos-httpd.noarch 26.0.1-1.fc26\n httpd-filesystem.noarch 2.4.29-1.fc26 httpd-tools.x86_64 2.4.29-1.fc26 \n mod_http2.x86_64 1.10.12-1.fc26 \n\nComplete!\n"]}
PLAY RECAP *********************************************************************
node1 : ok=2 changed=1 unreachable=0 failed=0
$
In this example I added -vvvv so I have full verbosity while I’m still debugging my recipe. From this point I can easily add more stuff to my automation and get more things running.
That’s the quickest way to start using Ansible that I know of :) I hope you enjoyed it.