How Do You Troubleshoot “/usr/bin/env: ‘python’: No such file or directory”?

Problem scenario
You try to run an AWS CLI command but you receive this error:
/usr/bin/env: ‘python’: No such file or directory

What should you do?

Possible Solution #1
If Python 3 is not installed, install it. You may want to see these postings:
How Do You Upgrade to Python 3.x on Ubuntu 16?
How Do You Upgrade Python 2.x to Python 3.7 in Debian or Ubuntu Linux?

Possible Solution #2
If Python 3 has been installed, run this command:
whereis python3
# If the above did not work, try this: sudo find / -name python3 -type f

Run a command like this, but substitute /path/to/python3 with one of the results in the above command (e.g., /usr/bin/python3).

sudo ln -s /path/to/python3 /usr/bin/python

That should be it. You should now try to run your Python command again.


If you want to purchase a book on Python, please see this link.

How Do You Create Your Own kubeconfig File?

Problem scenario
You want to create a kubeconfig file automatically. But you do not know how. What should you do?

Prerequisites
If kubelet, kubeadm and kubectl are installed, skip the prerequisites to go to step #1 in the procedures. Otherwise, run these commands:

cd /tmp

curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl

curl -Lo kubeadm https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubeadm && chmod +x kubeadm

curl -Lo kubelet https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubelet && chmod +x kubelet

sudo mv /tmp/kube* /usr/bin

If you are running CentOS/RHEL/Fedora, run this command: sudo yum -y install socat ebltables

Procedures
Run this command: sudo kubeadm init

The above command will create this file; it can be a template for a kubeconfig file: /etc/kubernetes/admin.conf

If you want to create one manually, you may want to see this posting.

How Do You Install Molecule on an Ubuntu 18.x Linux Server?

Problem scenario
You want to use Molecule to test Ansible roles. How do you install Molecule on Ubuntu 18.x with Python 3?

Solution
Prerequisites
i. You must have Docker installed. If you need assistance see this posting.
ii. You must have pip3 installed on the Docker host. sudo apt-get install -y python3-pip

Procedures
1. Run these commands:

python3 -m pip install virtualenv
python3 -m virtualenv my_env
source my_env/bin/activate
python3 -m pip install molecule docker
molecule init role -r ansible-apache -d docker
cd ansible-apache
molecule test

2.a. Run this command: vi tasks/main.yml
2.b. Delete what is there. Put this there instead:

---
- name: "Ensure required packages are present"
  yum:
    name: "{{ pkg_list }}"
    state: present

- name: "Ensure latest index.html is present"
  template:
    src: index.html.j2
    dest: /var/www/html/index.html

- name: "Ensure httpd service is started and enabled"
  service:
    name: "{{ item }}"
    state: started
    enabled: true
  with_items: "{{ svc_list }}"

- name: "Whitelist http in firewalld"
  firewalld:
    service: http
    state: enabled
    permanent: true
    immediate: true

3. Run this command: mkdir templates

4.a. Run this command: vi templates/index.html.j2
4.b. Put these three lines in it and save it:

<div style="text-align: center">
    <h2>Managed by Ansible</h2>
</div>

5.a. Run this command: vi vars/main.yml
5.b. Delete what is there. Place this code starting at the first non-blank line:

---
pkg_list:
  - httpd
  - firewalld
svc_list:
  - httpd
  - firewalld

6.a. Run this command: vi molecule/default/tests/test_default.py
6.b. Eliminate the content. Place these lines in the place of the old content:

import os
import pytest

import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


@pytest.mark.parametrize('pkg', [
  'httpd',
  'firewalld'
])
def test_pkg(host, pkg):
    package = host.package(pkg)

    assert package.is_installed


@pytest.mark.parametrize('svc', [
  'httpd',
  'firewalld'
])
def test_svc(host, svc):
    service = host.service(svc)

    assert service.is_running
    assert service.is_enabled


@pytest.mark.parametrize('file, content', [
  ("/etc/firewalld/zones/public.xml", "<service name=\"http\"/>"),
  ("/var/www/html/index.html", "Managed by Ansible")
])
def test_files(host, file, content):
    file = host.file(file)

    assert file.exists
    assert file.contains(content)

7. Run this: molecule test

8. You are done. The above directions were adapted from a DigitalOcean tutorial here.

How Do You Troubleshoot the Error “kubelet service is not enabled” or How Do You Write Your Own .service File in Linux?

Problem scenario
You try to run a kubeadm command. You get an error about kubelet

" [WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'"

How do you get "sudo systemctl start kubelet" or "sudo systemctl enable kubelet" to work? How do you create your own kubelet.service file?

Solution

  1. Create this file: /etc/systemd/system/kubelet.service
  2. The content should be as follows (but you may need to double check that the kubelet file is there; you may want to read "How do you manually install kubelet on any type of Linux?"):
[Unit]
Description=kubelet

[Service]
Type=simple
User=docker
Group=docker

ExecStart=/usr/bin/kubelet
Restart=always
WorkingDirectory=/
Nice=19

[Install]
WantedBy=multi-user.target

3. Now try these commands:

sudo systemctl start kubelet
sudo systemctl enable kubelet

(It may not stay on. But this can work for satisfying certain dependencies for proving experimental concepts. If you want the kubelet service to remain on and you are running a Red Hat derivative of Linux, see this posting.)

How Do You Troubleshoot Computer Speakers Not Working with Your Windows 10 Computer?

Problem scenario
You have some older speakers plugged into your laptop that was recently reformatted (Windows 10 was recently installed). You know the laptop's audio works via its built-in speakers. You know the speakers work from your a second laptop. But one laptop seems not compatible with one pair of head phones or one set of external speakers. There is a 3.5 mm jack that you connect the speakers to your laptop. Everything seems fine, but there is no sound. What should you do?

Solution

  1. Go to the Windows search field in the lower left hand corner. Type in "Sound" (with no quotes) and press Enter.
  2. Click on "Sound Control Panel"
  3. Double click "Speakers/Headphones"
  4. Go to the "Advanced" tab.
  5. Click on different "Default Format" options in the drop down menu (e.g, 16 bit or 24 bit, and different Hz options) one at a time. After you click on one option, click "Test."
  6. Reboot your computer to see if the problem has been resolved. If you still have a problem, can you go to the Windows Device Manager and upgrade the driver for the audio device? If the driver date is April 2018 or you cannot upgrade the driver, try installing this driver here: https://ftp.station-drivers.com/index.php?option=com_remository&Itemid=353&func=fileinfo&id=3738&lang=en
    (Go to the link above and find the word "Download" in the largest font of the other "Download" words. Run the .exe as administrator. Your computer will need to reboot twice, but it will automatically uninstall the previous driver.)
  7. If you know the laptop's 3.5 mm jack works and the speakers work, you may want to contact a computer repair person if the combination of hardware does not work and the above steps did not help.

See also this posting. As a last resort (if you have a physical hardware problem) you may want to buy new speakers; you can do so here. You can buy a new laptop here.

How Do You Get an Ansible Variable to Not Add Single Quotes or Brackets when the Variable Is Substituted?

Problem scenario
You are using an Ansible-supported method of a variable in a playbook. This variable will substitute a string or line number that the playbook is run on with the IP address of the host. You notice that this substitution includes an opening-and-closing single quote mark "'" and a pair of opening-and-closing brackets "[]". That is, single quotes and brackets are being introduced into the string you want to build with the Ansible playbook.

The playbook (aka .yaml file) has lines like these:

vars:
ip_address: "{{ ansible_all_ipv4_addresses }}"

The string that is later being created shows IP addresses like these:

['12.34.56.78']

You want it to be like this: 12.34.56.78

How do you get the playbook to work without adding these quote and bracket characters?

Solution
Rather than use the format (or syntax above), use this format below in your playbook:
vars:
ip_address: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"

YoYou may also want to see How Do You Get an Ansible Variable to Not Add Single Quotes or Brackets when the Variable Is Substituted?.

How Do You Get sonar-scanner to Work When You Seem to Have No Quality Profiles and Your Server Has No Internet Access?

Problem scenario
You run sonar-scanner, but it fails with an error such as this: "No quality profiles have been found, you probably don't have any language plugin installed."

You run sonar-scanner with the "-X" and see a Java stack trace like this: "Error during SonarQube Scanner execution java.lang.IllegalStateException: Unable to load component class org.sonar.scanner.report.MetadataPublisher"

You log into the SonarQube web UI. You go to "Quality Profiles" but you see no profiles and no button or option to create one.

How do you get sonar-scanner to work?

Solution

1. Go to the server with the sonar-scanner.
2. Find the main sonar directory. One command that should help is this: sudo find / -name sonar -type d
In this directory there should be a directory path such as this: extensions/plugins

3. Go to …/sonar/extensions/plugins/ and place one or more language .jar files so that SonarQube can analyze code of that language. The free Community Edition of SonarQube cannot support certain languages as there are no .jar files for certain languages. But it can support many common, popular, modern languages. To obtain the .jar file for a given language (that is a plugin for SonarQube to have a Quality Profile), go here. You can copy the .jar file to the SonarQube server.

How Do You Invoke a Function in Groovy?

Problem scenario
You want to call a function and have it return a value. How do you do this?

Solution
Overview
(If you need assistance installing Groovy, see this posting for CentOS. or see this posting for Debian/Ubuntu Linux.)

Groovy has something called "Closures." These act like functions in other programming languages. The syntax looks like his with the square brackets portion being a comma-delimited list of parameters:

{ [closureParameters -> ] statements }

In the above, the "statements" signifies zero or more Groovy expressions. Here is an example of a Groovy statement:

println "${str1} ${param}"

The above will print the value of "str1" and the value of "param". Normally "param" will be a parameter passed at the time the closure is invoked. Closures can be invoked with syntax like this: nameOfClosure.call("coolvalue")

The above would pass "coolvalue" as a parameter when nameOfClosure was invoked.

Procedures
Create a Groovy program called test.groovy with the following code:

def str1 = "Cool attempt: ";
def contint = { param -> println "${str1} ${param}" }
contint.call("This shows how a Closure is invoked!");

How Do You Deactivate a HipChat User with Only His/Her Email Address Using the REST API?

Problem scenario
You have email addresses of users that you want deactivated. You do not want to use the GUI for HipChat. How do you invoke REST APIs to deactivate a user?

Prerequisite
You need an API token for HipChat. If you have this, start with the Procedures below. Otherwise keep reading this "Prerequisite" section. You need several scopes for the API token authorize you to be able to deactivate a user. Once you have the alphanumeric string of an unexpired token that has sufficient privileges to delete users, you can craft a curl command.

To generate a token, log into the HipChat web UI. Go to the upper right hand corner and click on your icon, then click on "Account". See these directions and start on step #5:
https://community.frontapp.com/t/186p1p/how-to-enable-and-use-the-hipchat-integration

Procedures
Here is how to deactivate a user based on email address using the REST API:

curl -H "Authorization:Bearer abcd1234" -X DELETE http://hipchatservername.com/v2/user/jdoe@acme.com --insecure

# The command is "DELETE", but it does not delete the user.  It deactivates the user.

# If you want to learn more about how passing an API token works, you may want to read this link https://stackoverflow.com/questions/22229996/basic-http-and-bearer-token-authentication

How Do You Install Docker on Debian Linux in GCP?

Problem scneario
In Google Cloud Platform you have a Debian Linux server. You want to install Docker on it. What do you do?

Solution
1. Create a file called dockerinstall.sh in the /tmp/ directory with the following content:

apt-get -y update
apt-get install -y apt-transport-https ca-certificates wget software-properties-common

wget https://download.docker.com/linux/debian/gpg 
apt-key add gpg

echo "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee -a /etc/apt/sources.list.d/docker.list
apt-get -y update

apt-get -y install docker-ce

2. Run this script with sudo (e.g., sudo bash /tmp/dockerinstall.sh). It may take 10 minutes and appear to stall. This is normal.