码迷,mamicode.com
首页 > 其他好文 > 详细

Ansible常用模块

时间:2021-01-08 11:31:37      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:rev   nec   执行命令   move   功能   result   默认   inux   form   

Ansible常用模块

ansible常用模块使用详解

ansible常用模块有:

  • ping
  • yum
  • template
  • copy
  • user
  • group
  • service
  • raw
  • command
  • shell
  • script

ansible常用模块rawcommandshell的区别:

  • shell模块调用的/bin/sh指令执行
  • command模块不是调用的shell的指令,所以没有bash的环境变量
  • raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了

1. ansible常用模块之ping

ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong

[root@node1 ~]# ansible node2 -m ping
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

2. ansible常用模块之command

command模块用于在远程主机上执行命令,ansible默认就是使用command模块。

command模块有一个缺陷就是不能使用管道符和重定向功能。

// 查看受控主机node2的selinux状态
[root@node1 ~]# ansible node2 -m command -a ‘getenforce‘
node2 | CHANGED | rc=0 >>
Enforcing

//不加-m直接-a,默认模块就command
[root@node1 ~]# ansible node2 -a ‘getenforce‘
node2 | CHANGED | rc=0 >>
Enforcing

//conmmand模块不支持管道符,不支持重定向
[root@node1 ~]# ansible node2 -a ‘ps -ef|grep root‘
node2 | FAILED | rc=1 >>
error: unsupported SysV option

Usage:
 ps [options]

 Try ‘ps --help <simple|list|output|threads|misc|all>‘
  or ‘ps --help <s|l|o|t|m|a>‘
 for additional help text.

For more details see ps(1).non-zero return code

3. ansible常用模块之raw

raw模块用于在远程主机上执行命令,其支持管道符与重定向

//支持管道符
[root@node1 ~]# ansible node2 -m raw -a ‘echo "hello world" > /tmp/test‘
node2 | CHANGED | rc=0 >>
Shared connection to node2 closed.

[root@node2 ~]# cat /tmp/test 
hello world

//支持管道符
[root@node1 ~]# ansible node2 -m raw -a ‘cat /tmp/test|grep hello‘
node2 | CHANGED | rc=0 >>
hello world
Shared connection to node2 closed.

4. ansible常用模块之shell

shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向

//支持重定向
[root@node1 ~]# ansible node2 -m shell -a ‘echo "hello world" > /tmp/abc‘
node2 | CHANGED | rc=0 >>

[root@node2 ~]# cat /tmp/abc 
hello world

//支持管道符
[root@node1 ~]# ansible node2 -m shell -a ‘cat /tmp/abc|grep world‘
node2 | CHANGED | rc=0 >>
hello world

5. ansible常用模块之script

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

//在主控机上写一个执行ip a的脚本
[root@node1 ~]# mkdir scripts
anaconda-ks.cfg  scripts
[root@node1 ~]# vim scripts/ip.sh

#!/bin/bash

ip a > /tmp/ip.txt

//执行脚本
[root@node1 ~]# ansible node2 -m script -a ‘/root/scripts/ip.sh‘
node2 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to node2 closed.\r\n",
    "stderr_lines": [
        "Shared connection to node2 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}

//验证受控主机node2上是否执行了ip a命令
[root@node2 ~]# cat /tmp/ip.txt 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:b0:97:d3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.153.11/24 brd 192.168.153.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever

6. ansible常用模块之template

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

//下载一个阿里云的centos8的源,这里我下载好了,也配置完毕了
[root@node1 ~]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# ls
CentOS-Base.repo   epel-playground.repo  epel-testing-modular.repo  redhat.repo
epel-modular.repo  epel.repo             epel-testing.repo

//将配置好的centos8传给受控主机node2
[root@node1 ~]# ansible node2 -m template -a ‘src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo‘
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "e2c5e733b29668ef82633e043e094108e934d4d3",
    "dest": "/etc/yum.repos.d/CentOS-Base.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "021e9bb5a28116f6b3fe608ddb806ebc",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 1683,
    "src": "/root/.ansible/tmp/ansible-tmp-1609949907.6690288-15385-250713221897521/source",
    "state": "file",
    "uid": 0
}
//在受控主机node2上查看一下
[root@node2 ~]# cd /etc/yum.repos.d/
[root@node2 yum.repos.d]# ls
CentOS-Base.repo  redhat.repo

7. ansible常用模块之yum

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

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

state常用的值:

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

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

//在node1主机上使用yum模块在受控机node2上安装vsftpd
[root@node1 ~]# ansible node2 -m yum -a ‘name=vsftpd state=present‘
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: vsftpd-3.0.3-32.el8.x86_64"
    ]
}

//查看受控机上是否安装了vsftpd
[root@node2 ~]# rpm -qa|grep vsftpd
vsftpd-3.0.3-32.el8.x86_64

9. ansible常用模块之copy

copy模块用于复制文件至远程受控机。


Ansible常用模块

标签:rev   nec   执行命令   move   功能   result   默认   inux   form   

原文地址:https://www.cnblogs.com/leixixi/p/14244245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!