ansible-hoc 模块使用

文章目录

ansible模块

ansible工具

php 复制代码
1.ansible ****
    ansible ad-hoc 用来给指定主机清单设备推送单个任务,类似Linux的基础命令
    ansible [选项] 主机清单 -m 模块 -a '[模块参数]'
2.ansible-doc
    ansible模块的帮助文库
    -l 列出所有的模块
3.ansible-galaxy
    从ansible官方代码库下载成品(playbook)
4.ansible-playbook *****
    剧本,用来批量给指定主机清单推送多个任务,类似Linux的shell脚本

ansible代码验收

plain 复制代码
ansible的工具再执行过程中,通过输出代码的颜色来区分信息类别
绿色		正确信息,没有操作
黄色		正确信息,有操作
红色		报错信息,要仔细分析
紫色 	警告信息,可能会造成影响,也可以无影响

ansible-hoc 常用模块

使用ansible-hoc可以进行使用临时的命令,做简单的批量任务,多任务,复杂任务需要使用playbook进行使用

命令说明

语法:ansible 主机或组名字 -m 模块名字 -a '模块参数' ansible参数

主机和组 是在host文件中写好的

模块名字 可以使用ansible-doc -l查看模块,不写的时候是默认为command,这个默认的模块可以在ansible.cfg中修改,默认模块是不支持使用shell变量,特殊符号的

命令执行的模块
bash 复制代码
#command模块,加-a指定需要执行的命令就可以直接执行了,不能加变量和特殊符合
1、查看nfs主机当前的目录
[root@m01 ~]# ansible nfs -m command -a 'pwd'
nfs | CHANGED | rc=0 >>
/root

2、查看nfs主机目前的主机名字
[root@m01 ~]# ansible nfs -m command -a 'hostname'
nfs | CHANGED | rc=0 >>
nfs

#shell模块,语法和command差不多,不过是使用/bin/sh进行执行的,shell可以执行所有命令就像是在虚拟机一样
shell模块的ansible参数
 free_form: 需要执行的linux指令
 chdir: 执行之前先切换到指定的目录,默认是被管理节点的家目录,类似cd
 creates: 一个文件名 当文件存在就不执行
 removes: 一个文件名 当文件不存在就不执行

1、在nfs主机的/tmp目录中创建test文件
[root@m01 ~]# ansible nfs -m shell -a 'touch /tmp/test creates=test'
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If
you need to use command because file is insufficient you can add 'warn: false' to this
command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
nfs | CHANGED | rc=0 >>

[root@m01 ~]# ansible nfs -m shell -a 'ls /tmp/test'
nfs | CHANGED | rc=0 >>
/tmp/test


#raw模块,和shell用户是一样的,不同的是他没有ansible参数可以使用

script模块,可以使用进行脚本文件,并且不需要复制脚本文件到被控节点,可以使用本机的脚步远程执行
1、执行脚本,查询nfs的ip网卡信息和磁盘信息
#编辑脚本 给权限
[root@m01 test]# cat ip_df.sh
#!/bin/bash
ifconfig
df -Th
[root@m01 test]# chmod +x ip_df.sh

#使用shell模块执行,被控节点没有这个文件
[root@m01 test]# ansible nfs -m shell -a './test/ip_df.sh'
nfs | FAILED | rc=127 >>
/bin/sh: ./test/ip_df.sh: No such file or directorynon-zero return code

#使用script模块,正常执行
[root@m01 test]# ansible nfs -m script -a '/test/ip_df.sh'
nfs | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to nfs closed.\r\n",
    "stderr_lines": [
        "Shared connection to nfs closed."
    ],
常用模块练习
file模块练习

参数定义

owner:定义文件或目录的属主

group:定义文件或目录的组

mode:定义权限

src:要被链接的源文件,需要使用state=link的情况使用

dest:被链接的文件,需要使用state=link的情况使用

force:强制创建软链接

file:查看文件的状态

touch:创建不存在的文件

absent:删除文件、目录或取消链接文件 目录可以被递归删除

directory:创建目录 目录可以被递归创建

link:创建软连接

hard:创建硬链接

bash 复制代码
1、在/tmp,目录下创建test.txt 文件,指定权限是644 属组和属主是root
[root@m01 ~]# ansible nfs  -m file -a 'path=/tmp/test.txt state=touch mode=644 owner=root group=root'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "dest": "/tmp/test.txt",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "state": "file",
    "uid": 0
}

2、查看test.txt的状态
[root@m01 ~]# ansible nfs -m file -a 'path=/tmp/test.txt state=file'
nfs | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/tmp/test.txt",
    "size": 0,
    "state": "file",
    "uid": 0
}

3、创建test.txt的软连接到/root/test
[root@m01 ~]# ansible nfs -m file -a 'src=/tmp/test.txt dest=/root/test state=link'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "dest": "/root/test",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "size": 13,
    "src": "/tmp/test.txt",
    "state": "link",
    "uid": 0
}

4、在/tmp,目录下创建test1目录,指定权限是744 属组和属主是root
[root@m01 ~]# ansible nfs -m file -a 'path=/tmp/test1 mode=744 owner=root group=root state=directory'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0744",
    "owner": "root",
    "path": "/tmp/test1",
    "size": 6,
    "state": "directory",
    "uid": 0
}


5、在/tmp,目录下创建test1/abc/test2目录,指定权限是744 属组和属主是root
[root@m01 ~]# ansible nfs -m file -a 'path=/tmp/test1/abc/test2 mode=744 owner=root group=root state=directory'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0744",
    "owner": "root",
    "path": "/tmp/test1/abc/test2",
    "size": 6,
    "state": "directory",
    "uid": 0
}



6、删除test1目录 
[root@m01 ~]# ansible nfs -m file -a 'path=/tmp/test1/ state=absent'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "path": "/tmp/test1/",
    "state": "absent"
}
[root@m01 ~]#
copy模块练习

复制文件到远程主机

src:本地源文件路径,可以使用绝对路径或当前目录的相对路径,如果是目录会递归复制,路径的结尾如果是/,则只复制目录里面的内容,不加/则是包含目录的全部内容

dest:远程主机的路径 使用绝对路径

force:如果复制的内容远程主机有,但是内容不一样,模块设置为yes会强制覆盖,如果是no则只有不存在的时候复制,存在不复制

backup:覆盖之前将原文件备份 yes|no

content:可以代替src 指定设置的文件的值

others:所有file模块里的参数选项这里都可以使用

remote_src:复制远程主机的文件

bash 复制代码
1、将/test下的copy.TXT文件复制到nfs的/tmp下
[root@m01 test]# ansible nfs -m copy -a 'src=/test/copy.txt dest=/tmp'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "73325af89b3946573099e254dfbc8ea35cde2072",
    "dest": "/tmp/copy.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "51b0fa33360e53d54cad7d22c79c5814",
    "mode": "0644",
    "owner": "root",
    "size": 35,
    "src": "/root/.ansible/tmp/ansible-tmp-1765682359.38-33865-268993710447528/source",
    "state": "file",
    "uid": 0
}

2、将/test下的copy.TXT文件复制到nfs的/root,指定权限是644 属组和属主是root
[root@m01 test]# ansible nfs -m copy -a 'src=/test/copy.txt dest=/root owner=root mode=644 group=root'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "73325af89b3946573099e254dfbc8ea35cde2072",
    "dest": "/root/copy.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "51b0fa33360e53d54cad7d22c79c5814",
    "mode": "0644",
    "owner": "root",
    "size": 35,
    "src": "/root/.ansible/tmp/ansible-tmp-1765682416.62-34252-13625253724953/source",
    "state": "file",
    "uid": 0
}


3、将/test下的copy.TXT文件复制到nfs的/tmp  并且备份原文件
[root@m01 test]# ansible nfs -m copy -a 'src=/test/copy.txt dest=/tmp owner=root mode=644 group=root backup=yes'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "73325af89b3946573099e254dfbc8ea35cde2072",
    "dest": "/tmp/copy.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "51b0fa33360e53d54cad7d22c79c5814",
    "mode": "0644",
    "owner": "root",
    "size": 35,
    "src": "/root/.ansible/tmp/ansible-tmp-1765682474.77-34512-279332913259936/source",
    "state": "file",
    "uid": 0
}


4、在/tmp/copy.txt文件中写入just a test
[root@m01 test]# ansible nfs -m copy -a 'content="jusr a test" dest=/tmp/copy.txt'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "d6e8657a4c27d357c6e9f4330d586679e9e7e806",
    "dest": "/tmp/copy.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "c4f56579a02da4b9bd25abc12e555c50",
    "mode": "0644",
    "owner": "root",
    "size": 11,
    "src": "/root/.ansible/tmp/ansible-tmp-1765682615.44-35383-133423820636633/source",
    "state": "file",
    "uid": 0
}

[root@nfs tmp]# cat copy.txt
jusr a test
yum_repository模块练习

此模块是用于管理yum仓库的

file:配置文件的名字,不包含.repo

name:仓库名字

description:仓库描述信息

baseurl:yum的源地址

enabled:是否开启仓库 yes|no

gpgcheck:是否检查软件包的完整性 yes|no

gpgcheck:公钥地址

bash 复制代码
1、为nfs部署一个本地源
[root@m01 test]# ansible nfs -m yum_repository -a 'file=Baseos name=base baseurl=/tmp/txt enabled=yes gpgcheck=no description=baseos'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "repo": "base",
    "state": "present"
}
[root@m01 test]#
yum模块练习

使用yum管理软件包 安装

name 操作的软件包的名字 也可以给url地址或者rpm包的本地路径

state 状态 present安装 absent 卸载 latest最新版本

bash 复制代码
1、安装httpd
[root@m01 test]# ansible nfs -m yum -a 'name=httpd state=present'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
        "installed": [
            "httpd"
        ]
    },

2、卸载http
[root@m01 test]# ansible nfs -m yum -a 'name=httpd state=absent'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
        "removed": [
            "httpd"
        ]
    }
service模块练习

管理服务

name 服务名称

state:对服务的操作 started启动 stopped关闭 restarted重启 reloaded 重新加载

enabled:是否开启启动 yes|no

bash 复制代码
1、启动https服务,并且设置开启自启
[root@m01 test]# ansible nfs -m service -a 'name=httpd state=started enabled=yes'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started",
    "status": {
systemd模块练习

管理服务,和service两个选择一个使用即可

name 服务名称

state 对服务的操作 started启动 stopped关闭 restarted重启 reloaded 重新加载

enabled:是否开启启动 yes|no

daemon_reload 当配置文件有变化就重新加载服务

bash 复制代码
启动https服务
[root@m01 ~]# ansible nfs -m systemd -a 'name=httpd state=started'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "name": "httpd",
    "state": "started",
    "status": {
cron模块练习

name 任务的描述信息

state 状态 present创建 absent删除

user:使用哪个用户执行

day:日

hour:时

minute:分

month:月

weekday:周

job:要执行的任务

cron_file:指定的配置文件

bash 复制代码
1、星期二的12点使用root进行yum源清理

nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "yum"
    ]
}
[root@nfs cron.d]# crontab -l
*/5 * * * * /usr/sbin/chronyd -q 'server ntp.aliyun.com iburst' &> /dev/null
#Ansible: yum
0 12 * * 2 yum clean all

2、每2分钟向/tmp/test文件中写 test内容
[root@m01 ~]# ansible nfs -m cron -a 'minute=*/2 job="echo test >> /tmp/test" state=present'
[DEPRECATION WARNING]: The 'name' parameter will be required in future releases.. This
feature will be removed in version 2.12. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "yum",
        "None"
    ]
[root@nfs cron.d]# crontab -l
*/5 * * * * /usr/sbin/chronyd -q 'server ntp.aliyun.com iburst' &> /dev/null
#Ansible: yum
0 12 * * 2 yum clean all
#Ansible: None
*/2 * * * * echo test >> /tmp/test


删除定时任务
[root@m01 ~]# ansible nfs -m cron -a 'name=yum state=absent'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "None"
    ]
}
user模块练习

user模块是有useradd userdel usermod三个指令的

state:状态创建或者删除

name:用户名字

uid:用户uid

group 用户组 groups附加组

comment:用户描述信息

create_home:是否创建家目录 yes|no

home:指定用户家目录 和createhome一起使用

shell:用户的shell环境

password:指定密码,在指定的时候不要使用明文,可以先在外面将密码指定为加密字符串,命令为echo "123456" |openssl passwd -1 -salt $(</dev/urandom tr -dc '[:alnum:]'|head -c 32) -stdin echo后面接想设置的密码

remove:state是删除的时候 并且remove是yes的时候,就表示和家目录一起删除

bash 复制代码
1、使用jinyin密码创建一个jy用户,用户的uid是2000  附加组是root 创建家目录
[root@m01 ~]# ansible nfs -m user -a 'name=jy password="$1$PopVwHlf$U4bqzwgtXl1SY328NeZwg1" uid=2000  groups=root create_home=yes'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 2000,
    "groups": "root",
    "home": "/home/jy",
    "name": "jy",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 2000
}
[root@nfs mail]# id jy
uid=2000(jy) gid=2000(jy) groups=2000(jy),0(root)

2、删除jy用户
[root@m01 ~]# ansible nfs -m user -a 'name=jy remove=yes state=absent '
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "jy",
    "remove": true,
    "state": "absent"
}
group模块练习

group也是一样,有groupadd groupdel groupmod

gid指定组id

name 组名字

state 状态

bash 复制代码
1、创建jy组,gid是2000
[root@m01 ~]# ansible nfs -m group -a 'name=jy gid=2000 state=present'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "gid": 2000,
    "name": "jy",
    "state": "present",
    "system": false
}

2、删除组
[root@m01 ~]# ansible nfs -m group -a 'name=jy gid=2000 state=absent'
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "name": "jy",
    "state": "absent"
}
fetch模块练习

此模块和copy是相反的,是把被控端的文件复制到主控这边

src 被控节点的路径

dest:主控的路径 必须以/结尾

flat 默认是no 表示在主控这边是以被控的主机名的形式来组织目录结构的 yes则部署

bash 复制代码
1、把nfs的/etc/passwd文件复制到主控的tmp目录下,以被控的名字形式
[root@m01 ~]# ansible nfs -m fetch -a 'src=/etc/passwd dest=/tmp'
nfs | CHANGED => {
    "changed": true,
    "checksum": "f24b7ecfe7d407867a4f11cad220cbbf3d699cab",
    "dest": "/tmp/nfs/etc/passwd",
    "md5sum": "61811292e958ed0f7ee2fc53ad5f6650",
    "remote_checksum": "f24b7ecfe7d407867a4f11cad220cbbf3d699cab",
    "remote_md5sum": null
}

显示为:
[root@m01 nfs]# tree /tmp/nfs/
/tmp/nfs/
└── etc
    └── passwd

2、把nfs的/etc/passwd文件复制到主控的tmp目录下,不以被控的名字形式
[root@m01 tmp]# ansible nfs -m fetch -a 'src=/etc/passwd dest=/tmp/ flat=yes'
nfs | CHANGED => {
    "changed": true,
    "checksum": "f24b7ecfe7d407867a4f11cad220cbbf3d699cab",
    "dest": "/tmp/passwd",
    "md5sum": "61811292e958ed0f7ee2fc53ad5f6650",
    "remote_checksum": "f24b7ecfe7d407867a4f11cad220cbbf3d699cab",
    "remote_md5sum": null
}

显示为
[root@m01 tmp]# ls
passwd  vmware-root_772-2990547578  vmware-root_812-2957648972
[root@m01 tmp]#
get_url模块练习

主要是用于http ftp https服务器上进行下载文件,类似wget

url 下载的url

usl_password usl_username 用于需要进行用户密码验证的时候

dest 下载到哪里

mode:权限

owner:拥有人

group:拥有组

bash 复制代码
1、下载阿里云的镜像到/tmp/samba下  权限为0440
[root@m01 ~]# ansible nfs -m get_url -a 'url=https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/Packages/samba-4.9.1-8.el8.x86_64.rpm dest=/tmp/samba mode=0440'
unarchive模块练习

解压文件使用

src 指定压缩文件的路径

dest:解压的路径 解压到哪

remote_src yes文件会从主控复制到被控|no不复制直接在被控找压缩文件 默认是yes

owner 解压之后文件或目录的拥有人

group:解压之后文件或目录的拥有组

mode:解压之后的权限

creates 指定文件名字 文件存在就不解压

list_files,yes列出压缩文件|no不列出 默认是no

bash 复制代码
1、将打包的etc.tar.gz 解压缩到nfs的/tmp里面、
创建压缩文件
[root@m01 ~]# tar -czvf /root/etc.tar.gz /etc/*

[root@m01 ~]# ansible nfs -m unarchive -a 'src=/root/etc.tar.gz dest=/tmp/                                                                                                              '
nfs | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "dest": "/tmp/",
    "extract_results": {
        "cmd": [
            "/usr/bin/gtar",
            "--extract",
            "-C",
            "/tmp/",
            "-z",
            "-f",
            "/root/.ansible/tmp/ansible-tmp-1765686820.51-58152-194285481444295/source"
synchronize模块练习

使用rsync进行同步文件

src 要复制的文件路径 目录以/结尾就包含目录本身

dest:复制到哪里

archive 归档 默认就是开启的 yes|no

compress 是否压缩 yes|no

rsync_opts rsyncd的参数选项 例如-v -a -z等等

delete 删除不存在的文件 默认是no

dest_port 默认目录主机上的端口,默认是22 走ssh协议

mode:push或者pull push上传文件 pull下载 默认是push

bash 复制代码
#将/etc目录上传到nfs的/tmp目录下
[root@m01 ~]# ansible nfs -m synchronize -a 'src=/etc/ dest=/tmp/'
nfs | SUCCESS => {
    "changed": false,
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L /etc/ nfs:/tmp/",
    "msg": "",
    "rc": 0,
    "stdout_lines": []
}
相关推荐
_OP_CHEN7 小时前
【Linux系统编程】(十五)揭秘 Linux 环境变量:从底层原理到实战操作,一篇吃透命令行参数与全局变量!
linux·运维·操作系统·bash·进程·环境变量·命令行参数
南山nash7 小时前
Ansible安装使用详细教程
ansible
ice_bird7 小时前
Ansible 一键部署k8s1.28配置完整版
kubernetes·ansible
Lenyiin8 小时前
Linux 项目托管 `git`
linux·运维·服务器·git·lenyiin
Blossom.1188 小时前
基于时序大模型+强化学习的虚拟电厂储能调度系统:从负荷预测到收益最大化的实战闭环
运维·人工智能·python·决策树·机器学习·自动化·音视频
阿达_优阅达9 小时前
集成方案 | 通过 Xtract Universal,将 SAP 数据无缝接入 Power BI 与微软 Fabric
运维·microsoft·sap·fabric·theobald
网络小白不怕黑15 小时前
Docker容器网络:四大模式解析与自定义网络
运维·docker·容器
FeelTouch Labs19 小时前
Nginx核心架构设计
运维·前端·nginx
程序员zgh19 小时前
Linux系统常用命令集合
linux·运维·服务器·c语言·开发语言·c++