Ansible installation and configuration (1)

The company has about 200 cloud hosts that need to be managed, but if manual management is time-consuming and tiring, the final result is also prone to errors, so consider managing cloud hosts through automation. At present, the open source automation tools are mostly used by Ansible and Ansible. There are two types of Saltstack, but there are other ones that are not as common as these two. The first one I came into contact with was ansible, so the company uses ansible to manage hosts. Let's introduce the installation and configuration of ansible.

1. Preparation of the environment

  1.Python2.6+: Ansible is developed based on Python, so we need to install Python2.6 or above to control the host;

  2. Jinja2: Jinja2 is used when defining templates, and Jinja2 is Python's modern template language;

  3. PyYAML: The file type defined when writing Playbook is ymal, and PyYAML is a YAML encoding/decoding function library for Python;

  4.paramiko: Ansible is based on SSH protocol communication by default, while paramiko is a SSHv2 protocol function library written in pure Python;

  5.httplib2: a full-featured HTTP client library;

2. Ansible installation

 Ansible is based on SSH protocol communication by default. After installing Ansible, the control host does not need to start or run any Ansible in the background. You only need to install Ansible on the control host, so that any controlled node can be managed through Ansible, and the controlled node does not need to be installed. any client software;

  1. Source installation

# git clone git://github.com/ansible/ansible.git --recursive
# cd ./ansible
# source ./hacking/env-setup

  2.yum/apt installation

yum install ansible
apt-get install ansible

  3.pip install

pip install ansible   

  4. Check if ansible is installed successfully

ansible --version

 Returns ansbile version information

ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]

  5. Ansible command set

ansible # define ansible single task
ansible - config # View, edit, manage Ansible configuration
ansible - doc # Documentation viewing tool
ansible - galaxy # tools for sharing and downloading roles
ansible - inventory # View inventory host information
ansible-playbook           # 执行playbook
ansible - pull # Pull the playbook from the repository
ansible - vault # file encryption and decryption tool
ansible -console # ansible console

3. Configure the operating environment

  1. Configure the Ansible environment

  When running an Ansible command, the command will look for configuration files in a pre-set order

  I. First, Ansible will check whether the environment variable is set to ANSIBLE_CONFIG, and check the path to the file that the variable points to;
  II. Check whether the ansible.cfg configuration file is included in the current directory;
  III. Check again whether the current user's home directory contains the ansible.cfg configuration file;
  IV. Finally, check whether the ansible.cfg configuration file is included in the /etc/ansible directory. Generally, the configuration file will be automatically generated in the etc directory when the software management package (yum/apt) is installed;

Notice:

  When installing through pip, the completed ansible.cfg configuration file will not be generated. At this time, we need to copy the ansible.cfg configuration file from the examples directory in the github repository;

  2. Ansible main configuration file

1 /etc/ansible/ ansible.cfg #Ansible configuration file
 2 /etc/ansible/ hosts #host inventory
 3 /etc/ansible/roles/ #role path

 四、Ansible Inventory

  In the actual process, we need to manage various server resources in different businesses and different environments. The information of these servers is mainly stored in the Inventory component. The path /etc/ansible/hosts of the configuration file is defined by default in ansible.cfg;

  1. Define hosts and host groups

   Let's define hosts and host groups in the /etc/ansible/hosts file

# Ex 1 : define the host

    192.168.100.10

# Ex 2 : Define the ' WebServers ' host group

[WebServers]
    10.172 . 139.53 
    10.30 . 49.72 
    10.30 . 49 .[ 1 : 10 ] # A set of shorthand patterns for similar IP addresses

   illustrate:

    The group name indicated in square brackets [] is mainly used to classify different types of systems, so as to facilitate the management of server resources of the same type;

  2. Host and host group variables

# define host variables

    10.30.49.72 ansible_port   = ' 61821 ' #define    ssh remote port

# define host group variables
# The following variables belong to the entire WebServers group
[WebServers: Fresh]  
    ansible_ssh_pass='ansible'
    ansible_ssh_port='61821'

  3. Inventory common parameter description

ansible_ssh_host # The remote hostname to connect to.

ansible_ssh_port # SSH remote connection port, non-standard port setting.

ansible_ssh_user # SSH remote connection username

ansible_ssh_pass # SSH remote connection account corresponding password (this method is easy to leak password information, it is recommended to use --ask- pass or SSH authentication)

ansible_sudo_pass # sudo user password (this method is easy to leak password information, it is strongly recommended to use --ask-sudo- pass)

5. Write the first Ansible program

  After Ansible is installed, we have a basic understanding of how Ansible is configured, and let's write the first Ansible program.

Example 1: Test WebServers server group communication
ansible   WebServers -m ping -o        Host / Host Group Module
Return result:   
39.105.0.244 | SUCCESS => {"changed": false, "ping": "pong"}   47.104.149.180 | SUCCESS => {"changed": false, "ping": "pong"}
示例2:WebServers
/opt 目录下创建ansible.txt文件
  ansible
47.104.149.180 -m file -a "owner=root group=root mode=644 path=/opt/ansible.txt state=touch" -o        Host / Host Group Module Parameters  Return result:   47.104.149.180 | SUCCESS => {"changed": true, "dest": "/opt/ansible.txt", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0}

 Through the above examples, we have a general understanding of how to manage servers through Ansible commands. The specific module information and parameters will be introduced later. Here we just want to experience the convenience of Ansible, so that we have completed the installation and configuration of Ansible.

  

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768220&siteId=291194637