Convert from Python to JSON

If you have a Python object, you can convert it into a JSON string by using the json.dumps() method.

Example

Convert from Python to JSON:


 

Python Flask how to get parameters from a URL

In Flask, How do I extract parameters from a URL? How can I extract named parameters from a URL using flask and python?

When the user accesses this URL running on my flask app, I want the web service to be able to handle the parameters specified after the question mark:

Use request.args to get parsed contents of query string:

API URL will be as http://api.wpdemo.com/your-path?url=http://abc.com

Ref:

Ubuntu 16 default python is almost python 3

Loads of solutions exist, but for changing the system default, alias is not the way to go.

$ update-alternatives –list python update-alternatives: error: no alternatives for python

So we have 2 – use these in update-alernatives

Done

Create new post WordPress programmatically with custom meta fields

How can you create and insert new posts containing already filled Advanced Custom Fields programmatically into your database?

  1. Say you have an ACF group with 3 fields each of a different type
    • description (type Text)
    • image (type Image)
    • categories (type Taxonomy)
  2. prepare a basic post

  1. insert post and keep the created post id

  1. then update values of custom fields using the post id

… Or it can be done using the following code:

To enter a new post for a custom type

 

After inserting the post, a post id will be returned by the above function. Now if you want to enter any post meta information w.r.t this post then following code snippet can be used.

How to test sendmail from the terminal

Sendmail is an open source smtp server. Many different languages and frameworks interface with sendmail, and when debugging it’s good to be able to quickly test whether or not sendmail is working.

The following is a quick way to verify that sendmail is working.

enter image description here

WordPress display the_meta() of in custom page

I have some meta values on my wordpress custom page that I need to be displayed in a table. Is there any way I can do that.

Here’s the code I’m using now: <?php the_meta(); ?>

And this is what it shows:

enter image description here

I want to do something like:

enter image description here

I found out that:

the_meta() is located in wp-includes/post-template.php

This makes the output:

 

So it’s not recommended to edit files in that folder because of the wordpress update.

You can make a table in html and display each value individually, like this:

 

Here‘s the documentation or https://codex.wordpress.org/Using_Custom_Fields

How to Find the ID Facebook Page

Facebook requires you to input the Page ID when creating social plugins and chat widgets for the Messenger platform but they do not offer any easy way to determine the ID of Facebook pages. This simple guide should help.

Find Facebook Page ID

Step 1: Open any Facebook Page in your desktop browser and right-click the profile image of the page.

Step 2: Click “Copy Link Address” from the right-click menu to copy the photo link to your clipboard.

Step 3: Open Notepad (or Text Edit on your Mac) and paste the URL of the image. It would look something like this:

And the Page ID is 5550296508

Ansible task executed if file not exists condition

In this post, I will share an ansible task that is executed only if a condition is matched, the condition here is to check if file NOT exists on the system.

Ansible playbook create a new nginx vhost by template for wp at nginx proxy, then install wordpress latest if wp-config.php is not exists

if wp-config.php exists, the task Download WP is skipping.

Happy reading!

Up and running kubernetes cluster on aws eks in 10 minutes

In this post, I will share the post Up and running kubernetes cluster on aws eks in 10 minutes with cli tool called eksctl.

eksctl is cli tool written in GO by weaveworks and based on Amazon’s official CloudFormation templates.

Install eksctl

In our example, we will use wget, so make sure it is installed before you proceed.

On MAC you can do

Configure AWS API credentials

Install pip:

You can now install an awscli package using pip:

Let’s configure aws key/secret

Create Amazon EKS Cluster with eksctl

When all settings have been saved, you can now create a new cluster on EKS:

Options that can be used include:

Let’s begin creating our first kubernetes cluster with eksctl

Once you have created a cluster, the cluster credentials will be added in ~/.kube/config

To enable Auto Scaling Group for worker nodes group, use the options:

To get details about the deployed cluster or delete cluster:

Deploying Kubernetes on AWS EKS service with eksctl is easy, you do not need to struggle with aws console in UI. All cluster configurations are saved on deployment machine, you can quickly make changes and update your cluster.

Ansible structure layout – the best approach

This is called Ansible structure layout – the best approach that we used to use it as a template for our projects. This may be not the best but you can customized it as to fit your own.

Directory Layout

The top level of the directory would contain files and directories like so:

How to Arrange Inventory, Stage vs Production

  • production.ini: contains the inventory of all production hosts
  • stage.ini: contains the inventory of all staging hosts

It is suggested that you define groups based on the purpose of the host (roles) and also geography or data center location (if applicable):

Group And Host Variables

We can also assign variables to all hosts in a group or specific group or specific hosts. Let’s set those now:

If we had any default values or values that were universally true, we would put them in a file called group_vars/all:

We can define specific hardware variance in systems in a host_vars file, but avoid doing this unless you need to:

Playbooks Are Separated By Role

Playbook webservers.yml is used for all hosts in group webservers with roles common, nginx

Task And Handler Organization For A Role

Below is an example tasks file that explains how a role works. Our common role here just sets up NTP, but it could do more if we wanted:

Here is an example handlers file. As a review, handlers are only fired when certain tasks report changes and are run at the end of each play:

Let’s run your playbook

What about just reconfiguring NTP on everything? Easy.:


What about just reconfiguring my webservers?:


What about just my webservers in Boston?:


What about just the first 10, and then the next 10?:

And of course, just basic ad-hoc stuff is also possible.:

Stage vs Production

As also mentioned above, a good way to keep your stage (or testing) and production environments separate is to use a separate inventory file for stage and production. This way you pick with -i what you are targeting. Keeping them all in one file can lead to surprises!