自動化メンテナンスツールansible基礎応用


ansibleは自動化メンテナンスツールで、Pythonに基づいて開発され、多くのメンテナンスツール(puppet、cfengine、chef、func、fabric)の利点を集め、ロットシステム構成、ロットプログラム配置、ロット実行コマンドなどの機能を実現した.
ansibleはモジュールベースで動作し、一括配置の能力はありません.本当に一括配置があるのはansibleが実行するモジュールで、ansibleはフレームワークを提供するだけです.主な内容は次のとおりです.
1、接続プラグインconnection plugins:担当者と被監視者が通信する
2、host inventory:操作するホストを指定し、プロファイルの中で定義監視を売るホストである.
3、各種コアモジュール、commandモジュール、カスタムモジュール
4、プラグインによって記録ログメールなどの機能を完成する
5、playbook:slaveノードに一度に複数のタスクを実行させる
ansibleの特性:
1、モジュール化:特定のモジュールを呼び出し、特定の任務を完成する
2、python言語に基づいて実現し、Paramiko、PyYAMLとjinjaの3つのキーモジュールがある
3、配置が簡単
4、カスタムモジュールのサポート
5、playbookサポート
   
環境の準備
ホスト名
             IP
          localhost(ansiblemaster)
          10.10.86.56
           node1(ansibleslave)
          10.10.73.148
           bogon(ansibleslave)
          10.10.73.149
一、ansibleの取り付け
[root@localhost ~]# yum install ansible
##    :/etc/ansible/ansible.cfg
##    :/etc/ansible/hosts
##   :ansible、ansible-doc、ansible-playbook

二、ansible秘密鍵なしssh上陸
root@localhost ~]# ssh-keygen -t rsa -P ''
[root@localhost ~]# ssh-copy-id -i [email protected]
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.

[root@localhost ~]# ssh-copy-id -i [email protected]
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
##         slave  ,       (yes/no)

三、ホストグループのカスタマイズ
[root@localhost ~]# vim /etc/ansible/hosts
[ansible_agent]

10.10.73.148  ##port  

10.10.73.149  ##port
##  slave ssh       22,            

四、簡単なテスト
ansibleの共通モジュールリストの取得
[root@localhost ~]# ansible-doc -l

モジュールの使用方法の取得
[root@localhost ~]# ansible-doc -s COMMAND
##COMMAND:yum、cron、shell、setup、copy......

 1、ping
[root@localhost ~]# ansible ansible_agent -m ping    ##             
10.10.73.149 | SUCCESS => { 
    "changed": false, 
    "ping": "pong"
}
10.10.73.148 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

2、setupモジュール
[root@localhost ~]# ansible ansible_agent -m "setup"   ##           

3、commandモジュール
[root@localhost ~]# ansible ansible_agent -m shell -a "ls /tmp"
10.10.73.149 | SUCCESS | rc=0 >>
ansible_Z1xG26
wtc

10.10.73.148 | SUCCESS | rc=0 >>
ansible_6SQ3_D
report.sh
wtc.txt

4、shellモジュール
[root@localhost ~]# ansible ansible_agent -m shell -a "echo wxpp | passwd --stdin wtc"
10.10.73.148 | SUCCESS | rc=0 >>
Changing password for user wtc.
passwd: all authentication tokens updated successfully.

10.10.73.149 | SUCCESS | rc=0 >>
     wtc     。
passwd:                。

###ansible        ,     shell  ;      shell  ,     shell  

5、copyモジュール
  (1)src=  dest=
  (2)content=  dest=
(3)owner:所有者を示す
(4)group:配列を示す
(5)mode:権限の指定
[root@localhost tmp]# ansible ansible_agent -m copy -a "src=/tmp/wxpp.txt  dest=/tmp mode=665"  ##     "wxpp.txt"        
10.10.73.148 | SUCCESS => {
    "changed": true, 
    "checksum": "7641dc777dc18a1c2dfa3429aa8009c12c566913", 
    "dest": "/tmp/wxpp.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0665", 
    "owner": "root", 
    "path": "/tmp/wxpp.txt", 
    "size": 36, 
    "state": "file", 
    "uid": 0
}
10.10.73.149 | SUCCESS => {
    "changed": true, 
    "checksum": "7641dc777dc18a1c2dfa3429aa8009c12c566913", 
    "dest": "/tmp/wxpp.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0665", 
    "owner": "root", 
    "path": "/tmp/wxpp.txt", 
    "size": 36, 
    "state": "file", 
    "uid": 0
}
[root@localhost tmp]# ansible ansible_agent -m shell -a "cat /tmp/wxpp.txt"   ##             
10.10.73.149 | SUCCESS | rc=0 >>
wtc sent a bouquet of roses to wxpp

10.10.73.148 | SUCCESS | rc=0 >>
wtc sent a bouquet of roses to wxpp
[root@localhost tmp]# ansible ansible_agent -m copy -a "content='
Hello World' dest=/tmp/wxpp.txt" 10.10.73.149 | SUCCESS => {     "changed": true, } 10.10.73.148 | SUCCESS => {     "changed": true, }  [root@localhost tmp]# ansible ansible_agent -m shell -a "cat /tmp/wxpp.txt" 10.10.73.149 | SUCCESS | rc=0 >> Hello World 10.10.73.148 | SUCCESS | rc=0 >> Hello World ##content ,

6、cronモジュール
  (1)month= 
  (2)day=
  (3)hour=
  (4)weekday=
  (5)minute=
(6)job=##実行命令が何であるかを示す
(7)name=##タイミングタスクの説明を示す
(8)state##はステータスを指定し、prsentはタイミングタスクの追加を表し、デフォルト値でもある.absentはタイミングタスクの削除を表します
(9)user##は、そのユーザとして実行することを示す
[root@localhost ~]# ansible ansible_agent -m shell -a "ls /var/spool/cron/"
10.10.73.149 | SUCCESS | rc=0 >>
root

10.10.73.148 | SUCCESS | rc=0 >>
root
[root@localhost ~]# ansible ansible_agent -m cron -a "minute='*/1' job='/usr/sbin/ntpdate 10.10.86.56 &> /dev/null' user=wtc name='Wtc Job'"
10.10.73.149 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "Wtc Job"
    ]
}
10.10.73.148 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "Wtc Job"
    ]
}
[root@localhost ~]# ansible ansible_agent -m shell -a "cat /var/spool/cron/wtc"
10.10.73.149 | SUCCESS | rc=0 >>
#Ansible: Wtc Job
*/1 * * * * /usr/sbin/ntpdate 10.10.86.56 &> /dev/null

10.10.73.148 | SUCCESS | rc=0 >>
#Ansible: Wtc Job
*/1 * * * * /usr/sbin/ntpdate 10.10.86.56 &> /dev/null

7、fileモジュール:
(1)リンクファイルの作成:src=、path=、state=
(2)ファイル属性の変更:path=、owner=、mode=、group=
(3)ディレクトリの作成:path=、state=
  (4)state
ディレクトリが存在しない場合は、ディレクトリが作成されます.
link:ソフトリンクの作成
hard:ハードリンクの作成
touch:ファイルが存在しない場合は作成されます.ファイルが存在する場合はmtimeを変更します
absent:ディレクトリ、ファイルの削除、リンクの解除
[root@localhost ~]# ansible ansible_agent -m file -a "src=/etc/fstab path=/tmp/fstab.link state=link"
10.10.73.148 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/fstab.link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 10, 
    "src": "/etc/fstab", 
    "state": "link", 
    "uid": 0
}
10.10.73.149 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/fstab.link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 10, 
    "src": "/etc/fstab", 
    "state": "link", 
    "uid": 0
}
[root@localhost ~]# ansible ansible_agent -m shell -a "ls /tmp"
10.10.73.149 | SUCCESS | rc=0 >>
ansible_TdmFaK
fstab.link
wtc
wxpp.txt

10.10.73.148 | SUCCESS | rc=0 >>
ansible_1oUVuk
fstab.link
report.sh
wtc.txt
wxpp.txt
[root@localhost ~]# ansible ansible_agent -m file -a "path=/tmp/fstab.link state=absent"
10.10.73.149 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/fstab.link", 
    "state": "absent"
}
10.10.73.148 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/fstab.link", 
    "state": "absent"
}
[root@localhost ~]# ansible ansible_agent -m shell -a "ls /tmp"
10.10.73.148 | SUCCESS | rc=0 >>
ansible_jTgQOI
report.sh
wtc.txt
wxpp.txt

10.10.73.149 | SUCCESS | rc=0 >>
ansible_CuFRGA
wtc
wxpp.txt

8、その他のモジュール(ansible-doc-l)
fetchモジュール:リモートホストのファイルをローカルにプル(プルされたのはファイルのみですが、ローカルにプルされた後はディレクトリで、「hosts」ファイルの名前で命名されます
hostnameモジュールhostnameモジュール:ホスト名の管理
yumモジュール:リモートホストへのアプリケーションのインストール、プログラムのアンインストール
サービスモジュール:リモートホスト上のサービスの管理
userモジュール:ユーザーのアカウント、パスワードを管理する
groupモジュール:管理システムユーザーグループ
 
まとめ:
1、ansibleはsshに基づいて鍵なしでログインし、現在のユーザに限られる.「wtc」ユーザーがansible鍵を作成してリモートホストにプッシュする場合、「root」ユーザープッシュコマンドを使用するにはパスワードが必要です.
2、shellモジュールについては、コマンド式で表現できる限り、多くのモジュールの機能を実現できると思います.
ansible ansible_agent -m shell -a "yum install httpd"  
ansible ansible_agent -m shell -a "service httpd start"
ansible ansible_agent -m shell -a "echo 'wtc sent a bouquet of roses to wxpp' &>> /tmp/wxpp.txt"
ansible ansible_agent -m shell -a "ln -sv /etc/fstab /tmp/fstab.link"