2.ansible 命令模块 command shell script 文件模块 copy fetch file
ansible ad-hoc单个模块的执行过程
ansible命令语法:
ansible 主机信息(IP 主机组 all) -m (模块名)command -a "操作动作"
01 02 03 04 05 06
绿色 操作执行成功 没有对远程主机做任何改动
黄色 操作执行成功 对远程主机数据信息有改动
红色 操作失败
粉色 警告
蓝色 显示命令操作执行过程
1.command 在目标主机执行命令 默认模块 不支持管道符
ansible all -m command -a "hostname"
参数:
chdir 切换目录
ansible all -m command -a "chdir=/tmp pwd"
creates 判断文件是否存在 如果存在就跳过后面的操作
ansible 192.168.1.214 -m command -a "creates=/var/run/rsyncd.pid systemctl start rsyncd"
removes 判断文件是否不存在 不存在就跳过后面的操作
ansible 192.168.1.214 -m command -a "removes=/var/run/rsyncd.pid echo 11"
192.168.1.214 | CHANGED | rc=0 >>
11
2.shell 万能模块
ansible all -m shell -a "hostname && awk 'NR==3{print $1}' /etc/resolv.conf"
参数:
chdir 切换目录
ansible all -m shell -a "chdir=/tmp pwd"
creates 判断文件是否存在 如果存在就跳过后面的操作
ansible 192.168.1.214 -m shell -a "creates=/var/run/rsyncd.pid systemctl start rsyncd"
removes 判断文件是否不存在 不存在就跳过后面的操作
ansible 192.168.1.214 -m shell -a "removes=/var/run/rsyncd.pid echo 11"
192.168.1.214 | CHANGED | rc=0 >>
11
scripts 命令模块 执行脚本
第一个历程:编写脚本
[root@m01 scripts]# cat yum.sh
#!/bin/bash
yum install -y iftop
第二个历程:运行脚本
ansible backup -m script -a "/server/scripts/yum.sh"
copy 批量分发文件
作用:
1 分发文件数据信息 从管理端分发到被管理端
2 修改文件权限及属性
3 移动远程主机数据
src: 指定要推送的数据信息
dest: 指定保存文件的路径
ansible all -m copy -a 'src=/etc/hosts dest=/tmp'
owner: 文件属组信息
group: 文件属组信息
mode: 文件权限
backup 是否备份 备份会给源文件打时间戳
ansible all -m copy -a 'src=/etc/hosts dest=/root owner=nginx group=nginx mode=600 backup=yes'
[root@jenkins ~]# ll
-rw-------. 1 nginx nginx 279 10月 5 12:47 hosts
-rw-------. 1 nginx nginx 307 10月 5 12:46 hosts.19664.2022-10-05@12:47:56~
remote_src: 远程主机复制一份数据备份 cp /etc/hosts /tmp/hosts.bak
备份
ansible all -m copy -a 'src=/etc/hosts dest=/tmp/hosts.bak remote_src=yes'
还原
ansible all -m copy -a 'src=/tmp/hosts.bak dest=/etc/hosts remote_src=yes'
content: 直接编辑信息 并进行批量分发
ansible backupclient -m copy -a "content='oldboy123' dest=/etc/rsync.password mode=600"
fetch 批量拉取数据
ansible 192.168.1.203 -m fetch -a 'src=/etc/hosts dest=/tmp'
[root@rstx-53 scripts]# tree /tmp/192.168.1.203/
/tmp/192.168.1.203/
└── etc
└── hosts
src: 源文件
dest: 存放到本地的路径
file 修改远程主机文件权限
作用:
1 修改远程主机文件权限信息
2 创建远程主机数据信息/删除远程主机数据信息
path: 远程主机文件路径
owner: 属主
group: 属组
mode: 权限
ansible 192.168.1.203 -m file -a 'path=/tmp/hosts mode=777'
特殊用法
state
absent 删除数据信息
touch 创建文件
directory 创建目录
hard 硬链接
link 软连接
创建文件:
[root@rstx-53 ~]# ansible 192.168.1.201 -m file -a "path=/root/ansible.txt state=touch"
192.168.1.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/root/ansible.txt",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
创建目录:
[root@rstx-53 ~]# ansible 192.168.1.201 -m file -a "path=/root/ansible state=directory"
192.168.1.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/root/ansible",
"size": 6,
"state": "directory",
"uid": 0
}
[root@rstx-53 ~]# ansible 192.168.1.201 -m file -a "path=/root/ansible state=absent"
192.168.1.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/root/ansible",
"state": "absent"
}
创建链接文件:软连接/硬链接
创建硬链接:
[root@rstx-53 ~]# ansible 192.168.1.201 -m file -a "src=/etc/hosts path=/tmp/hosts state=hard"
192.168.1.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 158,
"src": "/etc/hosts",
"state": "hard",
"uid": 0
}
创建软链接
[root@rstx-53 ~]# ansible 192.168.1.201 -m file -a "src=/etc/hosts path=/tmp/hosts_link state=link"
192.168.1.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/hosts_link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 10,
"src": "/etc/hosts",
"state": "link",
"uid": 0
}
删除文件数据:
[root@rstx-53 ~]# ansible 192.168.1.201 -m file -a "path=/tmp/hosts state=absent"
192.168.1.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/tmp/hosts",
"state": "absent"
}