• Ansible常用模块


    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.
    ",
        "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
    

    8. ansible常用模块之copy

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

    //把etc/ansible下的inventory复制到受控机的/etc/目录下
    [root@node1 ~]# ansible node2 -m copy -a 'src=/etc/ansible/inventory dest=/etc/'
    node2 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "checksum": "eb47e5d0491233653c6fd9e7f24b93e017cdcfc8",
        "dest": "/etc/inventory",
        "gid": 0,
        "group": "root",
        "md5sum": "ca2c1d13b3ec3280f4794b358cb4c3e3",
        "mode": "0644",
        "owner": "root",
        "secontext": "system_u:object_r:etc_t:s0",
        "size": 35,
        "src": "/root/.ansible/tmp/ansible-tmp-1609950746.4783509-15450-147868352435171/source",
        "state": "file",
        "uid": 0
    }
    
    //查看一下
    [root@node2 ~]# cat /etc/inventory 
    
    node1
    
    [webservers]
    node2        
    

    9. ansible常用模块之group

    group模块用于在受控机上添加或删除组。

    //在受控机上添加一个系统组,其gid为306,组名为mysql
    [root@node1 ~]# ansible node2 -m group -a 'name=mysql gid=666 state=present'
    node2 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "gid": 666,
        "name": "mysql",
        "state": "present",
        "system": false
    }
    [root@node2 ~]# grep mysql /etc/group
    mysql:x:666:
    

    10. ansible常用模块之user

    user模块用于管理受控机的用户帐号。

    //创建一个tom用户,uid为555,设置为不能登录/sbin/nologin,没有家目录
    [root@node1 ~]# ansible node2 -m user -a 'name=tom uid=666 system=true shell=/sbin/nologin create_home=false state=present'
    node2 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "comment": "",
        "create_home": false,
        "group": 665,
        "home": "/home/tom",
        "name": "tom",
        "shell": "/sbin/nologin",
        "state": "present",
        "system": true,
        "uid": 666
    }
    
    
    //查看一下
    [root@node2 ~]# id tom
    uid=666(tom) gid=665(tom) groups=665(tom)
    

    11. ansible常用模块之service

    service模块用于管理受控机上的服务。

    //下载httpd服务
    [root@node1 ~]# ansible node2 -m dnf -a 'name=httpd state=present'
    node2 | 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: apr-1.6.3-11.el8.x86_64",
            "Installed: centos-logos-httpd-80.5-2.el8.noarch",
            "Installed: apr-util-1.6.1-6.el8.x86_64",
            "Installed: mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86_64",
            "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
            "Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
            "Installed: httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64",
            "Installed: httpd-filesystem-2.4.37-30.module_el8.3.0+561+97fdbbcc.noarch",
            "Installed: httpd-tools-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64"
        ]
    }
    
    //启动http服务,设置开机自启
    [root@node1 ~]# ansible node2 -m service -a 'name=httpd enabled=true state=started'
    node2 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "enabled": true,
        "name": "httpd",
        "state": "started",
        "status": {
    
    //查看服务启动状态
    [root@node1 ~]# ansible node2 -m shell -a 'systemctl status httpd'
    node2 | CHANGED | rc=0 >>
    ● httpd.service - The Apache HTTP Server
       Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
       Active: active (running) since Thu 2021-01-07 00:54:31 CST; 1min 6s ago
         Docs: man:httpd.service(8)
     Main PID: 18156 (httpd)
       Status: "Running, listening on: port 80"
        Tasks: 213 (limit: 11300)
       Memory: 25.0M
       CGroup: /system.slice/httpd.service
               ├─18156 /usr/sbin/httpd -DFOREGROUND
               ├─18167 /usr/sbin/httpd -DFOREGROUND
               ├─18168 /usr/sbin/httpd -DFOREGROUND
               ├─18169 /usr/sbin/httpd -DFOREGROUND
               └─18170 /usr/sbin/httpd -DFOREGROUND
    
    Jan 07 00:54:21 node2 systemd[1]: Starting The Apache HTTP Server...
    Jan 07 00:54:31 node2 httpd[18156]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.153.11. Set the 'ServerName' directive globally to suppress this message
    Jan 07 00:54:31 node2 systemd[1]: Started The Apache HTTP Server.
    Jan 07 00:54:41 node2 httpd[18156]: Server configured, listening on: port 80
    
  • 相关阅读:
    QML键盘响应
    C++获取迭代器的下标
    C++虚函数指针
    QVariant转指针或其他非内建数据
    Signal QQmlEngine::quit() emitted, but no receivers connected to handle it quit
    旧服务器上源代码迁移到新服务器
    SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
    Rdlc报表出现空白页解决方法
    我所经历的企业中IT部门在企业内部的地位
    Config程序配置文件操作实践进阶之ConfigurationSectionGroup
  • 原文地址:https://www.cnblogs.com/leixixi/p/14244245.html
Copyright © 2020-2023  润新知