I. Basic Knowledge:
1. Introduction
Ansible is developed based on Python and combines the advantages of many O & M tools to implement features such as batch system configuration, batch program deployment, and batch run commands. Ansible works based on modules and does not support batch deployment.
The ansible module is actually deployed in batches, and ansible is just a framework
(1) connection plugins: communicates with the monitored end;
(2) host inventory: Specifies the host for the operation. It is a host defined in the configuration file.
(3) core modules, command modules, and custom modules of various modules;
(4) logging email and other functions are completed by using the plug-in;
(5) playbook: When the script executes multiple tasks, the node can run multiple tasks at a time.
2. features:
(1) No agents: you do not need to install the task agent on the managed host.
(2) No server: No server. Run the command directly when using it.
(3) modules in any languages ages: modules can be developed in any language.
(4) yaml not code: Use the yaml language to customize the script playbook
(5) SSH by default: Work Based on SSH
(6) strong multi-tier solution: multi-level command can be implemented
3. Advantages:
(1) Lightweight. You do not need to install the agent on the client. when updating the agent, you only need to update the agent on the operating machine;
(2) Batch Tasks can be written as scripts and executed without being distributed remotely.
(3) easy to maintain using Python
(4) Support for sudo
2. Install ansible
1.1 RPM package installation
Epel Source:
[epel] name=Extra Packages for Enterprise Linux 6 - $basearch baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch failovermethod=priority enabled=1 gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 [epel-debuginfo] name=Extra Packages for Enterprise Linux 6 - $basearch - Debug baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch/debug #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-6&arch=$basearch failovermethod=priority enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 gpgcheck=0 [epel-source] name=Extra Packages for Enterprise Linux 6 - $basearch - Source baseurl=http://download.fedoraproject.org/pub/epel/6/SRPMS #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-6&arch=$basearch failovermethod=priority enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 gpgcheck=0 [root@localhost ~]# yum install ansible -y
Iii. Introduction to common modules
Copy Module
Objective: To copy the local master file to a remote node
[[email protected] ~]# ansible 192.168.118.14 -m copy -a "src=/root/bigfile dest=/tmp" 192.168.118.14 | SUCCESS => { "changed": true, "checksum": "8c206a1a87599f532ce68675536f0b1546900d7a", "dest": "/tmp/bigfile", "gid": 0, "group": "root", "md5sum": "f1c9645dbc14efddc7d8a322685f26eb", "mode": "0644", "owner": "root", "size": 10485760, "src": "/root/.ansible/tmp/ansible-tmp-1467946691.02-193284383894106/source", "state": "file", "uid": 0 }
File Module
Purpose: To modify the permissions, owner, and group of files on a specified Node
[[email protected] ~]# ansible 192.168.118.14 -m file -a "dest=/tmp/bigfile mode=777 owner=root group=root" 192.168.118.14 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0777", "owner": "root", "path": "/tmp/bigfile", "size": 10485760, "state": "file", "uid": 0 }
Cron Module
Purpose: define a scheduled task on a specified node, which is executed every three minutes.
[[email protected] ~]# ansible all -m cron -a ‘name="Cron job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/bin/ntpdate tiger.sina.com.cn"‘ 192.168.118.14 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "Cron job" ] } 192.168.118.13 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "Cron job" ] }
Group Module
Objective: to create a group named ansible and GID 2016 on a remote node
[[email protected] ~]# ansible 192.168.118.14 -m group -a "name=ansible gid=2016" 192.168.118.14 | SUCCESS => { "changed": true, "gid": 2016, "name": "ansible", "state": "present", "system": false }
User Module
Purpose: To create an ansible user group on a specified node.
[[email protected] ~]# ansible 192.168.118.14 -m user -a "name=ansible uid=2016 group=ansible state=present" 192.168.118.14 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 2016, "home": "/home/ansible", "name": "ansible", "shell": "/bin/bash", "state": "present", "system": false, "uid": 2016 }
Delete a remote node user. Note: deleting a remote user does not delete the user's home directory.
[[email protected] ~]# ansible 192.168.118.14 -m user -a "name=ansible state=absent" 192.168.118.14 | SUCCESS => { "changed": true, "force": false, "name": "ansible", "remove": false, "state": "absent" }
Yum Module
Objective: To install vsftpd on a remote node
[[email protected] ~]# ansible 192.168.118.14 -m yum -a ‘name=vsftpd state=present‘ 192.168.118.14 | SUCCESS => { "changed": true, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror\nSetting up Install Process\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package vsftpd.x86_64 0:2.2.2-14.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n vsftpd x86_64 2.2.2-14.el6 yum 152 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package(s)\n\nTotal download size: 152 k\nInstalled size: 332 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Installing : vsftpd-2.2.2-14.el6.x86_64 1/1 \n\r Verifying : vsftpd-2.2.2-14.el6.x86_64 1/1 \n\nInstalled:\n vsftpd.x86_64 0:2.2.2-14.el6 \n\nComplete!\n" ] }
Uninstall statement:
[[email protected] ~]# ansible 192.168.118.14 -m yum -a ‘name=vsftpd state=removed‘ 192.168.118.14 | SUCCESS => { "changed": true, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror\nSetting up Remove Process\nResolving Dependencies\n--> Running transaction check\n---> Package vsftpd.x86_64 0:2.2.2-14.el6 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n vsftpd x86_64 2.2.2-14.el6 @yum 332 k\n\nTransaction Summary\n================================================================================\nRemove 1 Package(s)\n\nInstalled size: 332 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Erasing : vsftpd-2.2.2-14.el6.x86_64 1/1 \n\r Verifying : vsftpd-2.2.2-14.el6.x86_64 1/1 \n\nRemoved:\n vsftpd.x86_64 0:2.2.2-14.el6 \n\nComplete!\n" ] }
Service Module
Start [email protected] ~] # Ansible 192.168.118.14-M Service-a 'name = vsftpd state = started enabled = Yes '192.168.118.14 | Success => {"changed": True, "enabled": True, "name": "vsftpd", "State": "started"} stop [email protected] ~] # Ansible 192.168.118.14-M Service-a 'name = vsftpd state = stopped enabled = Yes '192.168.118.14 | Success => {"changed": True, "enabled": True, "name": "vsftpd", "State": "STOPPED "}
Ping Module
[[email protected] ~]# ansible 192.168.118.14 -m ping 192.168.118.14 | SUCCESS => { "changed": false, "ping": "pong" }
Command Module
[[Email protected] ~] # Ansible 192.168.118.14 [-M command]-A 'W' #-M command can be omitted to use the naming module 192.168.118.14 | Success | rc = 0> 14:00:32 up, 2 users, load average: 0.00, 0.00, 0.00 user tty from [email protected] idle jcpu pcpu what root pts/0 192.168.118.69 0.12 s 0.12 s-bash root pts/1 192.168.118.13 0.00 s 0.04 s 0.00 S/bin/ sh-C Lang
Raw Module
The main purpose is to add pipeline symbols to the command.
[[email protected] ~]# ansible 192.168.118.14 -m raw -a ‘hostname | tee‘ 192.168.118.14 | SUCCESS | rc=0 >> localhost.localdomain
Get_url Module
Objective: To download http: // 192.168.118.14/1.png to a local device
[[email protected] ~]# ansible 192.168.118.14 -m get_url -a ‘url=http://192.168.118.14/1.png dest=/tmp‘ 192.168.118.14 | SUCCESS => { "changed": true, "checksum_dest": null, "checksum_src": "ba5cb18463ecfa13cdc0b611c9c10875275d883e", "dest": "/tmp/1.png", "gid": 0, "group": "root", "md5sum": "8c0df0b008eb5735dc955171d6d9dd73", "mode": "0644", "msg": "OK (14987 bytes)", "owner": "root", "size": 14987, "src": "/tmp/tmpY2lqHF", "state": "file", "uid": 0, "url": "http://192.168.118.14/1.png" }
Synchronize Module
Purpose: To push the main empty directory to the specified node/tmp directory
[[email protected] ~]# ansible 192.168.118.14 -m synchronize -a ‘src=/root/test dest=/tmp/ compress=yes‘ 192.168.118.14 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh ‘ssh -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/test\" \"192.168.118.14:/tmp/\"", "msg": ".d..t...... test/\n<f+++++++++ test/abc\n", "rc": 0, "stdout_lines": [ ".d..t...... test/", "<f+++++++++ test/abc" ] }
Source: https://www.cnblogs.com/hukey/p/5660538.html
Introduction to ansible