Linux下自动化运维工具ansible

文章目录

Ansible简介

Ansible特性

ansible架构

 Ansible 配置使用

1、yum安装及eper-release依赖

2、配置管理主机

3、配置秘钥对 

3.1 生成秘钥对

3.2 推送公钥给被管理机

3.3 测试推送

4、ansible配置

5、命令与选项


Ansible简介

Ansible是一个简单高效的自动化运维管理工具,用Python开发,糅合了众多老牌运维工具的优点实现了批量操作系统配置、批量程序的部署、批量运行命令等功能。仅需在管理工作站上安装ansible程序配置被管控主机的IP信息,被管控的主机无客户端。ansible应用程序存在于epel(第三方社区)源,依赖于很多python组件。主要包括:

  • 连接插件connection plugins:负责和被监控端实现通信;
  • host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
  • 各种模块核心模块、command模块、自定义模块;
  • 借助于插件完成记录日志邮件等功能;
  • playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

Ansible特性

模块化设计,调用特定的模块来完成特定任务,本身是核心组件,短小精悍;

基于Python语言实现,由Paramiko(python的一个可并发连接ssh主机功能库), PyYAML和Jinja2(模板化)三个关键模块实现;

  • 部署简单,agentless无客户端工具;
  • 主从模式工作;
  • 支持自定义模块功能;
  • 支持playbook剧本,连续任务按先后设置顺序完成;
  • 期望每个命令具有幂等性:

ansible架构

  • ansible core:ansible自身核心模块
  • host inventory:主机库,定义可管控的主机列表
  • connection plugins:连接插件,一般默认基于ssh协议连接
  • modules:core modules(自带模块)、custom modules(自定义模块)
  • playbooks:剧本,按照所设定编排的顺序执行完成安排任务

 Ansible 配置使用

部署过程

 部署主控端 -----> ssh+key免密码登录 -----> 被控端

1、yum安装及eper-release依赖

# yum install epel-release
# yum install ansible

Ansible组成介绍
tree /etc/ansible/
/etc/ansible/
├── ansible.cfg  # ansible的配置文件
├── hosts  # ansible的主仓库 用来存储需要管理的远程主机的相关信息
└── roles

2、配置管理主机

在hosts文件中添加管理主机的IP地址列表:

# vim /etc/ansible/hosts
[webserver] ---->> 定义一个组名称,到时候批量的时候可以使用模块名字,也可以是IP地址 
192.168.0.200
192.168.0.201

[others]
192.168.0.200
192.168.0.225

列出主机清单

# ansible webserver --list-hosts
  hosts (1):
    192.168.0.200
    192.168.0.201

host-pattern的格式 

多个组

  • 交集

    webserver:&others'

  • 并集

   'webserver:others'
    webserver,others

  • 差集

   'webserver:!others'

示例取交集的方式:

# ansible 'webserver:&others' -m ping
192.168.0.200 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

3、配置秘钥对 

利用SSH连接管理机与被管理机——管理机生成秘钥并推送公钥

3.1 生成秘钥对

ssh-keygen -t rsa

一路回车即可在$HOME/.ssh目录下生成 id_rsa  id_rsa.put 私钥和公钥两个文件。
注: 如果在生成密钥的时候设置了密码,ansible每次执行命令的时候,都会提示输入密钥密码,可通过下面的命令记住密码。

ssh-agent bsh
ssh-add ~/.ssh/id_rsa

3.2 推送公钥给被管理机

ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.200
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.201

注:ssh-copy-id命令会自动将id_rsa.pub文件的内容追加到远程主机root用户下.ssh/authorized_keys文件中

如被管理机器数量较多可以使用 脚本 来自动推送。

3.3 测试推送

# ssh -p22 192.168.0.200 date  //测试一台被管理机器,date表示连接,成功并退出连接
Thu Jul 30 11:42:33 CST 2020
# ansible all -m ping  //ping所有被管理机器
192.168.0.200 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.0.201 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

4、ansible配置

vim /etc/ansible/ansible.cfg
# 禁用每次执行ansbile命令检查ssh key host
host_key_checking = False
# 开启日志记录
log_path = /var/log/ansible.log
# ansible连接加速配置
accelerate_port = 10000
accelerate_multi_key = yes

5、命令与选项

hosts基本语法
主机与组

[webserver]
192.168.0.200
192.168.0.201
 
[dbserver]
192.168.0.202
192.168.0.203

:端口号不是默认的时候,不是默认root,即普通用户端口后面跟 ansible_ssh_user 可以表示为

192.168.0.200:5188 ansible_ssh_user=bertram

Inventory参数说明

要连接的远程主机与设定的主机别名不同时

ansible_ssh_host

指定ssh端口号

ansible_ssh_port

指定连接用户名

ansible_ssh_user

指定连接密码(建议使用--ask-pass)

ansible_ssh_pass

指定sudo密码(建议--ask-sudo-pass)

ansibl_sudo_pass

常用指令与选项

ansible

-u         # 指定用户名
-k         # 提示密码
-i         # 使用指定主机清单
-m         # 使用指定module,默认command
-a         # module参数

ansible-doc

-l        # 列出所有module
-s        # 列出指定模块的使用方法

常用模块及使用方法

(1)配置hosts文件

[webserver]
192.168.0.200
192.168.0.201

(2)配置ansible主机与个客户机间密钥认证(这里直接使用copy模块完成)

copy模块

ansible all -m copy -a 'src="/root/.ssh/id_rsa.pub" dest="/root/.ssh/authorized_keys" mode=600 backup="yes"' --ask-pass
 
说明:
src        # 源文件或文件夹(如果是源文件以/结尾,则只拷贝该目录下的内容)
dest       # 目标文件或目录(父目录不存在时会报错)
mode       # 权限
backup     # 是否需要备份目标目录原来的文件

command模块

①该模块是默认模块,示例分显示所有主机的时间

ansible all -m command -a "date"

②查看组模块websrver的目录

ansible webseraver -m command -a "chdir=/tmp ls"

user模块、group模块

例:在webserver组的每台主机上创建一个用户web1

ansible webserver -m user -a "name='web1' home='/home/test' system='yes' state='present'"
 
说明:
name        # 用户名
home        # 用户home目录
system      # 是否为系统用户
state       # present或absent(present添加,absent删除)

cron模块

①例:创建每周一12点执行 echo "sam 你好"

ansible webserver -m cron -a "minute=0 hour=12  weekday=1 job='echo \"sam 你好\"' name='echo' state='present'"
 
说明:
minute            # 分钟(0-59,*/2)
hour              # 小时(0-23,*/2)
day               # 日(1-31,*/2)
month             # 月(1-12,*/2)
weekday           # 星期(0-6)
name              # 任务名称
backup            # 是否需要备份原来的任务

②创建定时任务

ansible webserver -m cron -a "name=01 minute=*/5 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"

③删除定时任务

ansible webserver -m cron -a "name=None minute=*/5 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' state=absent"

copy模块

例:拷贝/etc/fstab到/tmp/fstab.ansible

ansible webserver -m copy -a "src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=644"
 
说明:
src            # 源文件
dest           # 目标文件
owner          # 文件属主
mode           # 权限
content        # 为目标文件指定内容,此时省略src
 
ansible webserver -m copy -a "content='Hello sam' dest=/tmp/sam.txt"

file模块

①例如:将webserver服务器/tmp/fstab.ansible 的文件权限更改为600

ansible webserver -m file -a "path=/tmp/fstab.ansible mode=600"
 
说明:
path            # 源文件(可以用name,dest)
mode            # 权限
owner           # 属主
state           # 如果是link,则表示要创建软连接(此时源文件用src表示)
src             # 源文件
 
ansible webserver -m file -a "path=/tmp/fstab.link state=link src=/tmp/fstab.ansible"

②远程创建目录

 ansible webserver -m file -a "dest=/data/backup state=directory"   

③远程创建文件

ansible webserver -m file -a "dest=/data/web/index.html state=touch"

yum模块

①例如:在webserver组安装httpd服务

ansible webserver -m yum -a "name=httpd state=present "
 
ansible dbserver -m yum -a "name='*' state=latest"
 
说明:
name            # 包名称,如果state=latest,那么name可以为*表示运行yum update -y ;如果state=present,那么name可以指定本地的一个软件包路径; 也可以是一个用 逗号 分隔的软件包列表
state           # (`present' or `installed', `latest'), or remove (`absent' or `removed')
list            # 等效于yum list <package>

②安装dos2nuix命令

ansible webserver -m yum -a "name=dos2nuix state=installed"

service模块

例如:启动webserver组的httpd服务,并设置为开机启动

ansible webserver -m service -a "name=httpd enabled=yes state=started "
 
说明:
name            # 服务名称
enabled         # 是否开机启动
state           # started,stopped,restarted,reloaded

shell模块

①例如:查看正在运行的httpd服务的进程号

ansible webserver -m shell -a "ps axu|grep httpd"

②把/etc/hosts文件中的内容重定向到/banana.txt 中

ansible webserver -m shell -a "cat /etc/hosts >/banana.txt"

③修改hosts文件

ansible webserver -m shell -a 'sed -i 's#192.168.1.210#192.168.1.118#' /etc/hosts'

④查看定时任务

ansible webserver -m shell -a "crontab -l"

script模块

首先创建一个shell脚本文件test.sh,内容如下:

#!/bin/bash
a=`pwd`
echo "This is $a"
echo "Hello sam ansible from script" >/tmp/script.txt

然后执行ansible调用该脚本

ansible webserver -m script -a "/tmp/test.sh"
 
说明:
参数为脚本名,可以是绝对路径

功能模块ping、setup

ansible webserver -m ping            
 
说明:
测试ansible主机与其他节点的连通性,成功返回 pong
 
ansible webserver -m setup
 
说明:
返回节点的facts信息,这些变量可以在playbook中直接使用

 fetch模块 

①从远程主机拉取文件

ansible 192.168.1.200 -m fetch -a "dest=/tmp  src=/etc/hosts"

②拉取时不创建目录(同名文件会覆盖)

 ansible webserver -m fetch -a "dest=/tmp/  src=/etc/hosts flat=yes"

mount模块

①创建挂载

 ansible webserver -m mount -a "fstype=nfs opts=ro src=192.168.0.200:/data path=/mnt state=mounted"

②卸载

ansible webserver -m mount -a "fstype=nfs opts=ro src=172.16.1.31:/data path=/mnt state=unmounted"

playbook使用与配置

参考:文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT_狂奔者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值