Ansible Commands Cheat Sheet



Here are some commands which are used in Ansible, such as:

  • To install EPEL repo on Centos/RHEL systems.

Ansible modules are standalone scripts that can be used inside an Ansible playbook. You can use these modules to run whatever commands it needs to get its job done. Ansible modules are categorized into various groups based on their functionality. There are hundreds of Ansible. Ping specific node ansible -i hosts nycweb01.prod.local -m ping Ping with wildcard ansible -i hosts 'nycweb.' -m ping Ping all nodes with SSH user 'root' ansible -i hosts all -m ping -u root run a command ansible -i hosts dev -a 'uname -a' check Yum packages ansible -i hosts dev -m yum check if Docker rpm is installed ansible -i hosts web01.nyc.

  • To install Ansible package on Centos/RHEL systems.
  • To perform an update to the packages on Debian/Ubuntu systems.
  • To install the software properties-common-package on Debian/Ubuntu systems.
  • To install Ansible personal package archive on Debian/Ubuntu systems.
  • To install Ansible on Debian/Ubuntu systems.
  • To issue a ping command on all servers defined in the inventory file named hosts.
User
  • To issue a ping command only on hosts2.
  • To copy the file 'testfile' on all hosts in the inventory file.
  • To install ncdu package on all hosts.
  • To remove ncdu package on all hosts.
  • To build the directory structure for the role named role1.
  • To dry-run p4.yml playbook.
  • To run a p4.yml playbook with password authentication for all hosts.

An in-depth walkthough of building and running an Ansible-based operator.

NOTE: If your project was created with an operator-sdk version prior to v1.0.0please migrate, or consult the legacy docs.

Prerequisites

  • Go through the installation guide.
  • User authorized with cluster-admin permissions.
  • An accessible image registry for various operator images (ex. hub.docker.com,quay.io) and be logged in in your command line environment.
    • example.com is used as the registry Docker Hub namespace in these examples.Replace it with another value if using a different registry or namespace.
    • Authentication and certificates if the registry is private or uses a custom CA.

Overview

We will create a sample project to let you know how it works and this sample will:

  • Create a Memcached Deployment if it doesn’t exist
  • Ensure that the Deployment size is the same as specified by the Memcached CR spec
  • Update the Memcached CR status using the status writer with the names of the CR’s pods

Create a new project

Use the CLI to create a new memcached-operator project:

Among the files generated by this command is a Kubebuilder PROJECTfile. Subsequent operator-sdk commands (and help text) run from theproject root read this file and are aware that the project type isAnsible.

Next, we will create a Memcached API.

Ansible commands cheat sheet

The scaffolded operator has the following structure:

  • Memcached Custom Resource Definition, and a sample Memcached resource.
  • A “Manager” that reconciles the state of the cluster to the desired state
    • A reconciler, which is an Ansible Role or Playbook.
    • A watches.yaml file, which connects the Memcached resource to the memcached Ansible Role.

See scaffolded files reference and watches reference for more detailed information

Modify the Manager

Now we need to provide the reconcile logic, in the form of an AnsibleRole, which will run every time a Memcached resource is created,updated, or deleted.

Update roles/memcached/tasks/main.yml:

This memcached role will:

  • Ensure a memcached Deployment exists
  • Set the Deployment size

Note that the tasks in this Ansible role file are what actually defines the behavior of the spec and status of the memcached custom resource.As Kubernetes allows entry of arbitrary fields when creating resources, we don’t need to actually create specific fields in the CRD.While we won’t be doing this in this tutorial, it is recommended to also define these fields in the CRD, so that Kubernetes userscan see the fields that will be used when using the custom resource.It is also good practice to set default values for variables used in AnsibleRoles, so edit roles/memcached/defaults/main.yml:

Finally, update the Memcached sample, config/samples/cache_v1alpha1_memcached.yaml:

The key-value pairs in the Custom Resource spec are passedto Ansible as extra variables.

Note: The names of all variables in the spec field are converted tosnake_case by the operator before running ansible. For example,serviceAccount in the spec becomes service_account in ansible. You candisable this case conversion by setting the snakeCaseParameters optionto false in your watches.yaml. It is recommended that you perform sometype validation in Ansible on the variables to ensure that yourapplication is receiving expected input.

Configure the operator’s image registry

All that remains is to build and push the operator image to the desired image registry.Your Makefile composes image tags either from values written at project initialization or from the CLI.In particular, IMAGE_TAG_BASE lets you define a common image registry, namespace, and partial namefor all your image tags. Update this to another registry and/or namespace if the current value is incorrect.Afterwards you can update the IMG variable definition like so:

Once done, you do not have to set IMG or any other image variable in the CLI. The following command willbuild and push an operator image tagged as example.com/memcached-operator:v0.0.1 to Docker Hub:

Run the Operator

There are three ways to run the operator:

  • As Go program outside a cluster
  • As a Deployment inside a Kubernetes cluster
  • Managed by the Operator Lifecycle Manager (OLM) in bundle format

1. Run locally outside the cluster

Execute the following command, which install your CRDs and run the manager locally:

2. Run as a Deployment inside the cluster

By default, a new namespace is created with name <project-name>-system, ex. memcached-operator-system, and will be used for the deployment.

Command Line Cheat Sheet Pdf

Run the following to deploy the operator. This will also install the RBAC manifests from config/rbac.

Verify that the memcached-operator is up and running:

3. Deploy your Operator with OLM

First, install OLM:

Bundle your operator, then build and push the bundle image. The bundle target generates a bundlein the bundle directory containing manifests and metadata defining your operator.bundle-build and bundle-push build and push a bundle image defined by bundle.Dockerfile.

Finally, run your bundle. If your bundle image is hosted in a registry that is private and/orhas a custom CA, these configuration steps must be complete.

Check out the docs for a deep dive into operator-sdk's OLM integration.

Create a Memcached CR

Update the sample Memcached CR manifest at config/samples/cache_v1alpha1_memcached.yaml and define the spec as the following:

Create the CR:

Ensure that the memcached operator creates the deployment for the sample CR with the correct size:

Check the pods and CR status to confirm the status is updated with the memcached pod names:

Cheat

Update the size

Update config/samples/cache_v1alpha1_memcached.yaml to change the spec.size field in the Memcached CR from 3 to 5:

Confirm that the operator changes the deployment size:

Cleanup

Run the following to delete all deployed resources:

Next Steps

We recommend reading through the our Ansible development sectionfor tips and tricks, including how to run the operator locally.

In this tutorial, the scaffolded watches.yaml could be used as-is, buthas additional optional features. See watches reference.

Ansible Commands Cheat Sheet Examples

For brevity, some of the scaffolded files were left out of this guide.See Scaffolding Reference

This example built a namespaced scope operator, but Ansible operatorscan also be used with cluster-wide scope.

OLM will manage creation of most if not all resources required to run your operator, using a bit of setup from other operator-sdk commands. Check out the OLM integration guide.

Last modified April 12, 2021: `run bundle(-upgrade)`: configure registry pod with root certificate secret (#4703) (7e43b470)