Ansible: a configuration management and IT automation tool
Today I will talk about ansible, a powerful configuration management solution compiled by Python. Although there are already many configuration management solutions available on the market, they have their own advantages and disadvantages, and ansible is characterized by its conciseness. What distinguishes ansible from the mainstream configuration management systems is that it does not require you to install your own components on each node you want to configure. At the same time, it provides an advantage that, if needed, you can control your entire infrastructure in more than one place. The last point is its correctness. It may be controversial, but I think it can still be used as an advantage in most cases. Let's start installing and configuring Ansible in RHEL/CentOS and Debian/Ubuntu-based systems.
Ansible batch build LAMP Environment
Preparations
- Release: RHEL/CentOS/Debian/Ubuntu Linux
- Jinja2: a designer-friendly Modern template language for Python
- PyYAML: a yaml encoding/anti-encoding function library in Python
- Paramiko: SSHv2 protocol function library written in Python only)
- Httplib2: a fully functional HTTP client function library
- The vast majority of operations listed in this article have been assumed that you will execute them as root users in bash or any other modern shell.
How Ansible works
The Ansible tool does not use daemon, and does not require any additional custom security architecture. Therefore, it is easy to deploy it. All you need is the SSH client and server.
- + ----------------- ++ --------------- +
- | Ansible installed | SSH | File Server 1 |
- | Linux/Unix workstation | <----------------> | Database Server 2 | local or remote
- + ----------------- + Module | Proxy Server 3 | Data Center's
- 192.168.1.100 + --------------- + Unix/Linux Server
Where:
- 192.168.1.100-install Ansible on your local workstation or server.
- File Server 1 to Proxy Server 3-use 192.168.1.100 and Ansible to automatically manage all servers.
- SSH-set an SSH key between 192.168.1.100 and a local/remote server.
Ansible installation tutorial
Ansible is easy to install. Many third-party software repositories of Release versions have ready-made software packages that can be directly installed. Other simple installation methods include installing pip or getting the latest version from github. To use your package manager for installation, you may need an EPEL repository in a RHEL/CentOS-based Linux system.
Installing ansible in RHEL/CentOS Linux
Enter the following yum command:
- $ Sudo yum install ansible
Install ansible In Debian/Ubuntu Linux
Enter the following apt-get command:
- $ Sudo apt-get install software-properties-common
- $ Sudo apt-add-repository ppa: ansible/ansible
- $ Sudo apt-get update
- $ Sudo apt-get install ansible
Install ansible using pip
The pip command is a tool used to install and manage Python software packages. For example, it can manage those software packages in the Python Package Index. The following method is used in Linux and Unix-like systems:
- $ Sudo pip install ansible
Install the latest version of ansible from the source code
Run the following command to install the latest version from github:
- $ Cd ~
- $ Git clone git: // github.com/ansible/ansible.git
- $ Cd./ansible
- $ Source./hacking/env-setup
When you run ansible from a git checkout, remember that you need to set your environment every time you use it, or you can add this setting process to Your bash rc file:
- # Add BASH RC
- $ Echo "export ANSIBLE_HOSTS = ~ /Ansible_hosts ">> ~ /. Bashrc
- $ Echo "source ~ /Ansible/hacking/env-setup ">> ~ /. Bashrc
The hosts file of ansible includes a series of hosts that can be operated by it. By default, ansible searches for the hosts file through the path/etc/ansible/hosts, but this behavior can also be changed, in this way, it is convenient to operate more than one ansible or for different customers in different data centers. You can use the command line parameter-I to specify the hosts file:
- $ Ansible all-m shell-a "hostname" -- ask-pass-I/etc/some/other/dir/ansible_hosts
However, I prefer to use an environment variable, which can be used when you want to switch the work target through a different file of source. The environment variable here is $ ANSIBLE_HOSTS, which can be set as follows:
- $ Export ANSIBLE_HOSTS = ~ /Ansible_hosts
Once all the required components have been installed and you have prepared your hosts file, you can try it. For quick testing, I wrote 127.0.0.1 to the hosts file of ansible:
- $ Echo "127.0.0.1"> ~ /Ansible_hosts
Now let's test a simple ping:
- $ Ansible all-m ping
Or the ssh password is prompted:
- $ Ansible all-m ping -- ask-pass
I have encountered several issues in the initial setup. Therefore, we strongly recommend setting SSH Public Key Authentication for ansible. However, we used -- ask-pass in the test just now. On some machines, you need to install sshpass or specify-c paramiko as follows:
- $ Ansible all-m ping -- ask-pass-c paramiko
You can also install sshpass, but sshpass is not always provided in the standard repository, so paramiko may be simpler.
For more details, please continue to read the highlights on the next page: