Ansible共通モジュールの紹介


ansibleには多くのモジュールがあり、ansibleホスト上でansible-doc-lコマンドを実行してansibleがサポートしているすべてのモジュールを表示できます.ansible-doc-s MODULEで_NAMEコマンドでは、指定したモジュールのすべてのパラメータを表示できます.
すべてのモジュールの表示
root@host1:/etc/ansible/roles/tomcat8_install/tasks# ansible-doc  -l
less 436
Copyright (C) 1984-2009 Mark Nudelman
less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
a10_server                      Manage A10 Networks AX/SoftAX/Thunder/vThunder devices                                     
a10_service_group               Manage A10 Networks devices' service groups                                                
a10_virtual_server              Manage A10 Networks devices' virtual servers                                               
acl                             Sets and retrieves file ACL information.                                                   
add_host                        add a host (and alternatively a group) to the ansible-playbook in-memory inventory         
airbrake_deployment             Notify airbrake about app deployments                                                      
alternatives                    Manages alternative programs for common commands                                           
apache2_module                  enables/disables a module of the Apache2 webserver                                         
apk                             Manages apk packages                                                                       
apt                             Manages apt-packages                                                                       
apt_key                         Add or remove an apt key                                                                   
apt_repository                  Add and remove APT repositories                                                            
apt_rpm                         apt_rpm package manager                                                                    
assemble                        Assembles a configuration file from fragments                                              
assert                          Fail with custom message                                                                   
at                              Schedule the execution of a command or script file via the at command.                     
authorized_key                  Adds or removes an SSH authorized key                                                      
azure                           create or terminate a virtual machine in azure                                             
bigip_facts                     Collect facts from F5 BIG-IP devices                                                       
bigip_gtm_wide_ip               Manages F5 BIG-IP GTM wide ip                                                              
bigip_monitor_http              Manages F5 BIG-IP LTM

モジュールパラメータを表示するには、次の手順に従います.
ansible-doc -s MODULE_NAME
  :
[root@localhost ansible]# ansible-doc -s file
- name: Sets attributes of files
  action: file
      follow                 # This flag indicates that filesystem links, if they exist, should be followed.
      force                  # force the creation of the symlinks in two cases: the source file does not exist (but will appear later);
                               the destination exists and is a file (so, we need to unlink the "path" file
                               and create symlink to the "src" file in place of it).
      group                  # name of the group that should own the file/directory, as would be fed to `chown'
      mode                   # mode the file or directory should be. For those used to `/usr/bin/chmod' remember that modes are actually
                               octal numbers (like 0644). Leaving off the leading zero will likely have
                               unexpected results. As of version 1.8, the mode may be specified as a
                               symbolic mode (for example, `u+rwx' or `u=rw,g=r,o=r').
      owner                  # name of the user that should own the file/directory, as would be fed to `chown'
      path=                  # path to the file being managed.  Aliases: `dest', `name'
      recurse                # recursively set the specified file attributes (applies only to state=directory)
      selevel                # level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the `range'.
                               `_default' feature works as for `seuser'.
      serole                 # role part of SELinux file context, `_default' feature works as for `seuser'.
      setype                 # type part of SELinux file context, `_default' feature works as for `seuser'.
      seuser                 # user part of SELinux file context. Will default to system policy, if applicable. If set to `_default', it
                               will use the `user' portion of the policy if available
      src                    # path of the file to link to (applies only to `state=link'). Will accept absolute, relative and nonexisting
                               paths. Relative paths are not expanded.
      state                  # If `directory', all immediate subdirectories will be created if they do not exist, since 1.7 they will be
                               created with the supplied permissions. If `file', the file will NOT be
                               created if it does not exist, see the [copy] or [template] module if you
                               want that behavior.  If `link', the symbolic link will be created or
                               changed. Use `hard' for hardlinks. If `absent', directories will be
                               recursively deleted, and files or symlinks will be unlinked. If `touch' (new
                               in 1.4), an empty file will be created if the `path' does not exist, while
                               an existing file or directory will receive updated file access and
                               modification times (similar to the way `touch` works from the command line)

モジュールの説明:
copyモジュール:http://docs.ansible.com/ansible/copy_module.html
機能:ファイルをリモートホストの指定したパスの下にコピーします.
パラメータ:
src:ローカルファイルのパス.ソースがディレクトリの場合、ディレクトリ内のすべてのファイルがcopyされます.
dest:リモートホストの絶対パス
owner:ファイル所有者
group:ファイル属グループ
mode:ファイル権限
コマンドのデモ:
ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab owner=root mode=0644'

fileモジュール:http://docs.ansible.com/ansible/file_module.html
機能:ファイル属性の設定、シンボルリンクの作成、ディレクトリの作成など
パラメータ:
path:ファイルパスを指定し、nameまたはdestを使用して置き換えることができます.
owner:ファイル所有者
group:ファイル属グループ
mode:ファイル権限
ファイルのシンボルリンクを作成するには、次の手順に従います.
src:ソースファイルの指定
dest:シンボルリンクファイルのパスを示す
コマンドのデモ:
ansible pms -m file -a 'src=/tmp/fstab dest=/srv/fstab state=link'
[root@localhost srv]# ll
lrwxrwxrwx 1 root root      10 Jul  6 14:08 fstab -> /tmp/fstab

pingモジュール:http://docs.ansible.com/ansible/ping_module.html
機能:管理対象ホストの接続性をテストする
コマンドのデモ:
[root@localhost ansible]# ansible all -m ping
172.16.206.134 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.10.10.202 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

commandモジュール:http://docs.ansible.com/ansible/command_module.html
機能:リモートホストでコマンドを実行する
コマンドのデモ:
[root@localhost ansible]# ansible all -m command -a 'hostname'      
172.16.206.134 | SUCCESS | rc=0 >>
localhost.localdomain
10.10.10.202 | SUCCESS | rc=0 >>
localhost.localdomain

注意:commandモジュールはパイプ文字をサポートしていません.これもcommandモジュールとshellモジュールの違いです.
例:
userモジュール:http://docs.ansible.com/ansible/user_module.html
機能:リモートホスト上のユーザーの作成または削除
パラメータ:
name:口座名
state:
present:作成するさくせいする
absent:削除
group:ユーザーの基本グループを指定する
uid:uidの指定
System:システムユーザー値yesまたはnoの作成
コマンドのデモ:
[root@localhost ansible]# ansible pms -m user -a 'name=test state=present uid=306 group=root  system=yes'          
172.16.206.134 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 0, 
    "home": "/home/test", 
    "name": "test", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 306
}
[root@localhost srv]# id test
uid=306(test) gid=0(root) groups=0(root)

サービスモジュール:http://docs.ansible.com/ansible/service_module.html
機能:リモートホスト上のサービスステータスの管理
パラメータ:
enabled=:電源を入れて自動的に起動するかどうか、yesまたはnoの値を取ります.enabled=yes、サービス起動を示す
name=:サービス名
state=:サービスステータス
started:起動
restarted:再起動
stopped:停止
reloaded:リロード
コマンドのデモ:
[root@localhost ansible]# ansible pms -m service -a 'name=zabbix-agent state=started enabled=yes'
172.16.206.134 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "zabbix-agent", 
    "state": "started"
}

scriptモジュール:http://docs.ansible.com/ansible/script_module.html
機能:リモートホストでホスト側のshell/pythonスクリプトを実行する
root@host1:/tmp# ansible pms -m script -a '/tmp/echo.sh'
172.16.206.134 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "", 
    "stdout_lines": []
}

shellモジュール:http://docs.ansible.com/ansible/shell_module.html
shellモジュールはパイプシンボルをサポートし、commandsモジュールとの最大の違いです.
コマンドのデモ:
ansible pms -m shell -a 'ps -ef | grep tomcat'        
172.16.206.134 | SUCCESS | rc=0 >>
root      18569      1  0 13:09 pts/0    00:00:36 /usr/java/jdk1.8.0_66/bin/java -Djava.util.logging.config.file=/tmp/catalina_base/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m -Djava.endorsed.dirs=/usr/local/apache-tomcat-8.0.29/endorsed -classpath /usr/local/apache-tomcat-8.0.29/bin/bootstrap.jar:/usr/local/apache-tomcat-8.0.29/bin/tomcat-juli.jar -Dcatalina.base=/tmp/catalina_base -Dcatalina.home=/usr/local/apache-tomcat-8.0.29 -Djava.io.tmpdir=/tmp/catalina_base/temp org.apache.catalina.startup.Bootstrap start
root      19314  19313  0 17:24 pts/1    00:00:00 /bin/sh -c ps -ef | grep tomcat
root      19316  19314  0 17:24 pts/1    00:00:00 grep tomcat

yumモジュール:http://docs.ansible.com/ansible/yum_module.html
機能:リモートホストにパッケージをインストールする
パラメータ:
name=:パケット名は、リモートサーバからローカルにパケットをインストール場合、name=/srv/jdk/jdk-8 u 66-linux-x 64などのリモートホスト上の絶対パスを書くことができる.rpm
state=:ステータス、値present、absent、lastest
present、lastedインストール
absent:アンインストール
Lastest:最新版のパッケージをインストールします.ソフトウェアパッケージのアップグレードに相当します.
removed:パッケージの削除
Installed:パッケージのインストール
[root@localhost tmp]# ansible pms -m yum -a 'name=/srv/jdk/jdk-8u66-linux-x64.rpm  state=present'  
172.16.206.134 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": []
}

synchronizeモジュール:http://docs.ansible.com/ansible/synchronize_module.html
注意:ansibleホストとリモートホストにrsyncをインストールする必要があります.
機能:ansibleホスト上のソースディレクトリの下のファイルをリモートホストに同期する
パラメータ:
src:ansibleホスト上のソースパス
dest:リモートホスト上のターゲットパス
delete:delete=yesの場合、ターゲットパスの下にあるソースパスに存在しないディレクトリまたはファイルを削除します.
compress:圧縮機能をオンにするかどうか、デフォルトはオン
mode:同期モード、デフォルトはpush、mode=pullを設定し、pullモードに変更
archive:デフォルトでこのパラメータが開き、デフォルトではrecursive、links、perms、times、owner、group、Dパラメータが開きます.このパラメータをnoに設定すると、次のような多くのパラメータを停止します.たとえば、次のような目的で再帰的に失敗し、プルできません.
rsync_opts:rsyncの追加オプションを追加します.たとえば
rsync_opts="--exclude=fstab"は、同期時のファイルを表す場合、fstabファイルを除外します.すなわち、fstabファイルを同期しません.delete=yesオプションがあり、ターゲットパスの下にfstabというファイルのようなソースパスの下に存在しないファイルがある場合
rsync_opts="--exclude=fstab"は、ターゲットパスのfstabファイルを削除しないことを示します.
ansible pms  -m synchronize -a 'src=/tmp/test/ dest=/tmp/aaa/ delete=yes  rsync_opts="--exclude=fstab"'

上記のコマンドは、ansibleホスト上の/tmp/test/ディレクトリ下のすべてのファイル(fstabを除く)をリモートホストの/tmp/aaa/ディレクトリ下に同期します./tmp/aaa/ディレクトリ下の/tmp/test/上に存在しないファイルまたはディレクトリを削除します.
unarchiveモジュール:http://docs.ansible.com/ansible/unarchive_module.html
機能:解凍、このモジュールには2つの使い方があります.
1、ansibleホスト上の圧縮パッケージをローカルで解凍してリモートホスト上に転送する場合、copy=yes
2、リモートホスト上の圧縮パッケージを指定したパスの下に解凍する.この場合、copy=noを設定する必要があります
パラメータ:
copy:デフォルトはyesです.copy=yesの場合、コピーしたファイルはansibleホストからリモートホストにコピーされます.copy=noに設定すると、リモートホストでsrcソースファイルが見つかります.
src:ソースパス、ansibleホスト上のパス、またはリモートホスト上のパス、リモートホスト上のパスであればcopy=noを設定する必要があります
dest:リモートホスト上のターゲットパス
mode:解凍後のファイル権限の設定
コマンドのデモ:
ansible pms -m unarchive -a 'src=/srv/tomcat8/apache-tomcat-8.0.29.tar.gz dest=/usr/local copy=no mode=0755'

get_urlモジュール:http://docs.ansible.com/ansible/get_url_module.html
機能:http、https、ftpからファイルをリモートホストにダウンロード
パラメータ:
url:ダウンロードアドレス
dest:リモートホスト上のターゲットパス
mode:リモートホストにダウンロードしたファイルの権限を設定する
コマンドのデモ:
root@host1:/srv# ansible pms -m get_url -a 'url=ftp://ftp.cheyaoshicorp.com/pub/    /derby.init.sh dest=/tmp'      
172.16.206.134 | SUCCESS => {
    "changed": true, 
    "checksum_dest": null, 
    "checksum_src": "770a432e9847e594e0154e31c906062585d571e0", 
    "dest": "/tmp/derby.init.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "4564411c7e614859965c9ab5d76df22b", 
    "mode": "0644", 
    "msg": "OK (3934 bytes)", 
    "owner": "root", 
    "size": 3934, 
    "src": "/tmp/tmp5nqAsJ", 
    "state": "file", 
    "uid": 0, 
    "url": "ftp://ftp.cheyaoshicorp.com/pub/    /derby.init.sh"