ansible Introduction
- Ansible can also manage the Linux of the Linux,debian system of the Redhat system, as well as the Windows host. The management node only connects to the remote host when the script is executed, and there is no special synchronization mechanism, so exceptions such as power outages generally do not affect ansbile.
2.ansible is a new automated operations tool, based on the development of Python, the collection of many operations tools (puppet, Cfengine, Chef, func, fabric) The advantages of the implementation of batch system configuration, batch program deployment, batch Run command and other functions. Ansible is a module-based operation and does not have the capacity to deploy in bulk. The real batch deployment is the module that Ansible runs, and Ansible just provides a framework. Mainly include:
? ?? ??? (1), connection plug-in connection plugins: responsible for and be monitored to achieve communication;
? ?? ??? (2), host Inventory: Specifies the operation of the host, is a configuration file inside the definition of monitoring host;
? ?? ??? (3), various modules core module, command module, custom module;
? ?? ??? (4), with the help of the plug-in to complete log mail and other functions;
? ?? ??? (5), playbook: When a script performs multiple tasks, it is not necessary to allow the node to run multiple tasks at once.
- 3.ansible Architecture: Connect to other hosts by default using SSH protocol
Introduction to the experimental environment:
Host name |
IP Address |
Operating System |
Ansible Management Side |
192.168.195.147 |
CentOS7 |
Managed by side 1 |
192.168.195.162 |
CentOS7 |
Managed by Side 2 |
192.168.195.163 |
CentOS7 |
Installation deployment
First install ansible on the management host
Yum install-y epel-release//installation Epel source
Yum Install Ansible-y
Ansible--version//view ansible version
Yum Install Tree-y
tree/etc/ansible///tree Structure display folder
/etc/ansible/
├──ansible.cfg #ansible的配置文件
├──hosts #ansible的主仓库 for storing information about remote hosts that need to be managed
└──roles #角色
Modify the Ansible configuration file hosts to add a managed host inventory
Cd/etc/ansible
VI hosts//configure host Inventory
[Webserver]
192.168.195.162
[MySQL]
192.168.195.163
Set the SSH key pair and push the public key to the managed host
ssh-keygen-t RSA #生成密钥
Ssh-copy-id [email protected]
Ssh-copy-id [email protected]//configure key pair verification
----------Free Interactive proxy--------------
Ssh-agent Bash
Ssh-add
Ansible Command-line module
1,------Command Module------
Command format: ansible [Host] [-M module] [-a args]
Ansible-doc-l//List all installed modules Note: Press Q to exit
Ansible-doc-s yum//-s lists Yum module description information and action actions
Ansible 192.168.195.162-m command-a ' date '//specified IP execution date
Ansible webserver-m command-a ' Date '//specify classification to perform date
Ansible mysql-m command-a ' Date '
Ansible all-m command-a ' Date '//All hosts hosts execute date command
Ansible all-a ' ls/' if the-M module is not added, the command module is run by default
2,-----cron module------
Two states: Present means add (can be omitted), absent means remove.
Ansible-doc-s cron//view cron module information
Ansible webserver-m cron-a ' minute= "/1" job= "/bin/echo Heihei" name= "Test cron Job"
Ansible webserver-a ' crontab-l '
Ansible webserver-m cron-a ' name= ' Test cron job ' state=absent '//Remove Scheduled Tasks, if the scheduled task does not have a name, Name=none can
3,-----User module------
The user module is requested by Useradd, Userdel, usermod three instructions
Ansible-doc-s User//View usage of user module
Ansible mysql-m user-a ' name= "test01" '//create user test01
Ansible mysql-m command-a ' tail/etc/passwd '
Ansible mysql-m user-a ' name= "test01" state=absent '//delete user test01
4,-----Group Module-----
The group module requested Groupadd, Groupdel, groupmod three instructions.
Ansible-doc-s Group//View the use of the group module
Ansible mysql-m group-a ' name=mysql gid=306 system=yes '//create group for MySQL
Ansible mysql-a ' tail/etc/group '//view newly created group MySQL
Ansible mysql-m user-a ' name=test01 uid=306 system=yes group=mysql '//create test01 user, and specify the group as MySQL,
Ansible mysql-a ' tail/etc/passwd '//view created user test01
Ansible mysql-a ' id test01 '//view user test01 Information
5,------copy module--------
Ansible-doc-s Copy
Ansible mysql-m copy-a ' src=/etc/fstab dest=/opt/fstab.back owner=root mode=640 '//File copy, copy Fstab to/ Opt under the name Fstab.back, and set the permission to 640, specify the owner as root
Ansible mysql-a ' ls-l/opt '//view files under/opt directory, whether there are new files generated
Ansible mysql-a ' cat/opt/fstab.back '//view copied file contents
Ansible mysql-m copy-a ' content= "Hello heihei!" dest=/opt/fstab.back '//write Hello heihei! to/opt/fstab.back also create new file
Ansible mysql-a ' cat/opt/fstab.back '//view its contents again, occurrence substitution
6,------the file module--------
Ansible-doc-s file
First create a test user MySQL and join the MySQL group
Ansible mysql-m user-a ' Name=mysql system=yes '
Ansible mysql-m group-a ' Name=mysql system=yes '
Ansible mysql-m file-a ' owner=mysql group=mysql mode=644 path=/opt/fstab.back '//modify file's owner group permissions, etc.
Ansible mysql-m file-a ' path=/opt/fstab.link src=/opt/fstab.back state=link '//set/opt/fstab.link to/opt/ Fstab.back's link file
Ansible mysql-m file-a "Path=/opt/fstab.back state=absent"//delete a file
Ansible mysql-m file-a "Path=/opt/test state=touch" Create a file
7,-----ping module-------
Ansible all-m Ping//test managed host is online
8,-----Yum Module-----
Ansible-doc-s Yum
Ansible mysql-m yum-a ' name=httpd '//yum installation httpd
[Email protected] ~]# rpm-q httpd
Ansible mysql-m yum-a ' name=httpd state=absent '//Uninstall HTTPD
9,-----Service Module--------
Ansible-doc-s Service
[Email protected] ~]# yum-y install httpd
Ansible webserver-m service-a ' enabled=true name=httpd state=started '//Start httpd service
[[email protected] ~]# systemctl status httpd//view webserver on httpd open
------Shell Module-----
Ansible-doc-s Shell
Ansible mysql-m shell-a ' echo abc123|passwd--stdin mysql '//create user to set password for user using no interactive mode
One,------script module---------
Ansible-doc-s script//Edit a simple shell script on the ansible management side
VI test.sh
#!/bin/bash
echo "Hello ansible from Script" >/opt/script.txt
chmod +x test.sh//Give Execute permission
Ansible mysql-m script-a ' test.sh '//Use the script module to execute scripts on MySQL
[[email protected] ~]# cat/opt/script.txt//View script execution results on MySQL
-----The Setup module-------
Ansible-doc-s Setup
Ansible mysql-m setup//Get facts information for the MySQL group host
Ansible as an automated operations tool there are many modules, this article only introduces some common modules.
Automatic operation Koriyuki ansible installation and Basic module application