First, the basic introduction
==========================================================================================
Ansible is a new automated operation tools, based on Python development, the collection of a number of 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 monitored by the end to achieve communication
(2), host Inventory: Specifies the operation of the hosts, 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: script when performing multiple tasks, it is not necessary to allow the node to run multiple tasks at once
1, ansible Basic installation
(1), python2.7 installation #目前还不支持3X的版本, you can also use
Https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgztar xvzf PYTHON-2.7.8.TGZCD Python-2.7.8./configure-- Prefix=/usr/localmake && make Install
Copy the Python header file to a standard directory to avoid the need for header files when compiling ansible
Cd/usr/local/include/python2.7cp-a./*/usr/local/include/
Back up old versions of Python and sign links to new versions of Python
CD/USR/BINMV python python.oldln-s/usr/local/bin/python.
Modify the Yum script to point to the old version of Python and avoid it from running
Vim/usr/bin/yum modified to #!/usr/bin/python.old
(2), Setuptools module installation
Https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.2.tar.gztar Xvzf SETUPTOOLS-19.6.2.TAR.GZCD Setuptools-19.6.2python setup.py Install
(3), Pycrypto module installation
Https://pypi.python.org/packages/source/p/pycrypto/pycrypto-2.6.1.tar.gztar Xvzf PYCRYPTO-2.6.1.TAR.GZCD Pycrypto-2.6.1python setup.py Install
(4), Pyyaml module installation
Http://pyyaml.org/download/libyaml/yaml-0.1.6.tar.gztar xvzf YAML-0.1.6.TAR.GZCD yaml-0.1.6./configure--prefix=/ Usr/localmake && make Installhttps://pypi.python.org/packages/source/p/pyyaml/pyyaml-3.11.tar.gztar zxvf PYYAML-3.11.TAR.GZCD Pyyaml-3.11python setup.py Install
(5), JINJA2 module installation
Https://pypi.python.org/packages/source/M/MarkupSafe/MarkupSafe-0.9.3.tar.gztar Xvzf MARKUPSAFE-0.9.3.TAR.GZCD Markupsafe-0.9.3python setup.py Installhttps://pypi.python.org/packages/source/j/jinja2/jinja2-2.8.tar.gztar Xvzf JINJA2-2.7.3.TAR.GZCD Jinja2-2.7.3python setup.py Install
(7), Simplejson module installation
Https://pypi.python.org/packages/source/s/simplejson/simplejson-3.8.1.tar.gztar Xvzf SIMPLEJSON-3.8.1.TAR.GZCD Simplejson-3.8.1python setup.py Install
(8), ansible installation
Https://github.com/ansible/ansible/archive/https://pypi.python.org/pypi/ansible #本人采用此地址版本tar ZXVF ANSIBLE-2.0.1.0.TAR.GZCD Ansible-2.0.1.0python setup.py Install
2. Ansible Configuration
(1), SSH password-free key login settings
Implementation of SSH password-free authentication login on each machine separately
Vim/etc/ssh/sshd_config turn on rsaauthentication Yes # Enable RSA authentication Pubkeyauthentication Yes # Enable public key private key pairing authentication Mode Authorizedkeysfile. SS H/authorized_keys # Public key file path ssh-keygen-t RSA generate key File CD ~/.ssh/cat id_rsa.pub >> authorized_keyschmod Eys
All the machines ' keys are added to their authorized_keys.
To restart the SSH service:
/bin/systemctl Restart Sshd.service
(2), ansible configuration
Mkdir-p/etc/ansiblevim/etc/ansible/ansible.cfginventory =/etc/ansible/hostsremote_port = 22
Host group definition
vim/etc/ansible/hosts[web59]192.168.1.59
(3), simple test
Ansible all-m command-a ' uptime ' 192.168.1.229 | SUCCESS | Rc=0 >> 13:00:08 up 5 days, 18:58, 2 users, load average:0.01, 0.03, 0.00192.168.1.59 | SUCCESS | Rc=0 >> 13:03:29 up 5 days, 19:20, 5 users, load average:0.00, 0.00, 0.00
3, the use of common modules
(1), Setup
To view some basic information about a remote host
Ansible web59-m Setup
(2), ping
Used to test the running state of a remote host
Ansible all-m Ping
(3), file
Setting the properties of a file
Force: There are two cases of forcing the creation of a soft link, one where the source file does not exist, but it will be established later, the other is the target soft link already exists, need to first cancel the previous soft chain, and then create a new soft chain, there are two options: yes|nogroup: Definition File/ Genus of Directories mode: Define permissions for files/directories owner: Define the File/directory's master path: Required option, define path to File/directory recurse: Recursive settings file properties, only valid for directory src: source file path linked, only for state= Link situation dest: The path that is linked to, applies only to State=link case state:directory: If the directory does not exist, create a directory file: Even if the file does not exist, will not be created link: Create a soft link hard: Create a rigid link touch: If the file does not exist, a new file is created, and if the file or directory already exists, update its last modified time absent: Deleting a directory, file, or unlinked file
Remote file Symbolic link creation
Ansible web59-m file-a "Src=/root/aa dest=/tmp/aa state=link"
Remote File Information view
Ansible web59-m command-a "Ls-al/tmp/aa"
Remote file Symbolic link deletion
Ansible web59-m file-a "Path=/tmp/aa state=absent"
(4), copy
Copying files to a remote host
The relevant options are as follows:
Backup: Before overwriting, the source file is backed up, and the backup file contains the time information. There are two options: yes|nocontent: Used instead of "src", you can directly set the value of the specified file dest: Required option. To copy the source file to the absolute path of the remote host, if the source file is a directory, the path must also be a directory Directory_mode: Recursively set the permissions of the directory, default to the system default permissions force: If the target host contains the file, but the content is different, if set to Yes, Then the overwrite is enforced, and if no, the file is copied only if the target location for the destination host does not exist. The default is yesothers: All the options in the file module can be used here src: a local file that is copied to the remote host, either an absolute path or a relative path. If the path is a directory, it will be replicated recursively. In this case, if the path ends with "/", only the contents of the directory are copied, and if "/" is not used to end, the entire content, including the directory, is copied, similar to rsync.
Copy Local single file "/ROOT/AA" to remote server
Ansible web59-m copy-a "Src=/root/aa dest=/tmp/aa owner=root group=root mode=0644"
Copy a local folder "/root/123" to a remote server
Ansible web59-m copy-a "src=/root/123 dest=/tmp/"
Remote File Information view
Ansible web59-m command-a "Ls-al/tmp/aa"
(5), command
The relevant options are as follows:
Creates: A filename, when the file exists, the command does not perform free_form: The Linux command to execute chdir: Before executing the instruction, switch to the directory removes: A file name, when the file does not exist, Then this option does not execute executable: Switch the shell to execute the instruction, the execution path must be an absolute path
Executing commands on a remote host
Ansible web59-m command-a "uptime"
(6), Shell
Switch to a shell to execute the specified instruction, with the same parameters as the command.
Unlike command, this module can support the command pipeline while another module also has this capability: raw
First create a shell script locally
Vim/tmp/test.sh#!/bin/bashdate +%f_%h:%m:%s
Distribute the created script file to the remote
Ansible web59-m copy-a "src=/tmp/test.sh dest=/tmp/test.sh owner=root group=root mode=0755"
Remote execution
Ansible web59-m shell-a "/tmp/test.sh"
CENTOS7 Source Compilation Installation Ansible detailed deployment