学习 ansible

学习 ansible

1.ansible简介

1> 什么是ansible

ansible是一款开源自动化平台,是一个配置管理工具,自动化运维工具

2> ansible的优点

  • 跨平台支持
  • 人类可读自动化: ansible提供linux,Windows,unix和网络设备的无代理支持,适用于物理、虚拟、云和容器环境
  • 完美描述应用:playbook
  • 轻松管理版本控制:playbook是纯文本,可视作源代码
  • 支持动态清单
  • 编排可与其他系统轻松集成:puppet、jenkins
  • 基础架构即代码
  • 减少人为错误

3> 安装ansible

在workstation上安装ansible,充当控制节点

dnf install ansible -y

查看ansible版本信息

# ansible --version

使用setup模块验证python 

2.部署ansible

1> 构建ansible清单 

什么是清单? --> 定义了ansible将要管理的一批主机

2> 静态清单

配置文件/etc/ansible/hosts(一台主机可以存在于多个主机组)

# vim /etc/ansible/hosts

查看

2> 定义嵌套组

3> 通过范围简化主机规格

可以指定主机名称或ip范围或者数字和字母范围

语法:[START:END]

172.25.[0:4].[0:254]    匹配172.25.0.0/24,172.25.1.0/24 ...

server[01:10].example.com    匹配server01.example.com到server20.example.com所有主机,此方式不匹配server1,之匹配server01

4> 验证清单

ansible all -i inventory --list-hosts 查看清单

# ansible dbservers -i inventory --list-hosts
# ansible webservers -i inventory --list-hosts

 

5>  默认清单位置:/etc/ansible/hosts

3.运行临时命令

1> 格式

ansible host-pattern -m module [-a 'module arguments'] [-i inventory]

2> 检查能否在受管主机上运行python模块

# ansible -m ping all

3> 使用临时命令通过模块执行任务

# ansible-doc -l  列出所有模块
# ansible-doc ping 查看ping模块的帮助文档

ansible模块

文件模块:

  • -copy:将本地文件复制到受管主机
  • -file:设置文件的权限和其他属性
  • -lineinfile:确保特定行是否在文件中
  • -synchronize:使用rsync同步内容

系统模块:

  • -firewalld:使用firewalld管理任意端口和服务
  • -reboot:重启
  • -service:管理服务
  • -user:添加、删除和管理用户账户

Net Tools模块 :

  • -get_url:通过http、https、或者ftp下载文件
  • -nmcli:管理网络
  • -uri:与web服务交互

4> 使用user模块确保westos用户存在于servera.lab.example.com且uid为1000

ansible -m user -a 'name=westos uid=1000 state=present' servera.lab.example.com

# ansible webservers -m command -a /usr/bin/hostname -o  
command模块允许执行远程命令,但这些命令不是shell处理,所以无法访问shell环境变量,所以不能执行重定向、传送等操作

使用command模块执行临时命令

# ansible localhost -m command -a 'id' -u westos 使用用户连接执行id命令,执行命令是需要在自定义目录中

使用copy模块

# ansible localhost -m copy -a 'content="hello westos\n" dest=/etc/motd' -u westos --become
以root身份去执行

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值