Setting up ubuntu linux for ansible development

Using ansible on ubuntu for the first time

The first task I was shown when following ansible tutorials was editing the hosts file. I ran into a few issues though, hence this post. The solutions I found that helped are documented below:

Install ansible

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

Backups the hosts file

sudo mv /etc/ansible/hosts /etc/ansible/hosts.backup

Edit the hosts  file.

sudo nano hosts

Add your environments and ip addresses, eg:

[local]
127.0.0.1

Get ssh working, and test it can connect to your listed servers

add-username-to-transactions
sudo apt-get install openssh-server

Then test it is working and started:

sudo service ssh start

Run the ansible ping command

ansible all -m ping

Errors

If you get these errors:

Error: Permission denied

Two solutions,

Run the following command with -k

ansible all -m ping -k

This will trigger the password request

OR

Force ask password through the ansible config file:

sudo nano /etc/ansible/ansible.cfg
uncomment:
ask_pass = True

This will also trigger the password request, but by default without needing the -k option.

Then insert your password. You should now get your pong result without the error.

Upgrading flask on a linux server, but getting version errors

Flask database throws errors when trying to upgrade and migrate

The following is a last resort solution for a problematic database, I have yet to find a cleaner solution for this problem.

Delete the existing version of alembic, and start from scratch

source ~/venvs/env.Vodacom-QuoteService/bin/activate

Clear the alembic table

sqlite3 [yourdbfile]
> delete * from alembic_version;

Remove the migrations folder

rm -r migrations/

Initialize the database

python manage.py db init

Upgrade and migrate the database

python manage.py db upgrade
python manage.py db migrate

Restart the service that is using the table.

Might need to rerun these three steps if the database is still not working

python manage.py db upgrade
python manage.py db migrate
(restart your service)

Add environment variables to your linux environment

Insert the below into the console

export VARIABLE_NAME = VARIABLE_VALUE

To remove the environment variable

unset VARIABLE_NAME

To view the environment variable:

env | grep -i VARIABLE_NAME

Note, for the env | grep -i command, the VARIABLE_NAME can also just be
part of the variable name, eg “proxy” and then any variables
containing that value will display. Eg http_proxy, and htts_proxy
will display if you insert env | grep -i “proxy”.

Command line for a newbie

Basics of command line explained. Functionality you should become familiar with.

What development can you do with the command line

The command line interface has become useful for scaffolding development projects in less time and with less effort.

With tools like git offering a simple but powerful version control interface it is becoming very important to add the command line to your toolbelt.

There are multiple online package libraries from which to download and install using the command line.

For php there is packagist.

For python there is pip.

For front end packages like angular or jquery and much more, there is npm.

The basics

So here’s a quick overview of the beginner stuff.

Change your location

When you open your command line tool, you will be in a particular folder on your computer. For windows you may be in C:\ and for ubuntu you may be in ~/

To change your location you can use command cd

cd [path]

Windows

Eg: cd C:/Projects/

Linux

Eg: cd ~/Projects

View files in current directory

As you move around the different folders on your computer you will want to see what other folders or files are within the folder you change to.

For Linux you will use the command ls:

ls

For Windows you will use the command dir:

dir

These will return a list of files and folders in the current folder.

You can combine this with a folder path in order to see a list of files and folders in a folder you are not currently in.

Linux:

ls [folder location]
Eg: ls /var/log

Windows:

dir /a [folder location]
Eg: dir /a C:/

Creating directories

Create a directory in the current location.

mkdir

If you want to create a directory in a different location you will need to use cd to go there.

Create a file

Ubuntu:

touch [filename]
eg touch index.html

Windows:

There are multiple ways to create a file in windows. You will just need to select the approach that works for you. Below are two of the various ways to create files on windows.

echo 2> [filename]

fc >> [filename]

Alternatively you can use notepad:

notepad [filename]

This will open up notpad which will then ask you if you would like to create a file named [filename]

Rename a file

ren

ren [current filename] [new filename]

Delete files

For Linux use rm

rem [filename]

For Windows use

DEL [filename]

Keep learning

This is only the tip of the iceberg. The command line is a very powerful interface and can save you a lot of time.

Get to know the hosts file

Find your hosts file

In windows your hosts file is located:

C:\WINDOWS\system32\drivers\etc\hosts

For windows you will need to open an editor as administrator to edit the file. Also you may need to disable your anti virus as it may prevent you from editing your hosts file. Just remember to enable the anti virus again as soon as you are done.

In linux your hosts file is located:

/etc/hosts

For linux you will need to use sudo to edit the hosts file.

Experiment with the hosts file

Add at the bottom of your hosts file:

127.0.0.1 moo
127.0.0.1 baa
127.0.0.1 moobaa

In your command line you can now ping these:

ping moo
ping baa
ping moobaa

Each one will show something similar to:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Then remove the lines we just added to the hosts file, and ping them again.

You will receive an error message similar to:

Ping request could not find host baa. Please check the name and try again.

Practice some more

Lets practice using an existing IP like google. First lets find google’s IP:

ping google.com

The result will return 216.58.223.36.

In your hosts file add this line at the bottom:

216.58.223.36 googlefun

Now ping googlefun:

ping googlefun

You will see that you have now pinged the google IP address.

What can you do with the hosts file?

You can point a domain name to an ip. You may want to do to block your computer from communicating with a certain domain name or just have custom domain names set up for fun.

Connect via ssh

Open ssh client then insert command:

ssh username@hostname

Trouble shooting ssh connections

If you’re struggling to connect to ssh, ping the domain name or ip address:

ping [domain/ip]

IP Addresses preset in the hosts file

In some cases you may set the hosts file for a domain name and IP address. If your ssh connects to an incorrect domain/ip, look at your .hosts file and check if you are pointing your ip and domain names correctly.

Server out of memory

I recently dealt with a server that stopped adding to its logs. It was as if the logs were frozen. After a lot of investigating it was found that the server had run out of memory.

Troubleshooting

Check your server space with the following command:

df -h

Investigate where the main memory is being used. This is very often in the /var/log/ folder.

If you would like to see the sizes of the files in a folder use this command:

ls -l  [folder]

eg: ls -l /var/log

If you would like to the see the sizes of folders:

du [folder]

eg: du /var/log

Clear up space by deleting files and folders

To delete a file use the following command:

sudo rm [filename]

To recreate a blank file use the following command:

sudo touch [filename]

Unable to find what is using server space

If you try to delete a file that is still in use by a service, the file will not delete off the server, but also not be displayed in the folder structure.

To investigate if there are any deleted files still in memory use this command:

lsof | grep ‘(deleted)’

You will see a list of files still in use with an id.

Use the following command to “kill” the file using the indicated id value:

kill -9 [id]