How Do You Run an Ansible Playbook to Configure 2 GB of Swap Space on Every Linux Server?

Problem scenario
You want every Linux server to have 2 GB of virtual memory.  You want to transfer a Bash script to each server and run it with sudoer privileges.  How do you transfer a file and execute it as a sudoer user?

Solution
1.  Install Ansible.  If you need directions on how to do this with RHEL, see this posting.  For SUSE, see this posting.

2.  Configure the managed nodes so playbooks can be run.  This typically means that you would need passwordless SSH to be configured between them and the control server (that is the Ansible server).  If you need help with configuring passwordless SSH connectivity, see this posting.  

3.  Configure the user on each server to be a passwordless sudoer.  This means the user can assume sudoer privileges without being prompted for a password.  (This step has nothing to do with SSH.)  If you do not know how, see this posting.

4.  Optional step.  Get a benchmark.  Run this command to see what the current swap space is:

ansible -m shell -a 'free -m' all

Or maybe run these three commands:

ansible -m shell -a 'free -m' all >> /tmp/info.txt
date >> info.txt
echo "Will run a playbook shortly *** >> /tmp/info.txt

5.  Create a .yaml file (e.g., contint.yaml).  It will look like this*:

- hosts: all
  tasks:
  - copy:
      src: /home/cooluser/swapspace.sh
      dest: /home/cooluser/swapspace.sh
      owner: cooluser
      group: cooluser
      mode: 0644

- name: Execute the script
  hosts: all
  remote_user: cooluser
  become: yes
  tasks:
     - name: Execute the script.
       command: bash /home/cooluser/swapspace.sh

6.  In the .yaml file above, change the source (which refers to a directory on the Ansible control server), destination directory (which refers to a path and file on managed nodes, destination servers), owner, group, mode, remote_user, and command to whatever you desire.  Once you are done save the .yaml file.  You will have to create a source file.  This is a regular file separate from the .yaml file itself.  For the content of this file (referred to in the "src" stanza in the .yaml file above with its absolute path and file name), see this posting's step #2; the content of step #2 of that posting, what is called "addswap.sh", is the content you need for this posting's "swapspace.sh."  That link includes a Bash script that will allocate 2 GB of virtual memory (also known as swap space) from the hard drive.  The script works on Debian/Ubuntu, CentOS/RHEL/Fedora, and Linux SUSE distributions.  If you do not change the .yaml file above, place the link's Bash script under the name "swapspace.sh" in /home/cooluser/ on the Ansible server.

7.  Run this command:  ansible-playbook contint.yaml  # substitute "contint" with the name you gave it.  You are now done.

8.  Optional step.  Verify your work.  Run this command to see what the current swap space is:

ansible -m shell -a 'free -m' all

Or maybe run these three commands:

ansible -m shell -a 'free -m' all >> /tmp/info.txt
date >> info.txt
echo "Will run a playbook shortly *** >> /tmp/info.txt

# You can now look at /tmp/info.txt to see your work.

* If you are using a legacy version of Ansible, it will look like this:

- hosts: all
  tasks:
  - copy:
      src: /home/cooluser/swapspace.sh
      dest: /home/cooluser/swapspace.sh
      owner: cooluser
      group: cooluser
      mode: 0644

- name: Execute the script
  hosts: all
  remote_user: cooluser
  sudo: yes
  tasks:
     - name: Execute the script.
       command: bash /home/cooluser/swapspace.sh

Leave a comment

Your email address will not be published.