1. Basic Introduction
Ansible is a configuration management and application deployment tool developed in Python, which is now also shining in the field of automation management. It integrates the advantages of many established operation and maintenance tools, and Ansible can basically achieve the functionalities that Pubbet and Saltstack can provide.
Pubbet and Chef are both written in Ruby and follow a C/S model, while Saltstack is written in Python and also follows a C/S model. (C/S model refers to Client/Server model)
Ansible operates without a client and accesses systems via SSH.
Therefore, Ansible can configure, deploy, and manage thousands of hosts in bulk. For example, operations that previously required switching to each host to execute one or more commands can now be completed on a single Ansible control node.
Ansible works based on modules; it only provides a running framework and does not have the capability to complete tasks by itself. The actual operations are executed by Ansible modules, such as the copy module for copying files to remote hosts, and the service module for managing the start, stop, and restart of services.
One of Ansible’s distinctive features is its agentless architecture, meaning it does not require an agent to be installed. It operates like a regular command and is not C/S software; Ansible only needs to be installed once on a host that acts as the control node, typically using SSH to control remote hosts without needing to install Ansible or any other additional services on the remote hosts.
When users execute commands or playbooks on the server terminal, Ansible will decompose the playbook into plays according to predefined rules, organize them into tasks recognizable by Ansible, invoke modules and plugins, and send temporary files to the remote clients via SSH based on the inventory, returning the results. After execution, these temporary files are automatically deleted.
Another notable feature of Ansible is that most of its modules are idempotent. Idempotence means that performing the same operation multiple times will have the same effect on system resources.
For example, executing the command systemctl stop xxx
to stop a service will do nothing if the target service is already stopped, so the result of stopping multiple times remains the same, which is stopped; it is idempotent, while systemctl restart xxx
is non-idempotent.
Many of Ansible’s modules will first check whether the target node needs to execute the task, so you can confidently let Ansible execute tasks, as repeating a task will most often not produce any side effects.
[root@k8s-master ~]# cat /etc/ansible/hosts
[local]192.168.3.88
[k8s]192.168.3.81
192.168.3.82
192.168.3.88
2. Features of Ansible
1. Simple deployment, requiring only the Ansible environment to be set up on the control side, with no operations needed on the managed side;2. Default management using SSH protocol;3. Centralized management of master and slave;4. Simple configuration, powerful functionality, and strong scalability;5. Supports APIs and custom modules, easily extensible through Python;6. Customizable powerful configuration and state management through playbooks;7. Good support for cloud computing platforms and big data;
3. Ansible Working Mechanism, Data Flow
4. Simple Commands
1. Test connectivity in bulk with Ansible
ansible k8s -i /etc/ansible/hosts -m ping
2. Bulk modify the hosts file content of three hosts to:
192.168.3.88 k8s-master
192.168.3.81 k8s-node1
192.168.3.82 k8s-node2
ansible k8s -i /etc/ansible/hosts -b -m lineinfile -a "path=/etc/hosts line='192.168.3.88 k8s-master' insertafter='EOF'"
ansible k8s -i /etc/ansible/hosts -b -m lineinfile -a "path=/etc/hosts line='192.168.3.81 k8s-node1' insertafter='EOF'"
ansible k8s -i /etc/ansible/hosts -b -m lineinfile -a "path=/etc/hosts line='192.168.3.82 k8s-node2' insertafter='EOF'"