ansible

一、ansible简介

Ansible可以同时管理Redhat系的Linux,Debian系的Linux,以及Windows主机。管理节点只在执行脚本时与远程主机连接,没有特别的同步机制,所以断电等异常一般不会影响ansbile

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:

1、连接插件connection plugins:负责和被监控端实现通信

2、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机

3、各种模块核心模块、command模块、自定义模块

4、借助于插件完成记录日志邮件等功能

5、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务

ansible的架构:连接其他主机默认使用ssh协议

在这里插入图片描述

二、ansible安装

1、环境部署

master:192.168.100.11

slave1:192.168.200.12

slave2:192.168.200.13

2、安装epel源

[root@master ~]# yum install -y epel-release

3、安装ansible

[root@master ~]# yum install -y ansible

[root@master ~]# ls /etc/ansible/ #查看ansible下的文件

在这里插入图片描述

4、修改hosts文件

[root@master ~]# vim /etc/ansible/hosts

#将一下插入到hosts中

[webserver] #想到与给远程控制的主机取名
192.168.200.12
[mysql]
192.168.200.13

5、设置密钥对验证

[root@master ~]# ssh-keygen -t rsa

[root@master ~]# ssh-copy-id root@192.168.200.12

[root@master ~]# ssh-copy-id root@192.168.200.13

在这里插入图片描述
在这里插入图片描述

6、面交互代理

[root@master ~]# ssh-agent bash

[root@master ~]# ssh-add

在这里插入图片描述

三、ansible模块

1、command模块

命令格式:ansible [主机] [-m 模块] [-a args]

[root@master ~]# ansible-doc -l #列出所有已安装的模块,按q退出

[root@master ~]# ansible-doc -s yum #-s列出yum模块描述信息和操作动作

[root@master ~]# ansible 192.168.200.12 -m command -a ‘date’ #指定IP执行date命令

[root@master ~]# ansible mysql -m command -a ‘date’ #指定名称执行date命令

[root@master ~]# ansible all -m command -a ‘date’ #所有主机执行date命令

如果不加-m模块,则默认为command模块

在这里插入图片描述

2、cron模块

#计划性任务模块

[root@master ~]# ansible-doc -s cron #查看cron模块信息

[root@master ~]# ansible 192.168.200.12 -m cron -a ‘minute="*/1" job="/usr/bin/echo fyf" name=“job”’

#minute指时间,每分钟执行一次;job值所要执行的任务;name指你这个计划的名称

[root@master ~]# ansible 192.168.200.12 -m command -a ‘crontab -l’ #进行查看

[root@master ~]# ansible 192.168.200.12 -m cron -a ‘name=“job” state=absent’ 关闭计划性任务

在这里插入图片描述

3、user模块

user模块请求的是useradd,userdel,usermod三个指令

[root@master ~]# ansible-doc -s user #查看user模块情况

[root@master ~]# ansible 192.168.200.12 -m user -a ‘name=“fyf”’ #创建用户

[root@master ~]# ansible 192.168.200.12 -m command -a ‘tail /etc/passwd’ #查看创建的用户

[root@master ~]# ansible 192.168.200.12 -m user -a ‘name=“fyf” state=“absent”’ #删除用户

在这里插入图片描述
在这里插入图片描述

4、group模块

#group模块请求的是groupadd,groupdel,groupmod三个指令

[root@master ~]# ansible-doc -s group #查看group模块的情况

[root@master ~]# ansible 192.168.200.12 -m group -a ‘name=fyf gid=333 system=yes’ #创建组,设置gid号,以及创建系统组

[root@master ~]# ansible 192.168.200.12 -a ‘tail /etc/group’ #查看组创建情况

[root@master ~]# ansible 192.168.200.12 -m user -a ‘name=“zzz” uid=“444” system=“yes” group=“fyf”’ #把zzz用户添加到fyf组中

[root@master ~]# ansible 192.168.200.12 -a “id zzz” #查看zzz用户的信息

在这里插入图片描述

5、copy模块

[root@master ~]# ansible-doc -s copy #查看copy模块的情况

[root@master ~]# ansible 192.168.200.12 -m copy -a ‘src=/etc/fstab dest=/opt/fstab.bak owner=root mode=644’ #将fastab文件复制到/opt下面,并修改权限

[root@master ~]# ansible 192.168.200.12 -a “ls /opt” #查看/opt下的文件

在这里插入图片描述

[root@master ~]# ansible 192.168.200.12 -m copy -a ‘content=“hahah” dest=/opt/aaa’ #将hahah文本导入到aaa文件中

[root@master ~]# ansible 192.168.200.12 -a ‘cat /opt/aaa’ #进行查看

在这里插入图片描述

6、file模块

#指定文件属性

ansible test01 -m user -a ‘name=lisi system=yes’
ansible test01 -m group -a ‘name lisi system=yes’
ansible test01 -m file -a ‘owner=lisi group=lisi mode=644 path=/opt/test.txt’

ansible test01 -m file -a ‘src=/opt/test.txt path=/opt/test.txt.link state=link’ # 创建软链接
ansible test01 -m file -a ‘path=/opt/123 state=touch’ # 创建123文件
ansible test01 -m file -a ‘path=/opt/123.txt state=absent’ # 删除123文件
ansible test01 -m file -a ‘path=/opt/test state=directory mode=700’

7、ping模块

[root@master ~]# ansible all -m ping

在这里插入图片描述

8、yum模块

[root@master ~]# ansible 192.168.200.12 -m yum -a ‘name=httpd’ #安装httpd服务

[root@master ~]# ansible 192.168.200.12 -m yum -a ‘name=httpd state=absent’ #卸载httpd服务

在这里插入图片描述

9、service模块

[root@master ~]# ansible 192.168.200.12 -a ‘systemctl start httpd’

[root@master ~]# ansible 192.168.200.12 -m service -a ‘enabled=true name=httpd state=started’

在这里插入图片描述

10、shell模块

[root@master ~]# ansible 192.168.200.12 -m shell -a ‘echo 123123 | passwd --stdin zzz’ #此处修改密码需要用户存在
在这里插入图片描述

11、script模块

[root@master ~]# cd /opt/

[root@master opt]# vim test.sh

#!/bin/bash
echo “hello ansible from script” > /opt/script.txt

[root@master opt]# chmod +x test.sh

[root@master opt]# ansible 192.168.200.12 -m script -a ‘test.sh’

[root@master opt]# ansible 192.168.200.12 -a ‘cat /opt/script.txt’

在这里插入图片描述

12、setup模块

[root@master opt]# ansible 192.168.200.12 -m setup

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值