Ansible与Shell结合使用

利用Shell脚本运行Ansible命令:

本实验采用rhel8.3。这次将使用 yum_repository 模块远程写入仓库配置。

写入仓库:

Shell脚本:

#!/bin/bash

ansible dev -m yum_repository -a 'name=myBase description="myTestRepo \
baseurl="/mnt/BaseOS" gpgcheck=no enabled=no'
ansible dev -m yum_repository -a 'name=myStream description="myTestRepo \
baseurl="/mnt/AppStream" gpgcheck=no enabled=no'

若不知道该模块的具体用请浏览Ansible快速上手
接着我们修改权限:

 chmod +x adhoc.sh

在这里插入图片描述
执行结果:

[root@Controller ansible]# ./adhoc.sh
192.168.110.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "myBase",
    "state": "present"
}
192.168.110.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "myStream",
    "state": "present"
}

在对端主机检查:

[root@Controller ansible]# ansible dev -m shell -a 'ls -l /etc/yum.repos.d'
192.168.110.129 | CHANGED | rc=0 >>
total 20
-rw-r--r--. 1 root root  75 Jun 29 10:03 myBase.repo #这里
-rw-r--r--. 1 root root  80 Jun 29 10:03 myStream.repo #这里
-rw-r--r--. 1 root root 358 Apr  9  2021 redhat.repo
-rw-r--r--. 1 root root 113 Mar  5 15:12 rpm.repo

用户管理:

Shell脚本:

#!/bin/bash
ansible dev -m user -a 'name="harry" groups="root" shell="/sbin/nologin"'

执行结果:

[root@Controller ansible]# ./users.sh
192.168.110.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1006,
    "groups": "root",
    "home": "/home/harry",
    "name": "harry",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": false,
    "uid": 1006
}

检查:

[root@Controller ansible]# ansible dev -m shell -a 'id harry'
192.168.110.129 | CHANGED | rc=0 >>
uid=1006(harry) gid=1006(harry) groups=1006(harry),0(root)

到此Shell运行Ansible命令就基本掌握了。后续自己可以尝试更多模块的使用。文章若有问题请联系更正——wgq3135@163.com

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值