ansibleベースコマンドの例

4855 ワード

参照先: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コア8 Gのようなサーバは最大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
    転載先:https://www.cnblogs.com/yitianyouyitian/p/9182846.html