Automated operations tools Ansible BASIC applications

Source: Internet
Author: User
Tags ssh port

Ansible is an automated operations tools, 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 capability that does not have the ability to deploy in batches. 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 communication with the monitored side

2, Host Inventory: Specifies the operation of the hosts, is a profile in a show off the definition of monitoring the host

3. Various core modules, command modules, custom modules

4, through the use of plug-ins to complete log mail and other functions

5. Playbook: Allows slave nodes to perform multiple tasks at once


Features of the ansible:

1. Modularization: Call a specific module to complete a specific task

2, based on Python language implementation, with Paramiko,pyyaml and JINJA2 three key modules

3. Simple Deployment

4. Support Custom Module

5, support Playbook


Prepare the Environment

Host Name Ip
localhost (ansiblemaster) 10.10.86.56
Node1 (Ansibleslave) 10.10.73.148
Bogon (Ansibleslave) 10.10.73.149


First, the installation of ansible

[email protected] ~]# Yum install ansible# #配置文件:/etc/ansible/ansible.cfg# #主机清单:/etc/ansible/hosts# #主程序: Ansible, Ansible-doc, Ansible-playbook


Second, ansible-free key SSH login

[email protected] ~]# ssh-keygen -t rsa -p  "[[Email protected]st  ~]# ssh-copy-id -i [email protected]/usr/bin/ssh-copy-id: info: attempting  to log in with the new key (s), to filter out any  That are already installed/usr/bin/ssh-copy-id: warning: all keys were  skipped because they already exist on the remote system. [[Email protected] ~]# ssh-copy-id -i [email protected]/usr/bin/ssh-copy-id:  info: attempting to log in with the new key (s), to  Filter out any that are already installed/usr/bin/ssh-copy-id: warning:  all keys were skipped because they already exist on the  remote system.# #将生产的秘钥推送给slave节点, first-time interaction required (yes/no) 


Third, the host group customization

[Email protected] ~]# vim/etc/ansible/hosts[ansible_agent]10.10.73.148 # #port 10.10.73.149 # #port # # If the SSH port of slave is not the default of 22, then the corresponding port behind the host can be


Four, simple test

Get a list of common modules for ansible

[Email protected] ~]# ansible-doc-l

Get the usage of a module

[Email protected] ~]# ansible-doc-s command# #COMMAND: Yum, cron, Shell, Setup, copy ...


1. Ping

[Email protected] ~]# ansible ansible_agent-m Ping # #用来测试远程主机的运行状态10.10.73.149 | SUCCESS = {"Changed": false, "ping": "Pong"}10.10.73.148 | SUCCESS = {"Changed": false, "ping": "Pong"}

2. Setup Module

[Email protected] ~]# ansible ansible_agent-m "Setup" # #获取远程主机的详情信息


3. Command Module

[Email protected] ~]# ansible ansible_agent-m shell-a "ls/tmp" 10.10.73.149 | SUCCESS | Rc=0 >>ansible_z1xg26wtc10.10.73.148 | SUCCESS | Rc=0 >>ansible_6sq3_dreport.shwtc.txt


4. Shell Module

[Email protected] ~]# ansible ansible_agent-m shell-a "echo wxpp | passwd--stdin WTC "10.10.73.148 | SUCCESS | rc=0 >>changing password for user wtc.passwd:all authentication tokens updated successfully.10.10.73.149 | SUCCESS | Rc=0 >> Change the user WTC password. PASSWD: All the authentication tokens have been successfully updated. # # # #ansible是要支持管道命令, you have to use shell modules, and you want to support shell features, you must use Shell modules


5. Copy Module

(1) src=\ ' #\ ' "/p>

(2) content= dest=

(3) Owner: Specify the Owner

(4) Group: Specify array

(5) Mode: Specify Permissions

[[email protected] tmp]# ansible ansible_agent -m copy -a  "src=/tmp/ wxpp.txt  dest=/tmp mode=665 "  # #复制本地的" wxpp.txt "file to the remote host 10.10.73.148 |  success => {     "changed": true,      "checksum" :  "7641dc777dc18a1c2dfa3429aa8009c12c566913",      "dest":  "/tmp/wxpp.txt",       "GID": 0,      "group":  "root",       "mode":  "0665",      "owner":  "Root",       "path":  "/tmp/wxpp.txt",      "Size": 36,       "state":  "file",      "UID":  0}10.10.73.149 | success  => {     "changed": true,      "checksum":  "7641dc777dc18a1c2dfa3429aa8009c12c566913 ",     " dest ": "/tmp/wxpp.txt ",     " GID ":  0,      "group":  "root",      "mode":  "0665",       "owner":  "root",      "path":  "/tmp/wxpp.txt",       "Size": 36,      "state":  "file",       "UID":  0}[[email protected] tmp]# ansible ansible_agent -m  shell -a  "Cat /tmp/wxpp.txt"    # #追加指定的文本至远程主机中10 .10.73.149 |  success | rc=0 >>wtc sent a bouquet of roses to  Wxpp10.10.73.148 | success | rc=0 >>wtc sent a bouquet of  roses to wxpp[[email protected] tmp]# ansible ansible_agent -m  copy -a  "content=" \Nhello world '  dest=/tmp/wxpp.txt ' 10.10.73.149 | success => {      "Changed": true, }10.10.73.148 | success => {      "changed":  true, } [[email protected] tmp]# ansible ansible_agent  -m shell -a  "Cat /tmp/wxpp.txt" 10.10.73.149 | success | rc=0  >>hello world10.10.73.148 | success | rc=0 >>hello world# #content是覆盖文件源内容, note when using


6. Cron Module

(1) month=

(2) day=

(3) hour=

(4) weekday=

(5) minute=

(6) job= # #指明运行的命令是什么

(7) Name= # #指明定时任务描述

(8) State # #指定状态, prsent indicates the addition of a timed task, which is also the default value. Absent to delete a scheduled task

(9) User # #指明以那个用户的身份执行

[[email protected] ~]# ansible ansible_agent -m shell -a  "Ls /var /spool/cron/"10.10.73.149 | success | rc=0 >>root10.10.73.148 |  Success | rc=0 >>root[[email protected] ~]# ansible ansible_agent  -m cron -a  "minute= ' */1 '  job= '/usr/sbin/ntpdate 10.10.86.56 &>  /dev/null '  user=wtc name= ' wtc job ' "10.10.73.149 | success => {      "changed": true,      "Envs": [],       "Jobs": [         "Wtc job"      ]}10.10.73.148 | SUCCESS => {     "Changed": true,       "Envs": [],      "Jobs": [          "Wtc joB "    ]}[[email protected] ~]# ansible ansible_agent -m  shell -a  "CAT /VAR/SPOOL/CRON/WTC" 10.10.73.149 | success | rc=0 > > #Ansible: wtc job*/1 * * * * /usr/sbin/ntpdate 10.10.86.56  &> /dev/null10.10.73.148 | SUCCESS | rc=0 >> #Ansible:  WTC  job*/1 * * * * /usr/sbin/ntpdate 10.10.86.56 &> /dev/null


7. File module:

(1) Create linked files: src=, path=, state=

(2) Modify file properties: Path=, owner=, mode=, group=

(3) Create directory: Path=, state=

(4) state

Directory: If the directory does not exist, it will create

Link: Create a soft link

Hard: Create a rigid link

Touch: If the file does not exist, it will be created. If the file exists, modify the Mtime

Absent: Delete a directory, file, unlink

[[email protected] ~]# ansible ansible_agent -m file -a  "src=/etc/ Fstab path=/tmp/fstab.link state=link "10.10.73.148 | success => {      "changed": true,      "Dest":  "/tmp/fstab.link",       "GID": 0,      "group":  "root",       "mode":  "0777",      "owner":  "Root",      " Size ": 10,     " src ": "/etc/fstab ",     " state ":   "link",      "UID":  0}10.10.73.149 | success => {      "changed": true,      "Dest":  "/tmp/fstab.link",       "GID": 0,      "group":  "root",       "mode":  "0777 ",     " owner ": " root ",     " size ": 10,       "src":  "/etc/fstab",      "state":  "link",       "UID":  0}[[email protected] ~]# ansible ansible_agent -m  shell -a  "Ls /tmp" 10.10.73.149 | success | rc=0 >>ansible_ Tdmfakfstab.linkwtcwxpp.txt10.10.73.148 | success | rc=0 >>ansible_ 1ouvukfstab.linkreport.shwtc.txtwxpp.txt[[email protected] ~]# ansible ansible_agent  -m file -a  "Path=/tmp/fstab.link state=absent" 10.10.73.149 | success =>  {     "changed": true,      "path":  "/tmp/ Fstab.link ",     " state ": " absent "}10.10.73.148 | success =>  {     "changed":  true,      "path":  "/tmp/fstab.link",      "state":   "Absent"}[[email protected] ~]# ansible ansible_agent -m shell -a   "Ls /tmp" 10.10.73.148 | success | rc=0 >>ansible_ Jtgqoireport.shwtc.txtwxpp.txt10.10.73.149 | success | rc=0 >>ansible_ CuFRGAwtcwxpp.txt


8. More modules (ansible-doc-l)

Fetch module: Pull the remote host's file to local (only the file is pulled, but after it is pulled to the local directory, named in the "hosts" file)

Hostname module: Managing host Names

Yum modules: Installing applications and uninstalling programs to remote hosts

Service modules: Managing services on remote hosts

User module: Manage users ' accounts and passwords

Group module: Managing System User groups


Summarize:

1, Ansible is based on SSH no key landing, limited to the current user. If the "WTC" user creates the Ansible key and pushes it to the remote host, a password is required to use the "root" user push command

2, for the "shell" module, the individual think it can achieve the function of most modules, as long as the command expression can be expressed, for example:

Ansible ansible_agent-m shell-a "yum install httpd" ansible ansible_agent-m shell-a "service httpd start" Ansible ANSI Ble_agent-m shell-a "Echo ' WTC sent a bouquet of roses to wxpp ' &>>/tmp/wxpp.txt" Ansible ansible_agent-m She Ll-a "Ln-sv/etc/fstab/tmp/fstab.link"


This article is from the "WTC" blog, so be sure to keep this source http://wangtianci.blog.51cto.com/11265133/1872586

Automated operations tools Ansible BASIC applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.