Linux学习-Ansible(二)

基本配置
bash 复制代码
#主机清单文件
[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-hosts
  hosts (5):
    192.168.29.161
    192.168.29.162
    192.168.29.171
    192.168.29.172
    192.168.29.173
#查看web组中所有主机
[root@harbor ansible]# ansible web --list-hosts
  hosts (2):
    192.168.29.161
    192.168.29.162
ansible远程管理方式
  • adhoc临时命令-->在命令行中执行管理命令
  • playbook剧本-->把管理任务用特定格式写到文件中
adhoc临时命令
  • 语法
bash 复制代码
ansible 主机或组列表 -m 模块 -a "参数"    # -a是可选的
ansible模块
bash 复制代码
#列出所有模块
[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特性,如管道、重定向
bash 复制代码
# 在所有被管主机上创建目录/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特性,如管道、重定向
bash 复制代码
# 查看web1的ip地址,只显示前10行
[root@harbor ansible]# ansible web1 -m shell -a "ip a s | head"
script模块
bash 复制代码
# 在控制端创建脚本即可
[root@harbor ansible]# vim test.sh
#!/bin/bash
for user in user{1..5}
do
    useradd $user
    echo '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的简写,目标
bash 复制代码
#查看使用帮助
[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:内容。需要写到文件中的内容
bash 复制代码
[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:目标。控制端的文件路径
bash 复制代码
# 将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:正则表达式,用于查找文件中的内容
bash 复制代码
#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:正则表达式,用于查找文件中的内容
bash 复制代码
# 把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都可以。
bash 复制代码
# 在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表示删除
bash 复制代码
# 在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"
相关推荐
Everglowwwwww22 分钟前
【bug】通过lora方式微调sdxl inpainting踩坑
学习·计算机视觉·ai作画·stable diffusion·bug
B.-39 分钟前
Remix 学习 - @remix-run/react 中主要的 hooks
前端·javascript·学习·react.js·web
tuantuan_tech1 小时前
开放式耳机哪个好用?开放式耳机好还是入耳式耳机好?
大数据·学习·生活·旅游·智能硬件
小林熬夜学编程1 小时前
【Linux系统编程】第二十弹---进程优先级 && 命令行参数 && 环境变量
linux·运维·服务器·c语言·开发语言·算法
街 三 仔1 小时前
【LabVIEW学习篇 - 25】:JKI状态机
学习·labview
Spring-wind1 小时前
【linux】 ls命令
linux
Flying_Fish_roe1 小时前
linux-安全管理-文件系统安全
linux·运维·安全
DKPT1 小时前
数据结构之排序的基本概念
java·数据结构·笔记·学习·算法
小小工匠2 小时前
Linux - 探秘/proc/sys/net/ipv4/ip_local_port_range
linux·本地端口分配范围
CXDNW2 小时前
【Linux篇】TCP/IP协议(笔记)
linux·网络·网络协议·tcp/ip·计算机网络