ansible原理、インストール、各種モジュールの詳細

12188 ワード

ansibleって何?
.ansibleは新しく登場した自動化メンテナンスツールで、Pythonに基づいて開発され、多くのメンテナンスツール(puppet、cfengine、chef、func、fabric)の利点を集め、一括システム構成、一括プログラム配置、一括実行コマンドなどの機能を実現した.ansibleはモジュールベースで動作し、それ自体は一括配置の能力がありません.本当に一括配置があるのはansibleが実行するモジュールで、ansibleはフレームワークを提供するだけです.主に以下を含む:(1)、接続プラグインconnection plugins:責任と監視された側が通信を実現する;(2)、host inventory:操作するホストを指定し、プロファイルに監視を定義するホストである.(3)、各種モジュールコアモジュール、commandモジュール、カスタムモジュール;(4)、プラグインによってログメールを記録するなどの機能を完成する.(5)、playbook:シナリオが複数のタスクを実行する場合、ノードに複数のタスクを一度に実行させる必要はありません.
ansible特性
1.    ,        Ansible  ,          ;

2.    SSH         ;
3.           ,           ;
4.    、    、    ;
5.  API      ,   Python    ;
6.  Playbooks        、    ;
7.   ,        agent,   ,               ;
8.   ,      1  n     ,          

Ansibleアーキテクチャと動作原理
Ansible:Ansible    。
HostInventory:   Ansible       ,    、  、ip 。
Playbooks:“  ”YAML    ,            ,                  。
CoreModules:    ,                    。
CustomModules:     ,             ,      。
ConnectionPlugins:    ,Ansible Host    

ansibleタスク実行モード
Ansibleシステムの制御ホストによる被管理ノードの操作方式は、adhocとplaybookの2つに分けられる.
ad-hoc  (     )
        ,          。ad-hoc               ,            。    bash     shell。
playbook  (    )
   Ansible      ,  Ansible         。playbook    task        , Web       、            。      playbook         ad-hoc       。

ansible実行フロー
簡単に理解すると、Ansibleは実行時に、まずansible.cfgの構成を読み出し、ルールに従ってInventoryの管理ホストのリストを取得し、これらのホストで構成を実行するタスクを並列に実行し、最後に実行の結果を待つ.
ansibleコマンド実行プロセス
1.自分のプロファイルをロードし、デフォルト/etc/ansible/ansible.cfg;2.対応するホストプロファイルを検索し、実行するホストまたはグループを見つける.3.commandなどの自分の対応するモジュールファイルをロードします.4.ansibleでモジュールまたはコマンドを生成し、対応する一時pyファイル(pythonスクリプト)をリモートサーバに転送する.5.実行ユーザのホームディレクトリに対応する.ansible/tmp/XXX/XX.PYファイル;6.ファイル+xに実行権限を与える.7.実行して結果を返す.8.一時pyファイルを削除し、sleep 0が終了する.
市場でよく使われる3種類の自動化編成ツール
Ansible:sshプロトコルに基づいてエージェントを必要とせず、中小規模アプリケーションシーンに適しているSaltstack:agentエージェントソフトウェア(より効率的に実行)が必要Puppet:ruby、機能が強く、構成が複雑で、超大型環境に適している
ansible環境導入
マスタ端子:192.168.136.1167被制御端子01:192.168.136.1168被制御端子02:192.168.136.1185
#          
 [root@localhost ~]# systemctl stop firewalld.service 
 [root@localhost ~]# setenforce 0

#     ansible
yum install -y epel-release  //  epel 
yum install ansible -y

ansible --version          //  ansible  
ansible 2.9.3
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

yum install tree -y
tree /etc/ansible/      //         
/etc/ansible/
├── ansible.cfg    #ansible     
├── hosts         #ansible    ,                  
└── roles     #

cd /etc/ansible
vim hosts       //      
[webserver]
192.168.136.168
[mysql]
192.168.136.185

#    
ssh-keygen -t rsa
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/idrsa):  #  
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):  #    
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/idrsa.
Your public key has been saved in /root/.ssh/idrsa.pub.
The key fingerprint is:
SHA256:QnRuJjR10Jy6HuyQxQz3ccWML8iHCdQ1HZx5ba57Ak0 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|      +o==.ooBo+.|
|     o.+o. o.B +|
|      o=+=  . + |
|     . +=  o E .|
|      .+S. . + . |
|      o.+   . o  |
|       + .   . . |
|        o     o .|
|               o |
+----[SHA256]-----+

#        
ssh-copy-id [email protected]
ssh-copy-id [email protected]    //       

#            
[root@localhost ~]# ansible 192.168.136.168 -m command -a 'date'
Enter passphrase for key '/root/.ssh/idrsa': 
192.168.136.168 | CHANGED | rc=0 >>
Sun Feb  9 09:02:44 CST 2020

[root@localhost ~]# ansible mysql -m command -a 'date'
Enter passphrase for key '/root/.ssh/idrsa': 
192.168.136.185 | CHANGED | rc=0 >>
Sun Feb  9 09:03:11 CST 2020

#   
[root@localhost ~]# ssh-agent bash #ssh  
[root@localhost ~]# ssh-add #    

[root@localhost ~]# ansible webserver -m command -a 'date'
192.168.136.168 | CHANGED | rc=0 >>
Sun Feb  9 09:05:08 CST 2020

------ansibleコマンドラインモジュール------
-----commandモジュール-----
    :ansible [  ] [-m   ] [-a args]
ansible-doc -l     //            : q  
ansible-doc -s yum   //-s  yum           
#ansible    ,all:      (      ),-a+''     
[root@localhost ~]# ansible all -a 'date'
192.168.136.185 | CHANGED | rc=0 >>
Sun Feb  9 09:16:22 CST 2020

192.168.136.168 | CHANGED | rc=0 >>
Sun Feb  9 09:16:22 CST 2020

ansible 192.168.80.182 -m command -a 'date'  //  ip  date
ansible webserver -m command -a 'date'       //      date
ansible mysql -m command -a 'date'       
ansible all -m command -a 'date'        //  hosts    date  
ansible all -a 'ls /'          -m  ,     command  

------cronモジュール------
    (state):present    (    ),absent    。
ansible-doc -s cron      //  cron    
#       ,job:  ,echo  heihei,name:  
ansible webserver -m cron -a 'minute="/1" job="/bin/echo heihei" name="test cron job"'
192.168.136.168 | CHANGED => {
    "ansiblefacts": {
        "discoveredinterpreterpython": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron job"
    ]
}
#          
[root@localhost ~]# ansible webserver -a 'crontab -l'
192.168.136.168 | CHANGED | rc=0 >>
#Ansible: test cron job
/1     /usr/bin/echo heihei

ansible webserver -a 'crontab -l'
ansible webserver -m cron -a 'name="test cron job" state=absent'    //      ,            ,name=None  

------userモジュール------
user       useradd, userdel, usermod    
ansible-doc -s user
ansible all -m user -a 'name="test01"'    //    test01
192.168.136.185 | CHANGED => {
    "ansiblefacts": {
        "discoveredinterpreterpython": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 1001, 
    "home": "/home/test01", 
    "name": "test01", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1001
}
192.168.136.168 | CHANGED => {
    "ansiblefacts": {
        "discoveredinterpreterpython": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 1001, 
    "home": "/home/test01", 
    "name": "test01", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1001
}

ansible mysql -m command -a 'tail /etc/passwd'
ansible webserver -m user -a 'name="test01" state=absent'    //    test01

------groupモジュール------
group      groupadd, groupdel, groupmod     。
ansible-doc -s group
ansible mysql -m group -a 'name=mysql gid=306 system=yes'

ansible mysql -a 'tail /etc/group'
[root@localhost ~]# ansible mysql -a 'tail /etc/group'
192.168.136.185 | CHANGED | rc=0 >>
slocate:x:21:
postdrop:x:90:
postfix:x:89:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
chen:x:1000:
mysql:x:306:
test01:x:1001:

ansible mysql -m user -a 'name=test02 uid=306 system=yes group=mysql'
ansible mysql -a 'tail /etc/passwd'

ansible mysql -a 'id test02'    
192.168.136.185 | CHANGED | rc=0 >>
uid=306(test02) gid=306(mysql) groups=306(mysql)

------copyモジュール------
ansible-doc -s copy
#src ,dest  ,owner:      
ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640'
ansible mysql -a 'ls -l /opt'
192.168.136.185 | CHANGED | rc=0 >>
total 4
-rw-r-----. 1 root root 541 Feb  9 09:44 fstab.back
drwxr-xr-x. 2 root root   6 Mar 26  2015 rh

ansible mysql -a 'cat /opt/fstab.back'

#contest:    ,       
ansible mysql -m copy -a 'content="hello heihei!"
dest=/opt/fstab.back'  // hello heihei!  /opt/fstab.back
ansible mysql -a 'cat /opt/fstab.back' 
192.168.136.185 | CHANGED | rc=0 >>
hello heihei!

------fileモジュール------
ansible-doc -s file
ansible mysql -m user -a 'name=mysql system=yes'
ansible mysql -m group -a 'name=mysql system=yes'
#path:      
ansible mysql -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back'        //            
ansible mysql -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link'      //  /opt/fstab.link /opt/fstab.back     
ansible mysql -m file -a "path=/opt/fstab.back state=absent"               //      
ansible mysql -m file -a "path=/opt/test state=touch"                   

-----ping  -------
ansible all -m ping
192.168.136.185 | SUCCESS => {
    "ansiblefacts": {
        "discoveredinterpreterpython": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.136.168 | SUCCESS => {
    "ansiblefacts": {
        "discoveredinterpreterpython": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

------サービスモジュール------
ansible-doc -s service
[root@ab ~]# yum install -y httpd
[root@aa ~]# ansible webserver -a 'systemctl status httpd'        //  web   httpd    
 ansible webserver -m service -a 'enabled=true name=httpd state=started'  #   stop
192.168.136.185 | CHANGED => {
    "ansiblefacts": {
        "discoveredinterpreterpython": "/usr/bin/python"
    }, 
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started", 
    "status": {
        "ActiveEnterTimestampMonotonic": "0", 
        "ActiveExitTimestampMonotonic": "0", 
        "ActiveState": "inactive", 

      //  httpd  
[root@ab ~]# systemctl status httpd         //      

------shell  -----
ansible-doc -s shell
[root@localhost ~]# ansible webserver -m shell -a 'echo abc123|passwd --stdin chen'
192.168.136.168 | CHANGED | rc=0 >>
Changing password for user chen.
passwd: all authentication tokens updated successfully.
       //                  

------scriptモジュール------
#                        
ansible-doc -s script
vi test.sh
#!/bin/bash
echo "hello ansible from script"> /opt/script.txt

chmod +x test.sh
ansible mysql -m script -a 'test.sh'

[root@localhost ~]# ansible mysql -a 'cat /opt/script.txt'
192.168.136.185 | CHANGED | rc=0 >>
hello ansible from script

-----yumモジュール-----
ansible-doc -s yum
ansible mysql -m yum -a 'name=httpd'           //yum  httpd
192.168.136.185 | CHANGED => {
    "ansiblefacts": {
        "discoveredinterpreterpython": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "httpd"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [

[root@ac ~]# rpm -q httpd

ansible mysql -m yum -a 'name=httpd state=absent'     //  zsh
[root@ac ~]# rpm -q httpd

------setupモジュール------
ansible-doc -s setup
ansible mysql -m setup           //  mysql    facts  
***