First, Ansible Basic installation
Install RPM using Yum with epel6,7 and more. However, you must install Python2.4 or a later version of Python.
Add Source:
Wget-o/etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
Yum Install Ansible-y
Second, ansible configuration
1. SSH face key login settings
# #生成公钥/Private key
SSH-KEYGEN-T RSA
Ssh-copy-id-i ~/.ssh/id_rsa.pub [email protected]
chmod 600/root/.ssh/authorized_keys
2. Ansible Configuration
#主机组定义
Vim/etc/ansible/hosts
[Web-server]
192.168.6.80
192.168.6.81
192.168.6.82
192.168.6.83
192.168.6.84
3. Test (with native and 81 as test case)
#ansible web-server-m Ping
650) this.width=650; "title=" Ansible-ping.png "src=" http://s1.51cto.com/wyfs02/M01/83/63/ Wkiol1dyfrixdsp4aaazhwemuow952.png "alt=" Wkiol1dyfrixdsp4aaazhwemuow952.png "/>
Note: The first time you run, you need to enter "yes" for public key verification, subsequent no need to enter again.
Third, the use of common modules
1. Setup
# #用来查看远程主机的一些基本信息
650) this.width=650; "title=" Ansible-setup.png "src=" http://s1.51cto.com/wyfs02/M02/83/63/ Wkiol1dyf9bwmeylaac0prhl0v4961.png "alt=" Wkiol1dyf9bwmeylaac0prhl0v4961.png "/>
2. Ping
# #用来测试远程主机的运行状态
#ansible web-server-m Ping
650) this.width=650; "title=" Ansible-ping.png "src=" http://s3.51cto.com/wyfs02/M01/83/64/wKiom1dyGB_ Wa-kxaaazhwemuow322.png "alt=" Wkiom1dygb_wa-kxaaazhwemuow322.png "/>
3. File
# #设置文件的属性
The relevant options are as follows
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|no
Group: Defines the genus of files/directories
Mode: Define permissions for files/directories
Owner: Defines a file/directory owner
Path: Required option, defines the path to the file/directory
Recurse: Recursive settings file properties, only valid for directory
SRC: The path of the source file being linked, only for the case of State=link
Dest: The path to be linked to only the State=link case
State
Directory: Create directories if the directory does not exist
File: Will not be created even if it does not exist
Link: Create a soft link
Hard: Create a rigid link
Touch: If the file does not exist, a new file is created and the last modified time is updated if the file or directory already exists
Absent: Deleting directories, files, or unlinked files
Example:
# #远程文件符号链接创建
#ansible web-server-m file-a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"
650) this.width=650; "title=" Ansilbe-link.png "src=" http://s5.51cto.com/wyfs02/M01/83/64/ Wkiom1dygtwyg9ywaabtiwurbig006.png "alt=" Wkiom1dygtwyg9ywaabtiwurbig006.png "/>
#远程查看文件信息
#ansible web-server-m command-a ' ls-la/tmp/resolv.conf '
650) this.width=650; "title=" Ansible-ls.png "src=" Http://s4.51cto.com/wyfs02/M02/83/63/wKioL1dyG6aDLvgTAAAeFe_ Bdm0195.png "alt=" Wkiol1dyg6adlvgtaaaefe_bdm0195.png "/>
#删除符号链接
#ansible web-server-m file-a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=absent"
650) this.width=650; "title=" Ansible-absent.png "style=" height:268px;width:784px "src=" Http://s1.51cto.com/wyfs02 /m02/83/63/wkiol1dyhewdvpjyaabbqgynspm179.png "width=" 796 "height=" 276 "alt=" Wkiol1dyhewdvpjyaabbqgynspm179.png "/ >
4. Copy
#复制文件到远程主机
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|no
Content: Used instead of "src", you can set the value of the specified file directly
Dest: Required option. The absolute path of the remote host to which the source file is to be copied, and if the source file is a directory, the path must also be a directory
Directory_mode: Recursively set permissions for the directory, default to system default permissions
Force: If the target host contains the file, but the content is different, if set to Yes, the override is enforced, and if no, the file is copied only if the target location for the destination host does not exist. The default is Yes
Others: 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.
Example:
# #将本地文件 "/etc/ansible/ansible.cfg" copied to the remote server
#ansible web-server-m copy-a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"
650) this.width=650; "title=" Ansible-copy.png "src=" http://s1.51cto.com/wyfs02/M02/83/64/ Wkiom1dyhdbwliwiaacttjqfgne071.png "alt=" Wkiom1dyhdbwliwiaacttjqfgne071.png "/>
5. Command
# #在远程主机上执行命令
The relevant options are as follows:
Creates: A filename when the file exists, the command does not execute
Free_form: Linux instructions to execute
ChDir: Before executing the instruction, switch to the directory
Removes: A filename when the file does not exist, this option does not execute
Executable: Switch the shell to execute the instruction, the execution path must be an absolute path
Example: ansible web-server-m command-a ' Date '
650) this.width=650; "title=" Ansible-date.png "src=" http://s5.51cto.com/wyfs02/M00/83/64/wKiom1dyIV_ A1vqhaaaukravtey028.png "alt=" Wkiom1dyiv_a1vqhaaaukravtey028.png "/>
6. Shell
# #切换到某个shell执行脚本 with the same parameters as the command
Example:
# #现在本地创建一个shell脚本
#vim/root/test.sh
#!/bin/sh
Date +%f_%h:%m:%s
Distribute the created script file to the remote
#ansible web-server-m copy-a "src=/root/test.sh dest=/root/test.sh owner=root group=root mode=0755"
Remote execution:
#ansible web-server-m shell-a "/root/test.sh"
650) this.width=650; "title=" Ansible-shell.png "src=" http://s2.51cto.com/wyfs02/M02/83/64/ Wkiol1dyjqasebrraaas3aplwoe449.png "alt=" Wkiol1dyjqasebrraaas3aplwoe449.png "/>
More modules are available for reference:
#ansible-doc-l
Website: http://docs.ansible.com/ansible/
This article is from "Linux OPS rookie" blog, please be sure to keep this source http://zhangchuang.blog.51cto.com/10884116/1793643
The ansible of automatic operation and maintenance tools (II.)