What Do You Do when an Ansible Playbook is Not Getting a Variable Assigned Properly?

Problem scenario
Ansible variables are not being picked up when the playbook runs. You may get no errors, or you may get errors like these:

"ERROR! Syntax Error while loading YAML…did not find expected key"

"ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path"

What should you do?

Possible Solution #1
Are you using the -i flag when you are running the command to execute the playbook? Did you recently start using a role instead of a playbook? The -i flag will prevent Ansible from looking in certain directories for an "all" file or "main.yml" file. If newly added variables are not being picked up, do you have them in different files? You may need to stick with one source file that works or change the directory from which you are running the playbook.

You may also want to see these links:
https://stackoverflow.com/questions/47159193/why-does-ansible-show-error-no-action-detected-in-task-error
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html

To learn about variable precedence, see this link.

Possible Solution #2
Put the end quotes at the end of the string rather than around the closing braces }}. Here is a line that would throw the error:
"{{ variable1 }}"/subdirectory/path/to/file

Here is a line that would not throw the error:
"{{ variable1 }}/subdirectory/path/to/file"

Possible Solution #3
From a pragmatic perspective, you may want to put the variables in a playbook and not in a role. This may help you avoid this error.

Possible Solution #4
You may want to view this posting when there are no visible errors when the playbook runs.

What Should You Do to Set Firefox to Be Your Default Browser in Windows 10?

Problem scenario
When you open Firefox in Windows 10 you get a prompt about whether or not it should be your default browser. You choose the option ("Use Firefox as my default browser") to make it your default browser, but it does not work. You see a window called "Settings" which has options for default applications.

What should you do to get Firefox to be your default browser in Windows 10?

Solution
(You can replace "Firefox" with "Chrome" if you want "Chrome" to be your web browser.) The solution is slightly counter-intuitive. If you want to set Firefox as your default app, in Settings* (this application window should appear after you click "Use Firefox as my default browser"), click on the default web browser (e.g., Microsoft Edge). The icon of the web browser that is your "Default app" is what you need to click on. Then choose from the following pop-up "Firefox".

If you want to select certain files to be associated with Firefox and not others do the following steps in the * below.

*In the Settings window, go to "Set defaults by app". Find Firefox. Click on it. Click "Manage". You will see various file extensions such as .htm, .html, .shtml, .xht etc. Click the icon of the web browser that is currently associated with the file, then click on "Firefox" that appears as a choice.

  • If "Settings" is not apparent, go to the circle in the left hand corner and search for "Settings". It is referred to as a "Trusted Microsoft store app." Open "Settings".

What is a ReplicationController in Kubernetes?

Question
What is a ReplicationController in Kubernetes?

Answer
It is a component that ensures the pods are constantly running (page 90 and 91 of Kubernetes in Action by Luksa). They were designed to create and manage redundant pods (page 91 of Kubernetes in Action by Luksa). They do just that -- they do not transfer pods but create new ones on different nodes (page 93 of Kubernetes in Action by Luksa).

There are three parts to ReplicationControllers: a label selector, a replica count, and a pod template (page 92 of Kubernetes in Action by Luksa).

They are created with a JSON or YAML file (page 93 of Kubernetes in Action by Luksa). If you post this file to the Kubernetes API server, a ReplicationController will be created and pods according to its specification including the number of pods, their label(s), the image of the container for the pods, and the container port values (page 94 of Kubernetes in Action by Luksa).

How Do You Use PowerShell to Find out if a Given Update Has Been Installed on Your Windows Workstation?

Problem scenario
You want to know if a particular KB was installed on your Windows machine. You know the KB number. How do you use PowerShell to find out if this update has been installed or not?

Solution
Run this: get-hotfix | sls "kb123456789" # Replace kb123456789 with the KB of your choice.

What Does ‘if name == “main”:’ in Python Mean?

Question
When programming in Python what does 'if name == "main":' mean?

Answer
To grasp this, realize that "main" is merely a quoted string. Now you must know how the built-in variable "name" is assigned. The name variable is set to main at run time by default.

From a command line or in a program, the value for name becomes "main". Here is an example:

>>> print(__name__)
__main__

Create two files foo.py and bar.py in the same directory and run foo.py:

# Here is the content of foo.py:
import bar
#import sys
#sys.path.insert(1, '/tmp/bar.py')
print("!!!!!!!!!!!!!!!!!")
print(__name__)
print("&&&&&&&&&&&&&&&&&") 

Here is the content of bar.py:

print ("Hello World from %s!" % __name__)

print("^^^^^^^^^^^^^^^^^^^^^^^^^^")
#print ("Hello World from %s!" % __main__)

if __name__ == "__main__":
    print("Hello World again from %s!" % __name__)

If you run foo.py (with bar.py in the same directory), you'll see this distinctive printout:

Hello World from bar!
^^^^^^^^^^^^^^^^^^^^^^^^^^
!!!!!!!!!!!!!!!!!
__main__
&&&&&&&&&&&&&&&&&

# You can see the first line was from bar.py (via "import bar").  The next lines were from foo.py.

Now, why would you want to do this 'if name == "main":'?

It allows you to repurpose a complex program. You may want to use a given module bar.py by itself: python bar.py

You may also want bar.py to be invoked via "import bar" from foo.py. You may want to leverage the program's functionality and have it enter an "if" clause when it is invoked via an import statement from a different program. Rather than develop separate programs, it can be very useful to ensure, via the "if" clause, certain preconditions are met before running a portion of the code.

It may be advisable to nest a program's main logic inside such a conditional if the program is going to be pushed to many different servers. Users will not accidentally execute the logic sequence in ways that were not intended. The .py file may be of value as a standalone program or via "import" statements in other programs. This equivalence logic facilitates reusing code and preventing execution that was not intended.

How Do You Deal with a Monitor Appearing in the Control Panel of Windows when That Monitor is Not Physically Attached to the Computer?

Problem scenario
In Windows Control Panel for Screen Resolution you see a reference to a monitor. But no monitor is physically attached. The monitor's graphic reference is dark (not a bluish gray) in the visual depiction of the positions of the different monitors. How do you eliminate this extra monitor reference?

Do you need to reinstall a display adapter or video card if there is an extra monitor listed in the Screen Resolution of the Windows Control Panel when no such monitor is physically attached?

Solution
Some monitors can connect via Bluetooth and TCP/IP. But assuming that this is not the situation, read the following.

Contrary to what some would think, there may be nothing wrong with your computer. You probably do not need to reinstall the display adapter driver. You simply need to remove this monitor via a simple manual process in the OS.

  1. On the "Display" drop down in "Screen Resolution" (in Control Panel), click on the Display device that corresponds with the integer (e.g., 2 or 3) with this extra monitor that is not really attached.
  2. Go to "Multiple Displays" a drop down just below "Display" referenced in the above step.
  3. Go to "Remove this display" in the drop down box referenced in step #2.
  4. Click "Apply".

How Do You Create a Recovery Drive (e.g., a USB stick) for Windows 10?

Problem scenario
You want a USB stick to use in case you need a new hard drive (e.g., your hard drive crashes). How do you create a recovery USB stick?

Solution

  1. Remove all USB sticks (as a recommended practice) except a USB stick that can be overwritten. This USB drive should have 8 GB or greater capacity.
  2. In the lower left hand search box, type in "Create a recover drive" with no quotes.
  3. Check the "Best Match" option at the top (above the suggestions). Click "Yes" to the pop up.
  4. Keep "Back up system files" checked. Click "Next". You may have to wait a few minutes.
  5. Select the USB flash drive from the list of "Available drive(s)". Click "Next."
  6. Click "Create" if you are willing to have all the files in the drive being deleted. If you get a prompt about not being able to create the recovery drive, is the drive really 8 GB? Some flash drives advertise this amount, but the real capacity is closer to 7.5 GB.

How Do You Test if a REST API Call Is Reaching a Linux Server?

Problem scenario
You have a Linux server with a web service listening on port 443 (or some other port). You are not sure if another server can reach this Linux server. There could be firewall issues or network problems restricting connectivity on a given port. You have used ping to test connectivity. You have used nmap to verify the port is listening. But you are not 100% sure the REST calls are reaching the intended server on the correct port (e.g., because of port forwarding or behind the scenes there could be some other problem). How do you test the connections in a non-theoretical way?

Solution
Prerequisite
You must have tcpdump installed. For a typical Debian/Ubuntu server, it will already be installed. On CentOS/RHEL/Fedora you could run this command to install it: sudo yum -y install tcpdump

Procedures
Step #1
On the server that is listening for a call over port 443, run this command (which will appear to hang):

sudo tcpdump -i any port 443

Step #2
While looking at the terminal of the above command, go to the server that needs to perform a GET or POST call on port 443. Have that server perform the operation to invoke the REST call on port 443.

You will see corresponding activity every time in real time (excepting only for the latency between the server making the REST calls and your listening Linux server). You can use a different port number in the above command to match the port you expect the REST call to use.

How Write a Custom Method inside of a Class in Groovy?

Problem scenario
You are familiar with Groovy scripts. Now you want to use a class and a method in the class. How do you do this?

Solution
Prerequisites
Install Groovy. See these directions for Ubuntu/Debian if you need assistance; see these directions for CentOS.

Procedures

  1. Create a file called test.groovy with the content below:
class Example {
   def static Display(nifty) {
      nifty.call("Red");
   }

   static void main(String[] args) {
      def str1 = "Cool attempt: ";
      def contint = { param -> println "${str1} ${param}" }
      contint.call("Blue");

      str1 = "Attempt: ";
      contint.call("Yellow");

      Example.Display(contint);
   }
}

2. Run it with this: groovy test.groovy

3. You are done. You see the ".call" syntax to invoke a method and pass a parameter.

How Do You Transform an Ansible Playbook into a Role?

Problem scenario
You want to refactor a playbook into a role. How do you do this?

Solution

  1. A role will be in a "roles" directory. Create the directory as such.
  2. Take your playbook (.yml or .yaml file) and eliminate the word "tasks" from it. The role should still be in the .yml or .yaml format.
  3. Variables must be extracted. This is the confusing portion. When you run a playbook that uses this new role, the variables must be in a directory that is visible to the playbook. If you use the "-i" flag when you call the playbook, make certain that the variables for this newly-created role (based on, perhaps minor, modifications of a playbook) are visible in a location that Ansible will look for. The way your ansible.cfg file is configured may affect this.
  4. Remove the hosts: stanza. That line doesn't belong in a .yaml file that is a role.

For more information, see these links: