当前位置: 首页 > news >正文

Linux学习-Ansible(二)

基本配置
#主机清单文件
[root@harbor ansible]# cat hostlist
[web]
192.168.29.161
192.168.29.162
[es]
192.168.29.171
192.168.29.172
192.168.29.173
#查看所有被管理的主机
[root@harbor ansible]# ansible all --list-hostshosts (5):192.168.29.161192.168.29.162192.168.29.171192.168.29.172192.168.29.173
#查看web组中所有主机
[root@harbor ansible]# ansible web --list-hostshosts (2):192.168.29.161192.168.29.162
ansible远程管理方式
  • adhoc临时命令–>在命令行中执行管理命令
  • playbook剧本–>把管理任务用特定格式写到文件中
adhoc临时命令
  • 语法
ansible 主机或组列表 -m 模块 -a "参数"    # -a是可选的
ansible模块
#列出所有模块
[root@harbor ansible]# ansible-doc -l 
# 查看包含yum的模块名
[root@harbor ansible]# ansible-doc -l | grep yum
# 查看yum模块的帮助文档,按空格键向下翻页,按q退出
[root@harbor ansible]# ansible-doc yum
command模块
  • ansible默认模块,用于在远程主机上执行任意命令
  • command不支持shell特性,如管道、重定向
# 在所有被管主机上创建目录/tmp/demo
[root@harbor ansible]# ansible all -a "mkdir /tmp/demo"
# 查看we1的ip地址
[root@harbor ansible]# ansible web1 -a "ip a s"
[root@harbor ansible]# ansible web1 -a "ip a s | head"  # 报错
shell模块
  • 与command模块类似,但是支持shell特性,如管道、重定向
# 查看web1的ip地址,只显示前10行
[root@harbor ansible]# ansible web1 -m shell -a "ip a s | head"
script模块
# 在控制端创建脚本即可
[root@harbor ansible]# vim test.sh
#!/bin/bash
for user in user{1..5}
douseradd $userecho '123456' | passwd --stdin $user
done# 在webservers组的主机上执行脚本
[root@harbor ansible]# ansible webservers -m script -a "test.sh"
file模块
  • 可以创建文件、目录、链接等,还可以修改权限、属性等
  • 常用的选项:
    • path:指定文件路径
    • owner:设置文件所有者
    • group:设置文件所属组
    • state:状态。touch表示创建文件,directory表示创建目录,link表示创建软链接,absent表示删除
    • mode:设置权限
    • src:source的简写,源
    • dest:destination的简写,目标
#查看使用帮助
[root@harbor ansible]# ansible-doc file
... ...
EXAMPLES:- name: Change file ownership, group and permissions  # 忽略ansible.builtin.file:           # 模块名。以下是它的各种参数path: /etc/foo.conf           # 要修改的文件的路径owner: foo                    # 文件所有者group: foo                    # 文件的所有组mode: '0644'                  # 权限
... ...
# 在webservers组的主机上创建/tmp/file.txt
[root@harbor ansible]# ansible webservers -m file -a "path=/tmp/file.txt state=touch"   # touch是指如果文件不存在,则创建;如果存在则改变它的时间戳
# 在webservers组的主机上创建/tmp/demo目录
[root@harbor ansible]# ansible webservers -m file -a "path=/tmp/demo state=directory"
# 将webservers组的主机上/tmp/file.txt的属主改为sshd,属组改为adm,权限改为0777
[root@harbor ansible]# ansible webservers -m file -a "path=/tmp/file.txt owner=sshd group=adm mode='0777'"
[root@harbor ansible]# ansible webservers -a "ls -l /tmp/file.txt"
# 删除webservers组的主机上/tmp/file.txt
[root@harbor ansible]# ansible webservers -m file -a "path=/tmp/file.txt state=absent"    # absent英文缺席的、不存在的
# 删除webservers组的主机上/tmp/demo
[root@harbor ansible]# ansible webservers -m file -a "path=/tmp/demo state=absent"
# 在webservers组的主机上创建/etc/hosts的软链接,目标是/tmp/hosts.txt
[root@harbor ansible]# ansible webservers -m file -a "src=/etc/hosts dest=/tmp/hosts.txt state=link"
copy模块
  • 用于将文件从控制端拷贝到被控端
  • 常用选项:
    • src:源。控制端的文件路径
    • dest:目标。被控制端的文件路径
    • content:内容。需要写到文件中的内容
[root@harbor ansible]# echo "hello world" > hello.txt
# 将hello.txt拷贝到webservers主机的/root/
[root@harbor ansible]# ansible web-m copy -a "src=hello.txt dest=/root/"
# 在目标主机上创建/tmp/mytest.txt,内容是Hello World
[root@harbor ansible]# ansible web-m copy -a "content='Hello World' dest=/tmp/mytest.txt"
fetch模块
  • 与copy模块相反,copy是上传,fetch是下载
  • 常用选项:
    • src:源。被控制端的文件路径
    • dest:目标。控制端的文件路径
# 将webservers主机上的/etc/hostname下载到本地用户的家目录下
[root@harbor ansible]# ansible web -m fetch -a "src=/etc/hostname dest=~/"
#查看从远程主机上下载的文件
[root@harbor ansible]# tree /root
/root
├── 192.168.29.161
│   └── etc
│       └── hostname
├── 192.168.29.162
│   └── etc
│       └── hostname
lineinfile模块
  • 用于确保目标文件中有某一行内容
  • 常用选项:
    • path:待修改的文件路径
    • line:写入文件的一行内容
    • regexp:正则表达式,用于查找文件中的内容
#web组中的主机,/etc/issue中一定要有一行Hello World。如果该行不存在,则默认添加到文件结尾
[root@harbor ansible]# ansible web -m lineinfile -a "path=/etc/issue line='hello world'"
[root@web1 ~]# cat /etc/issue
\S
Kernel \r on an \m
hello world
# web组中的主机,把/etc/issue中有hello的行,替换成welcome to china
[root@harbor ansible]# ansible web -m lineinfile -a "path=/etc/issue line='welcome to china' regexp='hello'"
[root@web1 ~]# cat /etc/issue
\S
Kernel \r on an \m
welcome to china
replace模块
  • lineinfile会替换一行,replace可以替换关键词
  • 常用选项:
    • path:待修改的文件路径
    • replace:将正则表达式查到的内容,替换成replace的内容
    • regexp:正则表达式,用于查找文件中的内容
# 把web组中主机上/etc/issue文件中的china,替换成henan
[root@harbor ansible]# ansible web -m replace -a "path=/etc/issue replace='china' regexp='henan'"
[root@web1 ~]# cat /etc/issue
\S
Kernel \r on an \m
welcome to henan
user模块
  • 实现linux用户管理
  • 常用选项:
    • name:待创建的用户名
    • uid:用户ID
    • group:设置主组
    • groups:设置附加组
    • home:设置家目录
    • password:设置用户密码
    • state:状态。present表示创建,它是默认选项。absent表示删除
    • remove:删除家目录、邮箱等。值为yes或true都可以。
# 在web组中的主机上,创建edison用户
[root@harbor ansible]# ansible web -m user -a "name=edison"
[root@web1 ~]# cat /etc/passwd | grep edison
edison:x:1000:1000::/home/edison:/bin/bash
# 在web组中的主机上,创建dizzy用户,设置uid=1010,主组adm,附加组daemon,root,家目录/home/dizzy
[root@harbor ansible]# ansible web -m user -a "name=dizzy uid=1010 group=adm groups=daemon,root home=/home/dizzy"
# 设置water的密码是123456
# {{}}是固定格式,表示执行命令。password_hash是函数,sha512是加密算法,则password_hash函数将会把123456通过sha512加密变成zhangsan的密码
[root@harbor ansible]# ansible web -m user -a "name=water password={{'123456' | password_hash('sha512')}}"
#删除edison用户,不删除家目录
[root@harbor ansible]# ansible web -m user -a "name=edison state=absent"
[root@web1 ~]# ls /home
dizzy  edison  water
#删除water用户,同时删除家目录
[root@harbor ansible]# ansible web -m user -a "name=water state=absent remove=yes"
[root@web1 ~]# ls /home
dizzy  edison
group模块
  • 创建、删除组
  • 常用选项:
    • name:待创建的组名
    • gid:组的ID号
    • state:present表示创建,它是默认选项。absent表示删除
# 在web组中的主机上添加名为devops的组
[root@harbor ansible]# ansible web -m group -a "name=devops"
[root@web1 ~]# cat /etc/group | grep devops
devops:x:1000:
# 在web组中的主机上删除名为devops的组
[root@harbor ansible]# ansible web -m group -a "name=devops state=absent"

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 严重干扰的验证码识别系统源码分享
  • Pandas与Matplotlib:Python中的动态数据可视化
  • 非凸科技钻石赞助第四届Rust China Conf 2024
  • 【渗透测试】——DVWA靶场搭建
  • 【运维监控】系列文章汇总索引
  • 网络学习-eNSP配置VRRP
  • 第15-02章:理解Class类并获取Class实例
  • 食品加工废水处理设备工作原理
  • C51单片机-单按键输入识别,键盘消抖
  • 极狐GitLab CI/CD 作业一直处于等待状态,如何解决?
  • 用Qt 对接‌百度语音识别接口
  • Altium designer布线技巧
  • 在字节跳动干了3年网络安全工程师,网络工程师30岁以后还有企业要吗
  • 机器学习和深度学习存在显著区别
  • CentOS Stream 8 通过 Packstack 安装开源 OpenStack(V版)
  • Angularjs之国际化
  • Docker 笔记(2):Dockerfile
  • es的写入过程
  • JavaScript标准库系列——Math对象和Date对象(二)
  • Redis学习笔记 - pipline(流水线、管道)
  • spring security oauth2 password授权模式
  • UMLCHINA 首席专家潘加宇鼎力推荐
  • 彻底搞懂浏览器Event-loop
  • 计算机常识 - 收藏集 - 掘金
  • 利用jquery编写加法运算验证码
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 前端自动化解决方案
  • 如何使用 JavaScript 解析 URL
  • 写给高年级小学生看的《Bash 指南》
  • 智能合约Solidity教程-事件和日志(一)
  • hi-nginx-1.3.4编译安装
  • zabbix3.2监控linux磁盘IO
  • 容器镜像
  • 树莓派用上kodexplorer也能玩成私有网盘
  • ​【C语言】长篇详解,字符系列篇3-----strstr,strtok,strerror字符串函数的使用【图文详解​】
  • ​html.parser --- 简单的 HTML 和 XHTML 解析器​
  • #HarmonyOS:基础语法
  • (13):Silverlight 2 数据与通信之WebRequest
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (3)nginx 配置(nginx.conf)
  • (Bean工厂的后处理器入门)学习Spring的第七天
  • (C++哈希表01)
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (十)【Jmeter】线程(Threads(Users))之jp@gc - Stepping Thread Group (deprecated)
  • (四)linux文件内容查看
  • (四)图像的%2线性拉伸
  • (一)SvelteKit教程:hello world
  • (转)大型网站架构演变和知识体系
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • .Mobi域名介绍
  • .net 写了一个支持重试、熔断和超时策略的 HttpClient 实例池
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉
  • .net打印*三角形