Ansible ad-hoc的应用

本文详细介绍了如何使用Ansible的ad-hoc命令管理远程主机,包括查看主机列表、测试连接、使用不同模块如ping、command、shell、script进行操作,以及处理常见的错误。此外,还涉及到了文件和目录管理、权限修改、文件拷贝与下载、文本内容修改等任务。最后,文章提到了用户管理、yum源配置、软件包安装与服务管理等高级应用。
摘要由CSDN通过智能技术生成

Ansible ad-hoc应用一

  • 测试主机列表中的主机是否可以ping通
  • 查看被管理主机的服务器信息(如时间、版本、内存等)
  • 学习ansible-doc命令的用法
  • 测试command与shell模块的区别
  • 使用script模块在远程主机执行脚本(装软件包、启服务)

[root@control ansible]# ansible-doc -l     #查看所有的模块

 

 

Ansible ad-hoc是一种通过命令行批量管理的方式,命令基本格式如下:

     格式:ansible 主机集合 -m 模块名 -a "参数"

查看主机列表

[root@control ~]# cd ~/ansible
[root@control ansible]# ansible all --list-hosts           # --list-hosts是ansible这个命令的固定选项,如同ls -a一样(-a是ls命令的固定选项)
  hosts (5):
    node1
    node2
    node3
    node4
    node5

 测试远程主机是否能ping通

当需要远程多个主机或者多个组时,中间使用逗号分隔

  1. [root@control ansible]# ansible node1 -m ping     #调用ping模块
  2. [root@control ansible]# ansible node1,webserver -m ping
  3. [root@control ansible]# ansible all -m ping          #测试所有模块(绿色表示成功)

模块就是脚本(多数为Python脚本),多数脚本都支持参数,默认模块为command

  1. [root@control ansible]# ansible node1 -m command -a "uptime"        #查看CPU负载
  2. [root@control ansible]# ansible node1 -m command -a "uname -r"      #查看内核版本
  3. [root@control ansible]# ansible node1 -a "ip a s"                          #查看网卡信息
  4. [root@control ansible]# ansible all -a "date"                                   #查看时间(所有机器的时间)

Shell模块

command和shell模块的区别,command不支持bash的特性。如管道和重定向等功能,但是shell模块可以支持,不可以使用shell模块执行交互命令,如vim、top等

  1. [root@control ansible]# ansible test -m command -a "ps | wc -l"      #报错           #wc -l  统计
  2. [root@control ansible]# ansible test -m command -a "ls &"          #报错
  3. [root@control ansible]# ansible test -m shell -a "ps aux | wc -l"      #进程数量
  4. [root@control ansible]# ansible test -m shell -a "who"                 #登陆信息
  5. [root@control ansible]# ansible test -m shell -a "touch /tmp/txt.txt"
  6.  [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.

    node1 | CHANGED | rc=0 >>

  7. #使用shell模块创建文件会有Warning警告提示,正常!!!

script模块 

script模块会把-a后面的脚本拷贝到被管理端主机,然后执行这个脚本

  1. [root@control ansible]# vim ~/ansible/test.sh
  2. #!/bin/bash
  3. dnf -y install httpd
  4. systemctl start httpd
  5. [root@control ansible]# ansible test -m script -a "./test.sh"     #test是主机组的名称,-m调用script模块,-a后面的./test.sh是上面创建脚本的相对路径和文件名,./是当前目录的意思,在当前目录下有个脚本叫test.sh

因为ansible远程的是node1,所以打开node1这台电脑,查看下是否安装了httpd软件、是否启动了服务

[root@node1 ~]# rpm -q httpd
          httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64

Ansible ad-hoc应用二

  • 远程目标主机新建文件和目录、修改文件或目录的权限
  • 在远程目标主机创建链接文件
  • 删除远程目标主机上的文件或目录
  • 将控制端本地的文件拷贝到被管理端
  • 从被管理端下
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值