How Do You Install MongoDB on a Debian/Ubuntu Linux Server?

Problem scenario
You want to install MongoDB on an Ubuntu/Debian Linux server.  How do you do this?

Solution
1.  If you know your distribution of Linux, go to step #2.  Otherwise try this command:  cat /etc/*-release

2.  Do one of the following that corresponds to your distribution of Linux:

On Debian:  sudo apt-get -y install mongodb mongodb-server

On Ubuntu 16.x or Ubuntu 17.x run these two commands (that may span three lines):

curl http://repo.mongodb.org/apt/ubuntu/dists/xenial/mongodb-org/3.6/multiverse/binary-amd64/mongodb-org-server_3.6.4_amd64.deb > /tmp/mongodb-org-server_3.6.4_amd64.deb
dpkg -i /tmp/mongodb-org-server_3.6.4_amd64.deb

3.  To test:  mongod -version


If you want to install MongoDB on a RedHat derivative of Linux, please see this posting.

How Do You Install Groovy on Centos 7.X?

Problem scenario
You want to install Groovy on CentOS 7.x but no yum repositories are configured for directly installing it.  What should you do?

Solution
Run these commands:

sudo yum -y install ant ant-junit antlr-tool apache-commons-cli apache-commons-logging apache-ivy bsf jansi jline junit objectweb-asm tomcat-jsp-2.2-api tomcat-servlet-3.0-api xstream

curl http://rpmfind.net/linux/centos/7.4.1708/os/x86_64/Packages/groovy-1.8.9-7.el7.noarch.rpm > /tmp/groovy-1.8.9-7.el7.noarch.rpm

sudo rpm -i /tmp/groovy-1.8.9-7.el7.noarch.rpm

How Do You Create a Dockerfile and Build a Docker Image?

Problem scenario
You know how to use Docker containers.  You know how to use Docker images if they are already built.  Now you want to use a Dockerfile to understand how to build images.  How do you use Dockerfile and how do you create your own image?

Solution

Prerequisites
This assumes you have installed Docker on your Linux server.  If you have not, click the URL corresponding with your distribution of Linux:

CentOS/RHEL/Fedora
Debian/Ubuntu
SUSE

Procedures
 1.  Use vi or a text editor.  Create a file called Dockerfile with the following content:

FROM ubuntu:latest
MAINTAINER NAME EMAIL

RUN apt-get -y update && apt-get -y upgrade && apt-get install -y build-essential

2.  Run this command to create a Docker image:  docker build -t "foobar:contint" .

Remember to substitute foobar and contint (in the above command) according to the following bullets:

-  foobar can be any arbitrary string as long as it is all lowercase.  This is the name of a repository of images.

-  contint is the "tag" name to idenify the image.  It have uppercase and lowercase letters.

3.  The Docker image has now been built.  (For your information the period in the docker build -t "foobar:contint" . command specifies the build context location.)

You are done. If you want to create a Docker container from this image just created, see this posting. When a docker build command runs, where is the image stored? For a pragmatic answer, use this command: docker images

For a more detailed answer, see this external page.

If you want more information, see this link.  For detailed reference information, see this link.  See this link for best practices.  To create a private registry of Docker images, see this posting.  If you want to purchase a book on Docker, click here.

How Do You Leverage Ansible to Deploy Salt Minion to an Ansible Managed Node?

Problem scenario
You have two configuration management servers: one is an Ansible control server and another is a Salt Master server.  You want Ansible to deploy Salt Minion to the managed nodes.  You want the managed nodes of Ansible to receive configurations from the Salt Master server.  This way one team can use SaltStack and another team can use Ansible.  How do you do this?

Solution

Prerequisites
This assumes you have deployed Salt Master and Ansible.  (If you are running Linux SUSE and need to install Ansible, see this posting.)  This assumes that a third server is configured to be a managed node and receives configurations from the Ansible control server with playbook runs.

This solution assumes you have the same username with sudoer privileges on each managed node.  For the example below we use "cooluser".  This username needs to be able to use sudo without being prompted for a password.  If you need assistance with this, see this posting.

Procedures
1.  Get a file that will be the template and source of the /etc/salt/minion file for the Salt Minion client servers.  Place this copy on the Ansible control server.  (If you want more information about this file, see "How do you install and configure Salt..." step #7 of this posting.)

2.  Place this file where you have other source files on the Ansible control server (and you can name it sminstaller.txt or whatever you want):

distro=$(cat /etc/*-release | grep NAME)

debflag=$(echo $distro | grep -i "ubuntu")
if [ -z "$debflag" ]
then   # If it is not Ubuntu, test if it is Debian.
  debflag=$(echo $distro | grep -i "debian")
  echo "determining Linux distribution..."
else
   echo "You have Ubuntu Linux!"
fi

rhflag=$(echo $distro | grep -i "red*hat")
if [ -z "$rhflag" ]
then   #If it is not RedHat, see if it is CentOS or Fedora.
  rhflag=$(echo $distro | grep -i "centos")
  if [ -z "$rhflag" ]
    then    #If it is neither RedHat nor CentOS, see if it is Fedora.
    echo "It does not appear to be CentOS or RHEL..."
    rhflag=$(echo $distro | grep -i "fedora")
    fi
fi

if [ -z "$rhflag" ]
  then
  echo "...still determining Linux distribution..."
else
  echo "You have a RedHat distribution (e.g., CentOS, RHEL, or Fedora)"
  yum -y install salt-minion   # install nc for initial testing only.
fi

if [ -z "$debflag" ]
then
  echo "...still determining Linux distribution..."
else
   echo "You are using either Ubuntu Linux or Debian Linux."
   apt-get -y install salt-minion
fi

suseflag=$(echo $distro | grep -i "suse")
if [ -z "$suseflag" ]
then
  if [ -z "$debflag" ]
  then
    if [ -z "$rhflag" ]
      then
      echo "*******************************************"
      echo "Could not determine the Linux distribution!"
      echo "Installation aborted. Nothing was done."
      echo "******************************************"
      exit
    fi
  fi
else
   zypper -n install salt-minion
fi

3.  On the Ansible control server write an Ansible playbook (e.g., a .yaml file) to transfer the files in step #1 and step #2. This playbook will execute the file in step #2 (above) on the managed node after it is transferred.  Create a .yaml file (e.g., contint.yaml) with the following lines (excluding the last "#**" lines):

- name: something
  hosts: all
  remote_user: cooluser
  become: yes
  tasks:
  - file:
      path: /etc/salt
      state: directory
      owner: cooluser
      mode: 0744

- name: Transfer the script file down.
  hosts: all
  tasks:
  - copy:
      src: /home/cooluser/sminstaller.txt # File in step #2; rename if whatever
      dest: /tmp/smin.sh
      owner: cooluser
      mode: 0644

- name: Install Salt Minion on any distribution of Linux.
  hosts: all
  remote_user: cooluser
  become: yes
  tasks:
     - name: Run the installer
       command: bash /tmp/smin.sh
     - name: Run the salt minion service for the first time.
       command: salt-minion
       async: 10
       poll: 0

- name: Transfer the minion file down.
  hosts: all
  tasks:
  - copy:
      src: /home/cooluser/minion.txt # File in step #1.
      dest: /etc/salt/minion
      owner: cooluser
      mode: 0644

#** Change the "cooluser" to a username that has sudoer privileges.
#** Change the src file names and directory paths above to match your configuration.

4.  Run the playbook with this command:  ansible-playbook contint.yaml

5.a.  Go to the Salt Master server to configure the new Salt Minions to work with the Salt Master server itself.  Run this command:  sudo salt-key -L

5.b.  Run this command but substitute "<nameOfSaltMinion>" with the hostname of the Salt Minion client based on the results of the above command:  sudo salt-key -a <nameOfSaltMinion>

How Do You Get Ansible Playbooks to Create Directories on Managed Nodes?

Problem scenario
You want Ansible to create directories on managed nodes.  How do you write a playbook to do this?

Solution
Prerequisites

This assumes that you have installed and configured Ansible.  If you do not know how to deploy Ansible, see this posting if you are using a Red Hat derivative.  If you are using Linux SUSE, see this posting.  If you need help running a playbook, see this posting.

Procedures
In the playbook (the .yaml file) for the section that creates the subdirectory, add these two stanzas where "cooluser" is a username with sudoer privileges on the managed node:

  remote_user: cooluser
  become: yes

Here is a complete playbook that creates a directory called "contint" in the /etc/ directory:

- name: something
  hosts: all
  remote_user: cooluser
  become: yes
  tasks:
  - file:
      path: /etc/contint
      state: directory
      owner: cooluser
      mode: 0744

How Do You Find the Size of an RPM Package before You Install It?

Problem scenario
You want to find out how much disk space you will consume before you install a given yum or RPM package.  You want to list available packages and their size based on a near match of a name.  What is the Linux (e.g., yum) command to find size of the package before you actually install it?

Solution

Prerequisites
This assumes that you have installed yum-utils.  If you need to install yum-utils, use this command: sudo yum -y install yum-utils

Procedures
Use this command but replace "openssl" with the name of the package you want to learn the more about):

sudo repoquery *openssl* --qf "%{name}:\n%{size:h}"

# We do not recommend changing the "h" to a "k" or "m" as the man page may suggest.

You may want to try this command too (but replace "openssl" with the name of the package you want):

yum info openssl

How Do You Install npm and Node.Js on Any Distribution of Linux?

Problem scenario
You have Linux servers of different distributions (including Debian/Ubuntu, Red Hat derivatives such as CentOS, RHEL, or Fedora, and Linux SUSE).  You want to install npm and Node.js on them.  What do you do?

Solution
Prerequisites
i.  You need a server with at least 1 GB of RAM.  To add virtual memory, see this posting.  To resize your GCP server, see this posting.  To upgrade your EC-2 instance (a VM running in AWS), see this posting.
ii.  Install the C compiler.  If you do not know how, see this posting.
iii. You have the make utility installed. If you need assistance, see this posting.

Procedures
1.  Create a file named "npminstaller.sh" (with no quotes) with the following content:

cd /opt/
curl http://nodejs.org/dist/v14.1.0/node-v14.1.0.tar.gz > node-v14.1.0.tar.gz
tar xzvf node-v14.1.0.tar.gz && cd node-v14*
./configure
make
make install

2.  Run the script with "sudo bash npminstaller.sh" with no quotes.  This script may take over one hour to run.

How Do You Troubleshoot the PowerShell Error “Cannot connect to CIM Server: Access is denied”?

Problem scenario
You run a PowerShell command with a user who is not a local administrator on the server.  You get the error "Cannot connect to CIM server: Access is denied".  How do you circumvent this error?

Possible solution #1
Make the user a local administrator.  Then try again.

Possible solution #2
Use JEA to allow the user to run the command.  If you want directions on deploying JEA, see this posting.

Possible solution #3
1. As an Administrator of the server, go to Server Manager -> Tools -> Computer Management.  On the left expand "Services and Applications" and right click "WMI Control".  Go to "Properties".

2.  In the newly open Window, click on Security tab.

3.  Expand Root tree, and then click on the node CIMV2, and click the button security

4.  In the newly open Window, click the button Advanced.

5.  In the newly open Window, click the button Add under the permission tab.

6.  In the newly open Window, click on “select a principal", then search for the user you that was having the problem.  

'7.  In the applies to, choose “this namespace and subnamespace".

8.  For the permission, check on “Execute Methods", “Enable Accounts" and “Remote Enable"

9.  Click accept on all the open dialogue boxes

10.  Restart WMI services.  As an Admininstrator of the server, go to Server Manager -> Tools -> Computer Management.  On the left expand "Services and Applications" and click on "Services".  Go to "Windows Management Instrumentation" and right click it.  Then choose "Restart".

11.  Try the command again. The above directions were adapted from this StackOverflow posting.


For further reading
To learn more about the Common Information Model of PowerShell, see PowerShell in Depth; the index of the book for "Common Information Model" has numerous references.

How Do You Troubleshoot the Powershell Error “Cannot find Get-Command”?

Problem scenario
You run a PowerShell command.  You get "Cannot find Get-Command" even though the command you entered was not Get-Command.  How do you solve this error?

Possible Solution #1
As an Admininstrator of the server, go to Server Manager -> Tools -> Computer Management.  In this Window (or MMC) go to System Tools -> Local Users and Groups -> Users.  Double click on the User that received the error.  

Is the user a member of Guests or Users?  Is this intentional?  You may want to add the user to a group.

Are environmental variables set as normal?  Will commands like "ls", "dir" or "whoami" work?  If not run these commands:

$env:UserName
$env:PSmodulePath

This may help you troubleshoot the problem.

Possible Solution #2
Are you using JEA when this problem exists?  If so, is your PowerShell Service Configuration (.pssc) file set to run as a virtual account?  That is, do you have a stanza such as one of these below?

# RunAsVirtualAccount = $true
RunAsVirtualAccount = $false

The top one is commented out.   Without using a virtual account, certain variables may not be readily available in the session.  If the "whoami" command does not work, try this: "$env:UserName" # with no quotes.

How Do You Create a Function in the C Programming Language?

Problem scenario
You are familiar with functions in programming languages (e.g., Python or SQL).  You like how you can create a modular program with a function.  Functions can allow you to reuse code and do data processing in a way that is readable.  Logic can be created rapidly when you know how to use a function.  How do you write a function in the C?

Solution

Prerequisites
Install the C compiler.  If you need directions, see this posting.

Procedures
Use this example.  The function here is called "printer". 

1.  Create a file called funcexample.cc.  

2.  Put the following lines in it (starting with "/* function example */" and ending with the last "}").

/* function example */
#include <stdio.h>
#include <string.h>

int printer(int z) {
   printf ("found %s\n","a test");
   z = z + 1;
   return z;
}
int main ()
{
  int y;
  y = printer(3);
  printf ("y is a %d\n",y);
  return 0;
}

3.  Compile the code with gcc funcexample.cc.

4.  Run it like this: ./a.out
You should see that y was assigned via the "printer" function.  Remember that "printer" is not a reserved word.  It is a function that returns an integer based on the way it was defined above.