[PYTHON] Two Ways to Make Ansible Portable

Introduction

In order to manage server configuration with Ansible, it is necessary to build an environment on a machine that can ssh the configuration target. However, there may be cases like this

--Cannot connect to the Internet --You can't install new software without permission

In such a case, you may want to use Ansible without installation. In other words, I want to do the following.

                  |→ Product environment
                  |★ This environment cannot be tampered with. ..
┌─────────────┐   |   ┌─────────────┐       ┌───────────────┐
|1           |  |   | 2          |      | 3            |
|            |──────|            |──────|              |
|            |  |   |            |ssh   |              |
└─────────────┘   |   └─────────────┘       └───────────────┘
Local machine|Ansible server Configuration target server

→ I want to use ansible just by transferring the directory to the Ansible server!

so, After gonyogonyo on the local machine, I will introduce two ways to make Ansible executable just by transferring the directory.

1. How to download the source from git

Many of you may know this as it is also mentioned in the Ansible documentation.

Procedure on local machine

First, git clone

git clone https://github.com/ansible/ansible.git --recursive

Ansible directory is created like this.

$ ls
ansible

Update the submodule.

cd ansible
git submodule update --init lib/ansible/modules/core
git submodule update --init lib/ansible/modules/extras
cd .. #Go back to the original directory

Now you are ready to go. ʻTransfer the ansible` directory (and playbook) to the Ansible server.

scp -r ansible [Ansible server]:

How to use Ansible server side

cd ansible
source hacking/env-setup

Now you can execute the ansible command.

Conditions under which this method can be used

It doesn't seem to work unless the following conditions are met.

--Python2.6 or Python2.7 is already installed --pip paramiko PyYAML Jinja2 httplib2 six has been installed

The second point seems to be a little high hurdle. ..

2. Transfer for each PyEnv environment

It is a somewhat brute force method to build an environment by transferring the entire directory created by PyEnv.

Procedure on local machine

First, create a directory to execute Ansible with the same path as the Ansible server side.

Example: If the user name of the Ansible server is opuser and the Ansible execution directory ʻansible is placed in / home / opuser`

# useradd opuser
# su - opuser
$ echo $HOME  # => /home/opuser
$ mkdir -p /home/opuser/ansible

Build the environment of Pyenv in the directory for Ansible.

cd /home/opuser/ansible
git clone https://github.com/yyuu/pyenv.git pyenv

touch ./pyenv.env
echo 'export PYENV_ROOT="/home/opuser/ansible/pyenv"' >> ./pyenv.env
echo 'if [ -d "${PYENV_ROOT}" ]; then' >> ./pyenv.env
echo '    export PATH=${PYENV_ROOT}/bin:$PATH' >> ./pyenv.env
echo '    eval "$(pyenv init -)"' >> ./pyenv.env
echo 'fi' >> ./pyenv.env

source ./pyenv.env

cd $PYENV_ROOT/plugins
git clone https://github.com/yyuu/pyenv-virtualenv.git

Install Python in the Pyenv environment. Then install Ansible. If there are other packages you want to install, install them.

cd /home/opuser/ansible
pyenv install 2.7.12
pyenv local 2.7.12
pip install ansible

Transfer the ansible directory.

scp -r /home/opuser/ansible opuser@[Ansible server]:

How to use Ansible server side

Read the environment variables of Pyenv.

cd ~/ansible
source pyenv.env
ansible --version #=>Ansible should be available.

The operating conditions are as follows

--The CPU archi is the same on the local machine and the Ansible server

Summary

Assuming that you want to use Ansible in a product environment where you want to minimize the impact of changes, I introduced how to create a portable Ansible execution environment.

Recommended Posts

Two Ways to Make Ansible Portable
Two Ways to Decode Binary Vector Tiles to GeoJSON
[Small story] Two ways to correctly deep merge Ansible and nested dict variables
API implementation to toggle values in two ways (go)
I tried two ways to combine multiple commits in Git
Easy to make with syntax
Introduction to Ansible Part ③'Inventory'
2 ways to deal with SessionNotCreatedException
Introduction to Ansible Part ④'Variable'
Two ways to display multiple graphs in one image with matplotlib