zoukankan      html  css  js  c++  java
  • ansible基础命令实例

    参考: https://www.cnblogs.com/ilurker/p/6421624.html

    1. 使用自定义的hosts

      格式: ansible  组机匹配  -i  自定义的hosts  -m modulename  -a  "一些参数"

       ansible ios_cn:ios_tw  -i ./hosts   -m ping   

      ansible all  -i ./hosts   -m ping   #all表示应用所有的服务器

          #subprocess 调用ansible命令,没有shell=True要写成列表格式,并且第一个元素是一个可执行的文件       

      

      child=subprocess.Popen(["ansible","all","-i","./hosts","-m","command","-a","ls /mnt/"], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
      child.stdout.read()

      #带了shell=True,可以直接像在shell在执行命令一样书写要执行的命令

      child=subprocess.Popen(["ansible all  -i ./hosts -m command -a 'ls -alh /mnt/'"], stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)

      child=subprocess.Popen("ansible all  -i ./hosts -m command -a 'ls  /mnt/'", stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)

       ansible ios_tw -i ./hosts -a "df -lh"    #居然可以不选择模块,其实默认应该是选择的command模块

    hosts 自定义如下

    [ios_cn]
    112.124.13.182
    112.124.52.243
    121.199.29.73
    112.124.58.190
    121.41.20.136
    115.29.5.254
    114.215.199.83
    
    [ios_tw]
    121.40.20.132

    2. ansible支持主机列表的正则匹配

    • 全量: all/*

    • 逻辑或: :
    • 逻辑非: 
    • 逻辑与: 
    • 切片: []
    • 正则匹配: 以~开头
    ansible all -m ping  #所有默认inventory文件中的机器
    ansible "*" -m ping  #同上
    ansible 121.28.13.* -m  ping #所有122.28.13.X机器
    
    ansible  web1:web2  -m  ping  #所有属于组web1或属于web2的机器
    ansible  web1:!web2  -m  ping #属于组web1,但不属于web2的机器
    ansible  web1&web2  -m  ping  #属于组web1又属于web2的机器
    
    ansible webserver[0]  -m  ping    #属于组webserver的第1台机器
    ansible webserver[0:5]  -m  ping  #属于组webserver的第1到4台机器
    
    ansible "~(beta|web).example.(com|org)"  -m ping 

    3. 在远程机器上用一般用户执行命令

    例如: 用sudo实现, 远程机器/etc/sudoer配置如下    ubuntu  ALL=(ALL:ALL) ALL

    ansible all -i ./hosts --ask-pass -m ping -u ubuntu -b  --become-user=root  --ask-become-pass
    
    #说明,这里是用密码登录,并且登录过后sudo那里也要输入密码

    4. ansible 并发

    -f 参数指定线程数

    建议指定为cpu 核数的偶数倍。如4核8G的服务器建议最多20个线程. 

    4. ansible 模块使用

    ansible-doc yum  查看模块的使用

    在shell命令行的使用如下:

    ansible all  -m apt -a "name=nginx state=present"

    在yml文件中格式一般如下

    - - -
    
    - name: Install the package "foo"
      apt:
        name: foo
        state: present
    
    
    - name: This command will change the working directory to somedir/ and will only run when somedir/somelog.txt doesn't exist.
      shell: somescript.sh >> somelog.txt
      args:
        chdir: somedir/
        creates: somelog.txt

    3.ansible-pull

    4.ansible-playbook

    5. ansible-vault

    6.ansible-doc

    ansible-playbook sudo

    https://www.jianshu.com/p/50d2bf6a5b73

  • 相关阅读:
    cocos2d-x3.0 SpriteFrameCache
    POJ 2417 Discrete Logging 离散对数
    C++Vector使用方法
    UML时序图
    7个最好的免费杀毒软件下载
    iOS_17_控制开关_TabBarController_由storyboard道路
    里氏替换原则
    笔试题&面试题:输入一个维度,逆时针打印出一个指定矩阵
    __weak如何实现目标值自己主动设置nil的
    静态构造函数实际运行多少次?
  • 原文地址:https://www.cnblogs.com/yitianyouyitian/p/9182846.html
Copyright © 2011-2022 走看看