Tag: ansible

  • Ansible Ad-Hoc Commands

    Ansible Ad-Hoc Commands

    Introduction

    Ansible gives you a powerful option to run commands ad-hoc. This negates the need to write a script, if you only need some quick information. There are two separate options for gathering data via ad-hoc. If you are running just a single command than use the ‘command’ module. If you need to run multiple commands, use the ‘shell’ module.

    You may or may not need to reference the your inventory file, if you are using DNS names. If you are using IP addresses, then you probably do not need it.

    -m = module (shell or command)
    -a = argument (command you want to run on the remote system).

    Run a Single Command against Multiple Hosts

    ansible -i inventory.ini -m command -a 'ip a' server1,server2

    Run Multiple Commands against a Single Machine

  • Validate the Integrity of a File Backup using Ansible

    Validate the Integrity of a File Backup using Ansible

    Introduction

    Running nightly file backups is a common task for administrators. How do we know the file was copied successfully with no errors? In this post, we will set up an ansible script and it will run a file integrity check using MD5 on both the source and the destination files to verify it was not corrupted during the copy process. In this process the Ansible server is assumed to be a separate server from both the source server and the designation server.

    Specifically, we will tell Ansible to execute a bash script on the source and destination servers, gather the results and store them in a temp text file, then it will output the text file to the body of an email and send it to interested parties for review.

    Create the Ansible Script

    Add comments to the head of the script. I like to include an example of the command, so that it can be easily copied to the command line.

    Add the variables to the script. All ansible scripts start with three dashes. Also note the Ansible is very sensitive to the placement of the columns. The names, hosts, and tasks columns must be lined up exact or the script will not execute.

    Add the tasks that must be executed.

    Finally we will send an email to interested parties.

    Build the Bash Script

    In Ansible, it will execute the code on all servers simultaneously. So, we don’t know what server’s results will be returned to Ansible first. That is why we need the server hostname.

    Create the headers.

    Create the variables.

    Execute the comamnds, to gather the needed data.

    Output the results. Remember these results will be returned to Ansible.

    This is my own method for verifying files were copied correctly. I hope you find it useful.

  • Update, Reboot, & Get the Health of Remote Servers

    Update, Reboot, & Get the Health of Remote Servers

    Introduction

    Ansible was designed to remotely manage multiple Linux servers simultaneously. Scripts can be used for many common tasks like updating, rebooting, or to the check health of your Linux servers . Ansible scripts are very format sensitive, so be sure that all columns match up, as below, or it will not run.

    Notice that some scripts call sudo and you need the ‘-K’ switch in the command. You can tell if the script calls sudo by the line ‘become: yes’.

    Update & Reboot all Servers

    This will updated, upgrade, remove unnecessary files, and clear the local repository cache. Finally, it will send an email when completed. It will run against all servers, in the ini file, listed in the group called ‘all_servers’.

    Reboot Specific Servers

    Check the Health of the Remote Servers

    The check health script gathers basic information about the remote servers. Is the hard disk drive full? Does the server need a reboot? How long has the server been up?

    The ansible script calls a bash script, that is then executed on all remote hosts. The results are returned and printed to a text file. An email copies the contents of the text file to the body of the email and results are emailed. Be sure to save the inventory.ini, bash scripts, and the ansible scripts in the same directory.

  • Managing User Accounts with Ansible

    Managing User Accounts with Ansible

    Introduction

    Ansible is a program designed to manage Linux servers. See blog post on setting up Ansible, creating a script, creating an inventory file, and calling a script from the command line. You can call a single server or multiple servers by separating then with a colon on the ‘hosts’ line. If a large number of hosts needs to be called, create a group in the inventory.ini file and call the group on the ‘hosts’ line.

    Create a New User’s Account on Multiple Servers

    When a new hire comes onboard, rather than log into each server directly and manually create their accounts, run this script and it will create the accounts on all servers simultaneously.

    Get a List of Servers that have a Specific User’s Account

    If a user leaves the company, you can never be sure what Linux servers they were given access to, so I run this to get a list of what servers they have accounts. It outputs the results to a text file, which the results can be easily be viewed. This script calls a host group called “all_servers” in the inventory.ini file.

    Remove A User’s Account

    Once you have identified which servers the user has an account on, add the username to the script and specify the target hosts. As before, you can list multiple servers, separated by a colon, or create a group in the *ini file and then add the group name to the ‘hosts’ line.

    Push a Key to Multiple Servers

    It is recommended that users login using public and private keys. It is easy to push a users public key to multiple servers at same time. Replace the ‘key’, with the user’s actual public key surrounded by double quotes.

    The authorized key command handles creating the directors and setting permissions on all files.

    Change a User’s Password

    Perhaps a user forgot their password, or they have left the company. You may need to change their password. Again, modify the ‘hosts’ line as necessary, with a single, multiple, or a group of servers.