自动化运维工具Ansible

  • Post author:
  • Post category:其他




简介

Ansible是一款简单的运维自动化工具,只需要使用ssh协议连接就可以来进行系统管理,自动化执行命令,部署等任务。



Ansible的特点

1、ansible不需要单独安装客户端,也不需要启动任何服务

2、ansible是python中的一套完整的自动化执行任务模块

3、ansible playbook 采用yaml配置,对于自动化任务执行过一目了然



Ansible组成结构


  • Ansible


    是Ansible的命令工具,核心执行工具;一次性或临时执行的操作都是通过该命令执行。

  • Ansible Playbook


    任务剧本(又称任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执行,yaml格式。

  • Inventory


    Ansible管理主机的清单,默认是/etc/ansible/hosts文件。

  • Modules


    Ansible执行命令的功能模块,Ansible2.3版本为止,共有1039个模块。还可以自定义模块。

  • Plugins


    插件,模块功能的补充,常有连接类型插件,循环插件,变量插件,过滤插件,插件功能用的较少。

  • API


    提供给第三方程序调用的应用程序编程接口。



Ansible安装

#安装epel源
[root@master ~] yum install -y epel-release
#安装ansible
[root@master ~] yum install -y ansible
#查看ansible的版本
[root@master ~]adsible --version
#编辑hosts映射
[root@localhost ~]# cat /etc/an
anacrontab  ansible/
[root@master ~]# vi /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
#此处添加需要被管理的服务器
[webserver]
20.0.0.4
[mysql]
20.0.0.10

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Here's another example of host ranges, this time there are no
# leading 0s:

## db-[99:101]-node.example.com

您在 /var/spool/mail/root 中有新邮件
#做免密登录
[root@master ~]ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:LtTJOXJy+UY2iZsS63hBc3ZSxPzL70km7Ggv5WnDHrE root@hk-2
The key's randomart image is:
+---[RSA 2048]----+
|         +.      |
|          +      |
|         . .     |
|      oo+=...    |
|     .=+So=...   |
|     ..O B o+o   |
|      +.+ o+Eoo  |
|     o.o .oo*=.. |
|    ...  ..=+oo  |
+----[SHA256]-----+
#复制公钥至被管理服务器,并做免交互代理
[root@master ~]ssh-copy-id root@20.0.0.4
[root@master ~]ssh-copy-id root@20.0.0.10
[root@master ~]ssh-agent bash
[root@master ~]ssh-add



ansible的使用


command模块

命令格式:ansible [主机] [-m 模块] [-a args]
ansible-doc -l		//列出所有已安装的模块(按q退出)
ansible-doc -s yum	//-s列出yum模块描述信息和操作动作

ansible web -m command -a 'date'	//web在hosts文件中,指定分类执行
ansible 192.168.100.22 -m command -a 'date'		//指定IP执行
ansible mysql -m command -a 'date'
ansible all -m command -a 'date'		//所有hosts主机执行date文件
ansible all -a 'ls /'		//如果不加-m模块,则默认运行command模块


user模块

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


group模块

group模块请求的是useradd,userdel,usermod三个指令
ansible-doc -s group
ansible web -m group -a 'name="testgroup" gid=1020 system=yes'
ansible web -a 'tail /etc/group'
ansible web -m user -a 'name=testuser uid=1020 system=yes group=testgroup'
ansible web -a 'tail /etc/passwd'
ansible web -a 'id testuser'


copy模块

ansible-doc -s copy
ansible web -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640'
ansible web -a 'ls -l /opt'
ansible web -a 'cat /opt/fstab.back'

ansible web -m copy -a 'content="hello!" dest=/opt/fstab.back'	//将hello!写入/opt/fstab.back
ansible web -a 'cat /opt/fstab.back'


file模块

ansible-doc -s file
ansible web -m user -a 'name=mysql system=yes'
ansible web -m group -a 'name=mysql system=yes'
ansible web -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back'	//修改文件的属主属组权限等
ansible web -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link'			//设置链接文件
ansible web -m file -a "path=/opt/fstab.back state=absent"	//删除文件
ansible web -m file -a "path=/opt/test state=touch"			//建立文件
ansible web -m file -a 'path=/opt/temp state=directory mode=755'	//创建目录


ping模块

ansible all -m ping
20.0.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
20.0.0.10 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}


yum模块

ansible-doc -s yum 
ansible web -m yum -a 'name=httpd'		//安装httpd
[root@server ~]# rpm -q httpd

ansible web -m yum -a 'name=httpd state=absent'		//卸载httpd
[root@server ~]# rpm -q httpd


service模块

ansible-doc -s service
[root@server ~]# yum -y install httpd
ansible web -a 'systemctl status httpd'	//查看httpd运行状态
ansible web -m service -a 'enabled=true name=httpd state=started'	//启动httpd服务
ansible all -m service -a 'name=firewalld state=stopped'	//关闭防火墙
[root@server ~]# systemctl status httpd


script模块

ansible-doc -s script
vi test.sh	//在ansible管理端创建脚本
#!/bin/bash
echo "hello ansible from script"> /opt/script.txt

chmod +x test.sh
ansible web -m script -a 'test.sh'
[root@server ~]# cat /opt/script.txt



版权声明:本文为Ghostpant原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。