The idea-the core concept of the automated Operation Koriyuki Ansible, installs the configuration ansible and learns to use its common modules.

Source: Internet
Author: User

Automated operations Overview:
    • Nowadays with the rapid development of the Internet, the traditional operation and maintenance mode is too inefficient, the deployment of automated operation and maintenance can be done safely and efficiently, and become the main mode of operation.
    • The automatic operation and maintenance tools will generally be divided into two categories: one is the need to use proxy tools, that is, based on a dedicated agent to complete the management functions, such as: Puppet, Func, Zabbix and so on; the other is that you do not need to configure the agent tool directly based on the SSH service to complete the management functions, such as: Ansible, Fabric, etc., Ansible Introduction:
    • Ansible is a new automated operation tools, based on Python development, the collection of many operations tools (puppet, Chef, Func, fabric) The advantages of the implementation of batch system configuration, batch program deployment, batch Run command and other functions.
    • 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.
    • 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.
    • Ansible Architecture: Connect to other hosts by default using the SSH protocol, the deployment of each module and plug-in can be clearly understood.

    • Ansible Operational Management Relationship diagram:
      The hint ansible several features:
    • Deployment is simple, only need to deploy ansible environment in the main control side, the control side does not need to do any action;
    • The device is managed by default using SSH protocol;
    • There are a large number of conventional operation and maintenance module, which can achieve most daily operation.
    • Simple configuration, powerful, and strong extensibility;
    • Support API and custom modules that can be easily extended via python;
    • Through the playbooks to customize the powerful configuration, state management;
    • Lightweight, no need to install agent on the client, update, only need to do an update on the machine;
    • Provides a powerful, highly operational web management interface and Rest API interface--AWX platform. Installation configuration ansible:

      1. Environment Readiness:

Host name Operating System IP Address Installing the Software Group name
Management side CentOS7.5 192.168.72.128 Ansible /
Managed side CentOS7.5 192.168.72.129 / Webserver
Managed side CentOS7.5 192.168.72.130 / Mysql

2. Installation Services :
Install the Epel source before installing ansible on the management host:

* systemctl stop firewalld.service     #关闭防火墙* setenforce 0                                 #关闭增强性安全功能* yum install -y epel-release      #安装epel源* yum install ansible -y              #安装Ansible* ansible --version          #查看ansible版本* yum install tree -y       #安装树结构查询服务* tree /etc/ansible/         #树状结构展示文件夹  /etc/ansible/ ├── ansible.cfg      #ansible的配置文件 ├── hosts              #ansible的主仓库,用于存储需要管理的远程主机的相关信息 └── roles               




3. Configure the Host inventory:

* cd /etc/ansible     * vi hosts               #配置主机清单  [webserver]        #定义一个被管理端的组名  192.168.72.129      #指定被管理主机的IP  [mysql]  192.168.72.130


4. Set SSH login without password:

* ssh-keygen -t rsa     #生成密钥* ssh-copy-id [email protected]* ssh-copy-id [email protected]    #配置密钥对验证 ↓↓免交互代理 * ssh-agent bash* ssh-add


Ansible command-line module:

1.command module (for running commands on managed hosts)

// 命令格式:ansible [主机] [-m 模块] [-a args]*  ansible-doc -l           #列出所有已安装的模块 注:按q退出* ansible-doc -s yum         #-s列出yum模块描述信息和操作动作* ansible 192.168.80.182 -m command -a ‘date‘       #指定ip执行date* ansible webserver -m command -a ‘date‘              #指定分类执行date* ansible mysql -m command -a ‘date‘       * ansible all -m command -a ‘date‘               #所有hosts主机执行date命令* ansible all -a ‘ls /‘             # 如果不加-m模块,则默认运行command模块



2.cron module (for defining task schedules)

// 两种状态(state):present表示添加(可以省略),absent表示移除。* ansible-doc -s cron        #查看cron模块信息* 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‘    #移除计划任务,假如该计划任务没有取名字,name=None即可


3.user module (for creating new users and changing, deleting existing users)

// user模块是请求的是useradd, userdel, usermod三个指令* ansible-doc -s user* ansible mysql -m user -a ‘name="test01"‘    #创建用户test01* ansible mysql -m command -a ‘tail /etc/passwd‘* ansible mysql -m user -a ‘name="test01" state=absent‘    #删除用户test01


4.group Module (Management of user groups)

//group模块请求的是groupadd, groupdel, groupmod 三个指令。* ansible-doc -s group* ansible mysql -m group -a ‘name=mysql gid=306 system=yes‘* ansible mysql -a ‘tail /etc/group‘* ansible mysql -m user -a ‘name=test01 uid=306 system=yes group=mysql‘* ansible mysql -a ‘tail /etc/passwd‘* ansible mysql -a ‘id test01‘    


5.copy module (for file copying and batch files)

// src=:定义本地源文件//dest=:定义远程目标文件路径// content=:取代src=,表示直接用此处指定的信息生成为目标文件内容* ansible-doc -s copy* ansible mysql -m copy -a ‘src=/etc/fstab dest=/opt/fstab.back owner=root mode=640‘* ansible mysql -a ‘ls -l /opt‘* ansible mysql -a ‘cat /opt/fstab.back‘* ansible mysql -m copy -a ‘content="hello heihei!"dest=/opt/fstab.back‘       


6.file Module (set file properties)

//src=:指明源文件//path=:指明符号链接文件路径* ansible-doc -s file* 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‘          #修改文件的属主属组权限等* ansible mysql -m file -a ‘path=/opt/fstab.link src=/opt/fstab.back state=link‘     #设置/opt/fstab.link为/opt/fstab.back的链接文件* ansible mysql -m file -a "path=/opt/fstab.back state=absent"             #删除一个文件* ansible mysql -m file -a "path=/opt/test state=touch"            #创建一个文件


7.ping Module (test whether the specified host can connect)

//测试指定主机是否能连接*  ansible all -m ping


8.service module (used to control the running state of the management Service)

// nabled=:是否开机自动启动,取值为true或者false//name=:服务名称// state=:状态  取值有started,stopped,restarted* ansible-doc -s service* ansible webserver -a ‘yum install -y httpd‘       #安装httpd,webserver需要有httpd服务才能控制* ansible webserver -m service -a ‘enabled=true name=httpd state=started‘    #启动httpd服务* ansible webserver -a ‘systemctl status httpd‘        #查看web服务器httpd运行状态* systemctl status httpd        #在webserver组的主机查看是否开启



9.shell modules (run commands on a remote host, especially complex commands that use functions such as pipelines)

* ansible-doc -s shell* ansible mysql -m shell -a ‘echo abc123|passwd --stdin mysql‘      #创建用户使用无交互模式给用户设置密码


10.script module (Copy the local script to the remote host and run it.) Note: To apply a relative path to the specified script)

* ansible-doc -s script* vi test.sh  #!/bin/bash  echo "hello ansible from script"> /opt/script.txt* chmod +x test.sh* ansible mysql -m script -a ‘test.sh‘* ansible mysql -a ‘cat /opt/script.txt‘



11.yum Module (Install package)

// name=:指明要安装的程序包,可以带上版本号// state=:present,latest表示安装,absent表示卸载*  ansible-doc -s yum*  ansible mysql -m yum -a ‘name=zsh‘          #yum安装zsh*  ansible mysql -a ‘rpm -q zsh‘                     #检查是否安装zsh* ansible mysql -m yum -a ‘name=zsh state=absent‘     #卸载zsh* ansible mysql -a ‘rpm -q zsh‘   


12.setup module (collects facts from remote host)

//每个被管理节点在接受并运行管理命令之前,会将自己主机相关信息,如操作系统版本,IP地址等报告给远程的ansible主机。* ansible-doc -s setup* ansible mysql -m setup          #获取mysql组主机的facts信息

The idea-the core concept of the automated Operation Koriyuki Ansible, installs the configuration ansible and learns to use its common modules.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.