53. ansible概述,模块 +nfs实践ansible

一,ansible概述

  1. Ansible基本概述

Ansible是一个IT自动化的配置管理工具,自动化主要体现在Ansible集成了丰富模块,丰富的功能组件,
可以通过一个命令行完成一系列的操作。进而能减少我们重复性的工作和维护成本,以提高工作的效率。

2.Ansible的功能

1)批量执行远程命令,可以对N多台主机同时进行命令的执行
2)批量配置软件服务,可以进行自动化的方式配置和管理服务。3)实现软件开发功能,jumpserver底层使用ansble来实现的自动化管理0
4)编排高级的IT任务,Ansible的playbook是一门编程语言,可以用来描绘一套IT架构。

3.Ansible的特点

1.容易学习,无代理模式,不像saltstack既要学服务端又要学习客户端,还要学习服务端与客户端之间的通讯协议
2.操作灵活,体现在Ansible有较多的模块,提供了丰富的功能,playbook则提供了类似于编程语言的复杂功能
3.简单易用,体现在Ansible —个命令可以完成很多事情
4.安全可靠,因为Ansible使用了SSH协议进行通汛,既稳定又安全
5.可移植性高,可以将写好的playbook拷贝至任意机器进行执行

介绍Ansible原理?
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,
ansible只提供一种框架。ansible这个框架主要包含以下功能:

(1)连接插件connection plugins:负责和被监控端事先通信;
(2)host inventory:操作主机清单;
(3)核心引擎:ansible
(4)借助与插件完成记录日志邮件等功能;
(5)Playbook:剧本执行多个任务时,非必须可以让节点一次性运行多个任务。

在这里插入图片描述

4.任务执行模式

  1. ad-hoc:这个模式支持执行单个模块,支持批量执行单条命令。
  2. playbook:可以把多个你想要执行的任务放到一个playbook当中,通过这多个任务可以完成一个总体的目标

在这里插入图片描述


准备工作

  • yum install ansible -y #安装
    cat ansible.cfg     #配置
    ansible.cfg [defaults] inventory = ./hosts host_key_checking=false #关闭指纹验证

  • vim hosts       #主机清配置

[webservers] 
172.16.1.7
172.16.1.8 
172.16.1.9 
[db] 
172.16.1.31
[...  ]

ssh-keygen -C 123@mail
#生成秘钥对 ssh-copy-id user@ip
#推送公钥 cat ~/.ssh/authorized_keys

#检验对端公钥信息
ansible webservers -m ping
执行ansible ad-hoc 测试 是否能与该清单定义的节点通讯

#配置文件查找的顺序

$ANSIBLE_CONFIG----》》查看当前环境变量 然后指向这个环境所在的ansible.cfg
2.当前目录下的ansible.cfg
3.当前用户家目录下的.ansible.cfg
4.查找/etc/ansible/ansible.cfg (最后匹配)
-i 目标主机列表可以指定任意位置的hosts,建议使用目录下的创建配置

二,Ansible Inventory 主机清单定义连接形式

1.基于密码的方式
[webservers]
172.16.1.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'

2.基于秘钥的方式(默认)
[webservers]
172.16.1.31

3.其他类写法 
[webservers]
web[1:99].oldboy.com

基于秘钥
[webservers]
172.16.1.31 hostname=nfs
172.16.1.7 hostname=web01
172.16.1.8 hostname=web02

[webservers]    (较多)
web01 ansible_ssh_host=172.16.1.7
web02 ansible_ssh_host=172.16.1.8

练习:
1.创建一个项目目录
2.在该项目下创建ansible.cfg、hosts
3.ansible --version 查看加载的是哪个配置文件
4.最后在该目录下执行ansible webservers -m ping -i /etc/ansible/hosts 检测

三,Ansible Ad-Hoc [playbook基础]

临时命令,执行完即结束,并不会保存。
场景:
比如查看多个机器的某个进程是否启动,将某个文件拷贝到多个机器中。

ansible webserbers -m 模块 -a ‘动作’
[root@manager ansible_adhoc]# ansible webservers -m command -a ‘df -h’

提示:
绿色:正常
黄色:被控端发生了更改
红色:报错


1. ad-hoc模块:

使用ansible-doc xxx模块 /EX查看示例

命令command(不支持管道)shell(偶尔用)
安装yumyum_repository
配置copyfile
启动systemdservice
挂载mount
定时cron
用户usergroup
防火墙selinuxfirewalld

2. 使用ad_hoc 实践安装nfs服务

创建项目目录/ansble_adhoc/
├── ansible.cfg
└── hosts

ansible.cfghosts
[defaults]inventory = ./hosts[webservers]
web1/172.16.1.7 ansible_ssh_host=172.16.1.7
web1/172.16.1.8 ansible_ssh_host=172.16.1.8
[client]
nfs/172.16.1.31 ansible_ssh_host=172.16.1.31

cd / 项目目录 / ----------》》》 安装yum—>>>配置copy—>>>初始化group、user、file—>>>启动systemd—>>>客户端测试mount


yum模块:   安装软件

name:   软件包名称
 state: present #安装a 、bsen #卸载
 latest   安装最新版
enablerepo:指定通过那个仓库下载
disablerepo:禁止从哪个仓库下载

示例一、安装当前最新的Apache软件,如果存在则不安装
ansible webservers -m yum -a "name=httpd state=present"

示例二、安装当前最新的Apache软件,通过epel仓库安装
ansible webservers -m yum -a "name=httpd state=present enablerepo=epel"

示例三、通过公网URL安装rpm软件
ansible webservers -m yum -a "name=https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.0-1.el7.x86_64.rpm state=present"

示例四、安装最新版本的Apache软件,如果存在则更新Apache
ansible webservers -m yum -a "name=httpd state=latest"

示例五、更新所有的软件包,但排除和kernel相关的
ansible webservers -m yum -a "name=* state=latest exclude=kernel"

示例六、删除Apache软件
ansible webservers -m yum -a "name=httpd state=absent"

group、user模块:创建用户组和用户

group:
  state: present、absent
  gid:指定gid

[root@manager ansible_adhoc]# ansible webservers -m group -a "name=www gid=666 state=present"

user:

name: 用户名称
 uid: uid
 group:指定组
 groups:指定附加组 append = yes
 shell: 指定登陆shell
 create_home: 创建用户家目录
 state
 present 创建
 absent 删除
 remove:移除用户相关的文件

[root@manager ansible_adhoc]# ansible webservers -m user -a "name=www uid=666 create_home=no shell=/no/login group=www state=present"

 copy模块:文件配置

copy: 拷贝当前目录下的文件到被控端指定的路径

src 要拷贝的文件(相对路径 绝对路径)
  dest 拷贝到目标主机的哪个路径下
  owner 设定文件的属主
  group 设定文件的数组
  mode 设定文件的权限
  backup 备份(只有有变化时候会做备份)
  content 往目标主机文件中增加内容(重定向)

[root@manager ansible_adhoc]# ansible webservers -m copy -a "src=./exports.j2 dest=/etc/exports owner=root group=root mode=644"
[root@manager ansible_adhoc]# ansible webservers -m copy -a "src=./exports.j2 dest=/etc/exports owner=root group=root mode=644 backup=yes"
[root@manager ansible_adhoc]#ansible webservers -m copy -a "content="http-test" dest=/tmp/1.txt"

nfs的配置 文件:
[root@manager ansible_adhoc]# vim ./exports.j2
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
[root@manager ansible_adhoc]# ansible webservers -m copy -a “src=./exports.j2 dest=/etc/exports owner=www group=www mode=644 backup=yes”

示例一、创建test用户、uid为555
[root@manager ansible_adhoc]# ansible webservers -m user -a "name=test uid=555"

示例二、移除test用户,同时移除家目录
[root@manager ansible_adhoc]# ansible webservers -m user -a "name=test uid=555 state=absent remove=yes"

#示例三、创建jsm用户,为其添加123作为登录密码,并且创建家目录
ansible localhost -m debug -a "msg={{ '123' | password_hash('sha512', 'salt') }}"

ansible webservers -m user -a 'name=jsm password="$6$salt$jkHSO0tOjmLW0S1NFlw5veSIDRAVsiQQMTrkOKy4xdCCLPNIsHhZkIRlzfzIvKyXeGdOfCBoW1wJZPLyQ9Qx/1" create_home=yes'

file模块: ( 创建文件或、目录、授权 )

path:指定被控端的路径
  state:
   touch
  directory
   link
   absent
   owner:属主 默认root
   group:属组 默认root
   mode:文件默认644、目录755
   recurse:递归授权

unarchive模块

dest:远程绝对路径,档案应该被解压缩
 exec:列出需要排除的目录和文件
 src:指定源
 creates:一个文件名,当它已经存在时,这个步骤将不会被运行。

将解压缩一个存档。
默认情况下,它将在解包之前将源文件从本地系统复制到目标。
设置remote_src=yes为解包目标上已经存在的档案。
常用选项:

systemd | service: 启动模块

name: 服务名称  nfs | httpd | nginx
state:
	started
	reloaded
	stopped
	restarted
 enabled:

yes 加入开机自启动
no 关闭开机自启动

案例:
启动nfs服务,并加入开机自启动
[root@manager ansible_adhoc]# ansible webservers -m systemd -a "name=nfs state=started enabled=yes"

停止nfs服务
[root@manager ansible_adhoc]# ansible webservers -m systemd -a "name=nfs-utils state=stopped enabled=yes"

挂载mount模块

src: 设备| nfs|磁盘| 光盘 /dev/sda1
 path: 被控端要挂载的目录 /data
 fstype: nfs,xfs
opts:
  ro,noauto
  defaults
 state:
  mounted:挂载设备,并加入开机自启动 ***
  present:写入fstab,不挂载
  absent:卸载设备,会清除/etc/fstab ***
  unmounted:卸载,不会清除/etc/fstab
  remounted:重新在挂载一次

showmount -e 172.16.1.xx

172.16.1.7 /data挂载到 172.16.1.31 /opt
172.16.1.8 /data挂载到 172.16.1.31 /mnt

[root@manager ansible_adhoc]# ansible client -m mount -a "src=172.16.1.7:/data path=/opt fstype=nfs opts=defaults state=mounted"
[root@manager ansible_adhoc]# ansible client -m mount -a "src=172.16.1.8:/data path=/mnt fstype=nfs opts=defaults state=mounted"

get_url 模块 (可以联网下载)

url:连接地址
  dest: 存放目标目录
  mode:权限


nfs_server_client.sh

ansible webservers -m yum -a "name=nfs-utils state=present"
ansible webservers -m copy -a "src=./exports.j2 dest=/etc/exports owner=root group=root mode=644"
ansible webservers -m group -a "name=www gid=666 state=present"
ansible webservers -m user -a "name=www uid=666 create_home=no shell=/no/login group=www state=present"
ansible webservers -m file -a "path=/data state=directory owner=www group=www mode=755 recurse=yes"
ansible webservers -m systemd -a "name=nfs-utils state=started enabled=yes"


挂载
ansible client -m mount -a "src=172.16.1.7:/data path=/opt fstype=nfs opts=defaults state=mounted"
ansible client -m mount -a "src=172.16.1.8:/data path=/mnt fstype=nfs opts=defaults state=mounted"

四,出现的问题:
无法通讯? 公钥没有推送到对应的节点
在这里插入图片描述
无法远程挂载:
systemctl status nfs
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
Active: inactive (dead)
①-------->>>>> (单个节点:systemctl start nfs ansible: ansible webservers -m systemd -a “name=nfs state=started enabled=yes”)
在查看nfs状态 systemctl status nfs
②exports配置错误导致的重启失败
其他正常挂载是夯主:systemctl status firewalld 防火墙可能没关

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值