• Ansible工具的初步使用


    概述

    Ansible工具的初步使用

    实践

    ansible的命令行使用

    # 模块 shell(执行shell命令)、copy、systemd
    # ansible [pattern] -i hosts
    # pattern:匹配执行的ansible节点、组名、主机名、all、多个组或主机用逗号合并
    # -i hosts: 指定hosts(每个人指定自己的hosts、不改默认,即定义plyabook规则的文件名) /etc/ansible/hosts(默认、一般不做修改)
    # -m:模块名
    # -a:模块参数
    # --diff 比较异同 --check 检测
    # ansible nodes -i hosts --list-hosts   --list-hosts:匹配哪些机器,打印出来
    # -f --fork 默认为5  指定最大进程数
    # -e "" 传入变量
    # [root@ks2p-hadoop04 ~]# man ansible  显示帮助文档
    # -v -vv -vvv -vvvv 打印ansible详细执行日志
    [root@ks2p-hadoop04 ansible]# ansible nodes -i hosts -m shell -a "ls /data/soft"
    ks2p-hadoop06 | CHANGED | rc=0 >>
    apache-kyuubi-1.8.0-bin
    flink-1.17.1
    kyuubi.tgz
    new.tgz
    ks2p-hadoop07 | CHANGED | rc=0 >>
    spark-3.0.2-bin-hadoop3.2.tgz
    spark-3.0.3-bin-hadoop3.2.tgz
    spark-3.1.2-bin-hadoop3.2.tgz
    [root@ks2p-hadoop04 ansible]# ansible nodes -i hosts -m copy -a "src=hosts dest=/data/soft/hosts"
    ks2p-hadoop06 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "checksum": "aaaffde09aa44b0e01eaa0559cd841f80a7eaa2b", 
        "dest": "/data/soft/hosts", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "38d63d2eca30455056de0116a850f327", 
        "mode": "0644", 
        "owner": "root", 
        "size": 179, 
        "src": "/root/.ansible/tmp/ansible-tmp-1713236123.87-49670-171904112008748/source", 
        "state": "file", 
        "uid": 0
    }
    ks2p-hadoop07 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "checksum": "aaaffde09aa44b0e01eaa0559cd841f80a7eaa2b", 
        "dest": "/data/soft/hosts", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "38d63d2eca30455056de0116a850f327", 
        "mode": "0644", 
        "owner": "root", 
        "size": 179, 
        "src": "/root/.ansible/tmp/ansible-tmp-1713236123.88-49672-160837999053406/source", 
        "state": "file", 
        "uid": 0
    }
    [root@ks2p-hadoop04 ansible]# ansible nodes -i hosts -m shell -a "ls /data/soft"
    ks2p-hadoop06 | CHANGED | rc=0 >>
    apache-kyuubi-1.8.0-bin
    flink-1.17.1
    hosts
    kyuubi.tgz
    new.tgz
    ks2p-hadoop07 | CHANGED | rc=0 >>
    hosts
    spark-3.0.2-bin-hadoop3.2.tgz
    spark-3.0.3-bin-hadoop3.2.tgz
    spark-3.1.2-bin-hadoop3.2.tgz
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    ansible-playbook

    ansible-playbook -i hosts test.yml

    # 剧本集
    
    - hosts: nodes
      gather_facts: false
      tasks:
        - name: 上传hosts文件
          copy:
            src: hosts
            dest: /data/soft
        - name: hosts存在将不执行
          shell: pwd
          args:
            chdir: /tmp
            creates: /data/soft/hosts # 该文件存在,则不执行
        - name: 打印调试信息
          debug:
            msg: "hello"
    
    - hosts: tbj
      gather_facts: false
      tasks:
        - name: 打印跳板机hostname
          shell: hostname
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    [root@ks2p-hadoop04 ansible]# ansible-playbook -i hosts test.yml
    
    PLAY [nodes] ***********************************************************************************************************************************************************************
    
    TASK [上传hosts文件] *******************************************************************************************************************************************************************
    changed: [ks2p-hadoop06]
    ok: [ks2p-hadoop07]
    
    TASK [hosts存在将不执行] *****************************************************************************************************************************************************************
    ok: [ks2p-hadoop06]
    ok: [ks2p-hadoop07]
    
    TASK [打印调试信息] **********************************************************************************************************************************************************************
    ok: [ks2p-hadoop06] => {
        "msg": "hello"
    }
    ok: [ks2p-hadoop07] => {
        "msg": "hello"
    }
    
    PLAY [tbj] *************************************************************************************************************************************************************************
    
    TASK [打印跳板机hostname] ***************************************************************************************************************************************************************
    changed: [ks2p-hadoop04]
    
    PLAY RECAP *************************************************************************************************************************************************************************
    ks2p-hadoop04              : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    ks2p-hadoop06              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    ks2p-hadoop07              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    官方文档

    Module Index

    playbooks

    roles

  • 相关阅读:
    大一作业HTML网页作业 HTML校园篮球网页作业(12个页面)
    Mac下使用Homebrew安装MySQL5.7
    (附源码)APP+springboot个人健康管理 毕业设计 202031
    【华为机试真题 JAVA】找朋友-100
    【历史上的今天】6 月 27 日:摩托罗拉创始人出生;微软发布 ASP.NET Core;靠计算机起家的美国大亨
    Vue(七)——Vue中的Ajax
    ThinkPHP框架使用工厂模式对接多个物流公司下单接口架构示例(php7.0及以上)
    x-cmd ai | x openai - 用于发送 openai API 请求,以及与 ChatGPT 对话
    《C++ Primer Plus》第十三章复习题和编程练习
    Linux基本指令(2)
  • 原文地址:https://blog.csdn.net/2301_79691134/article/details/137809677