设为首页 加入收藏

TOP

ansible的常用模块(二)
2023-07-23 13:38:51 】 浏览:78
Tags:ansible 常用模
-rw-r--r-- 1 root root 43 Oct 23 19:00 test.sh //使用shell模块在受控机上执行受控机上的脚本 [root@ansible ~]# ansible 192.168.111.142 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test' 192.168.111.142 | CHANGED | rc=0 >> [root@ansible ~]# ansible 192.168.111.142 -m shell -a 'cat /tmp/test' 192.168.111.142 | CHANGED | rc=0 >> 123456 123 456

5.ansible常用模块之script

script模块用于在受控机上执行主控机上的脚本

[root@ansible ~]# ll /scripts/
total 4
-rw-r--r-- 1 root root 48 Oct 23 19:06 a.sh

[root@ansible ~]# ansible 192.168.111.142 -m script -a '/scripts/a.sh &> /tmp/a'
192.168.111.142 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.111.142 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.111.142 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}

//查看受控机上的/tmp/a文件内容
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'cat /tmp/a'
192.168.111.142 | CHANGED | rc=0 >>
123
456
hello world

6.ansible常用模块之template

template模块用于生成一个模板,并可将其传输至远程主机上。

[root@ansible ~]# ansible 192.168.111.142 -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/centos.repo'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8",
    "dest": "/etc/yum.repos.d/centos.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "ed031c350da2532e6a8d09a4d9b05278",
    "mode": "0644",
    "owner": "root",
    "size": 1653,
    "src": "/root/.ansible/tmp/ansible-tmp-1666530145.7581205-29420421110479/source",
    "state": "file",
    "uid": 0
}

//查看受控机上是否有163源
[root@localhost ~]# ls /etc/yum.repos.d/
centos.repo

7.ansible常用模块之yum

yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个

  • name:要管理的包名
  • state:要进行的操作

state常用的值:

  • latest:安装软件
  • installed:安装软件
  • present:安装软件
  • removed:卸载软件
  • absent:卸载软件

若想使用yum来管理软件,请确保受控机上的yum源无异常。

//在受控机上查询看vsftpd软件是否安装
[root@localhost ~]# rpm -qa|grep httpd
[root@localhost ~]# 

//在ansible主机上使用yum模块在受控机上安装httpd
[root@ansible ~]# ansible 192.168.111.142 -m yum -a 'name=httpd state=present'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: mailcap-2.1.48-3.el8.noarch",
        "Installed: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64",
        "Installed: apr-1.6.3-12.el8.x86_64",
        "Installed: httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch",
        "Installed: apr-util-1.6.1-6.el8.x86_64",
        "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
        "Installed: httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64",
        "Installed: centos-logos-httpd-85.8-2.el8.noarch",
        "Installed: m
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Linux性能监控(二)-top 下一篇Linux SAMBA 服务-cifs文件系统的..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目