zoukankan      html  css  js  c++  java
  • 【ansible】 笔记 (2)

    先使用 ansible-doc 获取帮助文档

    [root@localhost ~]# ansible-doc ping   
    > PING    (/usr/lib/python2.7/site-packages/ansible-2.3.0-py2.7.egg/ansible/modules/system/ping.py)
    
      A trivial test module, this module always returns `pong' on successful contact. It does not make sense in
      playbooks, but it is useful from `/usr/bin/ansible' to verify the ability to login and that a usable python is
      configured. This is NOT ICMP ping, this is just a trivial test module.
    
    EXAMPLES:
    # Test we can logon to 'webservers' and execute python with json lib.
    ansible webservers -m ping
    
    
    MAINTAINERS: Ansible Core Team, Michael DeHaan
    
    METADATA:
            Status: ['stableinterface']
            Version: 1.0
            Supported_by: core

    命令格式  Usage: ansible <host-pattern> [options]    

    • 显示 hosts 
    [root@localhost ~]# ansible all --list-hosts
      hosts (2):
        192.168.34.129
        192.168.34.130
    [root@localhost ~]# ansible webserver --list-hosts
      hosts (2):
        192.168.34.129
        192.168.34.130
    [root@localhost ~]# ansible 192.168.34.129:192.168.34.130 --list-hosts
      hosts (2):
        192.168.34.129
        192.168.34.130
    • 指定 inventory(hosts) 执行命令,使用 ansible -m 指定模块 -a 指定参数 -o 单行输出
    [root@localhost ~]# ansible  all -m ping                      
    192.168.34.130 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.34.129 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    [root@localhost ~]# ansible  webserver  -m ping    
    192.168.34.129 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.34.130 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    [root@localhost ~]# ansible  192.168.34.129  -m ping           
    192.168.34.129 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    [root@localhost ~]# ansible  192.168.34.129:192.168.34.130  -m ping 
    192.168.34.129 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.34.130 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    [root@localhost ~]# ansible  192.168.34.129:192.168.34.130  -m command -a uptime -o
    192.168.34.129 | SUCCESS | rc=0 | (stdout)  05:08:05 up  1:53,  2 users,  load average: 0.08, 0.13, 0.13
    192.168.34.130 | SUCCESS | rc=0 | (stdout)  05:08:04 up  1:54,  2 users,  load average: 0.14, 0.10, 0.09
    • -m 默认模块是 command,这里可以忽略。同时我们可以自己编写脚本指定“动态 inventory”,具体的写法请自行百度。这个脚本需要支持两个参数, --list/-l  和 --host/-H,并且返回 json 格式的数据。

    • 需要注意的是如果“被控机”开启了 selinux,则在使用 copy 模块时需要在“被控机”上安装  libselinux-python,才能保证正常执行 copy 模块
  • 相关阅读:
    jquery Flexigrid只选择一行,增加双击事件,获取数据库ID
    [工具库]JOJSONBuilder工具类——一键把多个bean对象数据转换为JSON格式数据
    java 观察者模式
    [工具库]JOXMLBuilder工具类——一键把多个bean对象数据转换为XML格式数据
    JVM原理
    WEB项目的分层结构
    一刻钟精通正则表达式
    [Java]Stack栈和Heap堆的区别(终结篇)[转]
    [java]二、八、十、十六进制之间的转换
    java 适配器模式
  • 原文地址:https://www.cnblogs.com/lxmhhy/p/6506296.html
Copyright © 2011-2022 走看看