[PYTHON] [Ansible installation procedure] From installation to execution of playbook

What you can achieve with this post

--Create Controller / Target node by node in Virtualbox --The range is from the controller to the Target by sending ansible playbook to the initial construction of the Target. --Change host name --Package installation (vim, git) --Ansible is installed as a module of virtualenv

Construction environment

Common

Constitution

What is ansible?

Overview / Features

--Python configuration management automation tool --In the form of defining the termination condition, it is characterized by idempotent guarantee (some commands need to be guaranteed by themselves) --Highly readable in YAML format playbook --Since there is no agent, the introduction cost is low (it is OK if SSH and python are included in the management target) ――It goes well with the use of self-made scripts that have already been created. ――Module development / role development at 3rd Party is active, and most of what I want to do is in Documents / ansible-galaxy. - modules : http://docs.ansible.com/ansible/modules.html - roles : https://galaxy.ansible.com/ --Push type configuration management is common -Convert YAML format playbook to python and distribute / execute to managed target

ansible term

Term Description
Inventory Specify Target Node. INI format
Playbook Describe the flow of processing you want to execute on Target Node. YAML format

Environment

Controller / Target: Create general user (ansible)

Created by general users


useradd -s /bin/bash -m ansible
passwd ansible
#Grant sudo privileges
echo "ansible ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/ansible

Controller: Public key authentication settings

su - ansible
cd ~
ssh-keygen
ssh-copy-id -i .ssh/id_rsa.pub 192.168.56.152 (Target Node IP address)

Controller: Install required packages

Install command


su - ansible
sudo yum -y install epel-release
sudo yum -y install gcc libffi-devel python-devel openssl-devel python-pip
sudo pip install --upgrade pip

#Not required if virtualenv is not used
sudo pip install virtualenv
#Not required if virtualenv is not used
cd ~
virtualenv venv
source venv/bin/activate

pip install "ansible==2.2"

Controller: Create working directory

Creating a working directory


su - ansible
cd ~
mkdir -p practice/{inventory,{group,host}_vars,roles,playbooks}
touch practice/{inventory/inventory.ini,group_vars/all.yml}

Controller: Creating an Inventory file

Creating an Inventory file


su - ansible
cd ~/practice
vim inventory/inventory.ini

inventory/inventory.ini


[practice_servers]
practice01 ansible_host=192.168.56.152 #Target Node 

Controller: Ansible communication confirmation

Communication confirmation


#Confirm that the server information of Target Node is returned as Response
ansible practice_servers -i inventory/inventory.ini -m setup

Controller: Playbook creation

Playbook creation


su - ansible
cd ~/practice
vim playbooks/initial_setting.yml

~/practice/playbooks/initial_setting.yml


---
- hosts: practice_servers
  become: yes #Implement sudo on Target server
  become_user: root #Performed on behalf of the root user

  tasks:
    - name: 1. Set hostname
      hostname:
        name: practice01

    - name: 2. Install required packages
      yum:
        name: "{{ item }}"
      with_items: 
        - vim
        - git

Run playbook

Run playbook


su - ansible
cd ~/practice
ansible-playbook -i inventory/inventory.ini playbooks/initial_setting.yml

reference

Vagrantfile used for construction

Controller


Vagrant.configure("2") do |config|
  # Basic VM settings
  config.vm.box = "CentOS7.0"
  config.vm.box_download_insecure = true
  config.vm.hostname = 'controller'
  config.vm.network "private_network",ip:"192.168.56.151"
	config.vm.network :public_network, bridge: 'en0: Wi-Fi (AirPort)'
  config.vm.network :forwarded_port, id: "ssh", guest: 22, host: 2151
	config.ssh.insert_key = false

  # Virtual box setting
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--ostype", "Redhat_64"]
    vb.name = "ansible_controller"
    vb.memory = 512
    vb.cpus = 1
  end
end

Target_Node


Vagrant.configure("2") do |config|
  # Basic VM settings
  config.vm.box = "CentOS7.0"
  config.vm.box_download_insecure = true
  config.vm.hostname = 'practice'
  config.vm.network "private_network",ip:"192.168.56.152"
	config.vm.network :public_network, bridge: 'en0: Wi-Fi (AirPort)'
  config.vm.network :forwarded_port, id: "ssh", guest: 22, host: 2152
	config.ssh.insert_key = false

  # Virtual box setting
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--ostype", "Redhat_64"]
    vb.name = "practice"
    vb.memory = 512
    vb.cpus = 1
  end
end

Recommended Posts

[Ansible installation procedure] From installation to execution of playbook
Introduction to Scapy ① (From installation to execution of Scapy)
From editing to execution
From the introduction of pyethapp to the execution of contract
Now "average of sums from 1 to 10" and its execution speed
Explanation from installation of Sphinx to use of external theme (Bootswatch)
Procedure from HTML to JSON Ajax communication of API server
From Elasticsearch installation to data entry
OpenMPI installation from download to pass-through
Python (from first time to execution)
PySpark 1.5.2 + Elasticsearch 2.1.0 Installation procedure and execution
Getting Started with Poetry From installation to execution and version control
Summary of Oracle Database XE installation procedure
[Note] [PyTorch] From installation to easy usage
From MuJoCo license acquisition, renewal to execution
Procedure from AWS CDK (Python) development to AWS resource construction * For beginners of development
From Attention of Zero Tsuku 2 to Transformer
Flask tutorial (from installation to hello world)
Summary of vtkThreshold (updated from time to time)
[2021 version] From Selenium Basic installation to Web scraping execution by Python Windows 10 (64bit)
Summary of gcc options (updated from time to time)
Setting to output the log of cron execution
Change the decimal point of logging from, to.
How to operate Linux from the outside Procedure
From ROS for Windows installation to operation check
Procedure from uninstalling pyenv to installing again (anaconda)
From easy git installation to docker startup python
Procedure to set hydrogen of atom (virtual environment)
The story of moving from Pipenv to Poetry
Pylearn 2 installation procedure
[Ansible] Example of playbook that adds a character string to the first line of the file
Sum from 1 to 10
PostgreSQL 10.0 installation procedure
blockdiag installation procedure
The wall of changing the Django service from Python 2.7 to Python 3
I want to grep the execution result of strace
Installation of TensorFlow, a machine learning library from Google
[Updated from time to time] Review of Let Code NumPy
RaspberryPi3 (STRETCH) setup from OS installation to Hello World
Execution procedure of TensorFlow in batch processing of supercomputer ITO
Python development environment construction 2020 [From Python installation to poetry introduction]
DataNitro, implementation of function to read data from sheet
[Introduction to cx_Oracle] (Part 11) Basics of PL / SQL Execution
Procedure to exe python file from Ubunts environment construction
Convert elements of numpy array from float to int
[Python] Chapter 01-02 About Python (Execution and installation of development environment)
Procedure from environment construction to operation test of testinfra, a server environment test tool made by Python