Ansible (basic knowledge) for O & M Automation)

Source: Internet
Author: User
Tags ssh port aliyun ansible playbook
Ansible Development History

Ansible
?? Founder, Michael DeHaan (cobbler and func)
?? , Released version 0.0.1, Red Hat acquisition
?? Red Hat announced the acquisition
?? Ansible is an agentless (SSH key pair matching is required based on SSH), which can implement batch configuration, command execution and control, and Python-Based Automated O & M tools.
GitHub focus on similar automation tools)

Ansible features

1. modularization: Call specific modules to complete specific tasks
2. There are three key modules: paramiko, pyyaml, and jinja2 (template language ).
3. Support for custom modules
4. Python-based implementation
5. easy deployment, based on Python and SSH (installed by default), and agentless
6. Secure, based on OpenSSH
7. Support for playbook orchestration tasks
8. idempotence
9. No proxy dependency on PKI (no SSL required)
10. You can use any programming language to write a module.
11. Job Orchestration in yaml format, supporting a wide range of data structures
12. Powerful multi-layer Solutions

Ansible Architecture

How ansible works

Main Components of ansible

1. ansible playbooks:
?? The task script (Task Set) orchestrates and defines the configuration files of the ansible task set, which are executed sequentially by ansible, usually in the yml file in JSON format.
2. inventory:
?? Ansible manages the host list/etc/anaible/hosts.
3. modules:
?? Most of the functional modules used by ansible to execute commands are built-in core modules and can be customized.
4. plugins:
?? Module function supplement, such as connection type plug-ins, loop plug-ins, variable plug-ins, and filter plug-ins. This function is not commonly used.
5. API:
?? Application Programming Interface called by third-party programs
6. ansible:
?? The Green Box Combining inventory, API, modules, and plugins can be understood as ansible command tool, which is the core execution Tool
7. ansible Command Execution Source:
?? User, a common user, that is, System Administrator
?? CMDB (Configuration Management Database) API call
?? Public/private cloud API call
?? User-> ansible playbook-> ansibile
8. Use ansible for management:
?? Ad-hoc is the ansible command, which is mainly used for the use of temporary commands.
?? Ansible-playbook is mainly used for long-term planning, large-scale project scenarios, and requires a prerequisite for planning
9. ansible-playbook execution process:
?? Write an existing job set to ansible-playbook
?? Use the ansible-playbook command to split the task set to one ansible command, and execute the command one by one according to the predefined rules.
10. Main ansible operation objects:
?? Hosts host
?? Networking Network Device
11. Notes:
?? The host that executes ansible is generally called the master, central control, master, or bastion host.
?? The Python version of the master must be 2.6 or later.
?? Python-simplejson must be installed when the controlled Python version is earlier than 2.4.
?? If SELinux is enabled on the controlled end, libselinux-Python must be installed.
?? Windows cannot be the master

Ansible Installation

RPM package installation:
?? Ansible Based on epel Source
???? Centos6 :? Http://mirrors.aliyun.com/repo/epel-6.repo wget-O/etc/yum. Repos. d/epel. Repo
???? Centos7 :? Wget-O/etc/yum. Repos. d/epel. Repo http://mirrors.aliyun.com/repo/ epel-7.repo
???? Yum-y install ansible
Compile and install:
?? Yum-y install python-jinja2 pyyaml Python-paramiko Python-Babel Python-Crypto
?? Tar xf ansible-1.5.4.tar.gz
?? CD ansible-1.5.4
?? Python setup. py build
?? Python setup. py install
?? Mkdir/etc/ansible
?? CP-r examples/*/etc/ansible
Git mode:
?? Git clone git: // github.com/ansible/ansible.git -- Recursive
?? CD./ansible
?? Source./hacking/env-Setup
PIP installation: Pip is the manager for installing Python packages, similar to yum
?? Yum install Python-pip Python-devel
?? Yum install GCC glibc-devel zibl-devel rpm-bulid OpenSSL-devel
?? PIP install -- upgrade Pip
?? PIP install ansible -- upgrade
Confirm installation: ansible -- version

Ansible files

Configuration File
?? /Etc/ansible. cfg main configuration file to configure ansible features
?? /Etc/ansible/hosts host list
?? /Etc/ansible/roles/directory for storing roles
Program
?? /Usr/bin/ansible main program, temporary command execution Tool
?? /Usr/bin/ansible-Doc
?? /Usr/bin/ansible-Galaxy download/upload excellent code or official platform of the roles Module
?? /Usr/bin/ansible-playbook custom automation tasks, script orchestrating tools/usr/bin/ansible-pull remote command execution tools
?? /Usr/bin/ansible-vault file encryption tool
?? /Usr/bin/ansible-console execution tool that interacts with users based on the console interface

Ansible host list inventory

Inventory host list
?? Ansible is mainly used for batch host operations. To conveniently use some of the hosts, you can name them in inventory file.
The default inventory file is/etc/ansible/hosts.
Inventory files can be multiple, and can be dynamically generated through Dynamic Inventory
/Etc/ansible/hosts file format
The inventory File follows the INI file style, and the characters in brackets are group names. You can merge the same host into multiple different groups at the same time. In addition, if the target host uses a non-default ssh port, you can also use the colon and port number after the host name to indicate
???? Ntp.dklwj.com
???? [Webservers]
???? Www1.dklwj.com: 2222
???? Www2.dklwj.com
???? [Dbservers]
???? Db1.dklwj.com
???? Db2.dklwj.com
???? Db3.dklwj.com
If the host name follows a similar naming mode, you can also use the list to identify each host
Example:
?? [Websrvs]
?? WWW [0:100] .example.com

?? [Dbsrvs]
?? DB-[A: F] .example.com

Ansible configuration file

Ansible configuration file
?? /Etc/ansible. cfg (usually keep the default value)

[Defaults] # inventory =/etc/ansible/hosts # host list configuration file # library =/usr/share/my_modules/# library file storage directory # remote_tmp = $ home /. ansible/tmp # The temporary py command file is stored in the remote host directory # local_tmp = $ home /. ansible/tmp # local temporary command execution directory # forks = 5 # default concurrency # sudo_user = root # default sudo user # ask_sudo_pass = true # Whether to ask the SSH password for each execution of the ansible command # ask_pass = true # remote_port = 22 # host_key_checking = false # Check the host_key of the corresponding server, we recommend that you uncomment # log_path =/var/log/ansible. log # log files

Ansible commands

ansible ansible-doc ansible-playbook ansible-vaultansible-console ansible-galaxy ansible-pull

Ansible-Doc: Display Module help
?? Ansible-Doc [Options] [module...]
?? -A: displays documents of all modules.
?? -L, -- list available modules
?? -S, -- snippet: Display The Playbook fragment of the specified Module
Example:
?? Ansible-doc-l ??? List all modules
?? Ansible-Doc Ping ?? View the help usage of a specified Module
?? Ansible-doc-s Ping? View the help usage of a specified Module

Ansible (basic knowledge) for O & M Automation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.