Ansible常用模块介绍

2018-01-02 16:33:54 浏览数 (1)

1、 ansible-doc

希望知道更加详细的module的信息,最好的方法是使用ansible自带的ansible-doc的-s选项

代码语言:javascript复制
[root@node1 ~]# ansible-doc -s raw
- name: Executes a low-down and dirty SSH command
  action: raw
      executable             # change the shell used to execute the command. Should be an absolute path to the
                               executable. when using privilege escalation
                               (`become'), a default shell will be assigned if
                               one is not provided as privilege escalation
                               requires a shell.
      free_form=             # the raw module takes a free form command to run. There is no parameter actually
                               named 'free form'; see the examples!
[root@node1 ~]# 
[root@node1 ~]# ansible-doc -s shell
- name: Execute commands in nodes.
  action: shell
      chdir                  # cd into this directory before running the command
      creates                # a filename, when it already exists, this step will *not* be run.
      executable             # change the shell used to execute the command. Should be an absolute path to the
                               executable.
      free_form=             # The shell module takes a free form command to run, as a string.  There's not an
                               actual option named "free form".  See the
                               examples!
      removes                # a filename, when it does not exist, this step will *not* be run.
      warn                   # if command warnings are on in ansible.cfg, do not warn about this particular
                               line if set to no/false.
[root@node1 ~]# 
[root@node1 ~]# ansible-doc -s command
- name: Executes a command on a remote node
  action: command
      chdir                  # cd into this directory before running the command
      creates                # a filename or (since 2.0) glob pattern, when it already exists, this step will
                               *not* be run.
      executable             # change the shell used to execute the command. Should be an absolute path to the
                               executable.
      free_form=             # the command module takes a free form command to run.  There is no parameter
                               actually named 'free form'. See the examples!
      removes                # a filename or (since 2.0) glob pattern, when it does not exist, this step will
                               *not* be run.
      warn                   # if command warnings are on in ansible.cfg, do not warn about this particular
                               line if set to no/false.
[root@node1 ~]# ansible localhost -m raw -a "echo hello"
localhost | SUCCESS | rc=0 >>
hello


[root@node1 ~]#

2、 常用模块之command/shell/raw

使用模块 command或者shell或者raw都能调用对象机器上的某条指令或者某个可执行文

  • command模块,执行远程命令
  • script模块 ,在远程主机执行主控端的shell/python脚本
  • shell模块 ,执行远程主机的shell/python脚本
  • raw模块 ,类似于command模块、支持管道传递

(1)直接执行脚本

代码语言:javascript复制
[root@node1 ~]# ansible 192.168.80.133 -m raw -a "/tmp/test.sh"
192.168.80.133 | SUCCESS | rc=0 >>
Mon Oct  2 08:52:08 EDT 2017
Shared connection to 192.168.80.133 closed.


[root@node1 ~]# ansible 192.168.80.133 -m shell -a "/tmp/test.sh"
192.168.80.133 | SUCCESS | rc=0 >>
Mon Oct  2 08:52:21 EDT 2017

[root@node1 ~]# ansible 192.168.80.133 -m command -a "/tmp/test.sh"
192.168.80.133 | SUCCESS | rc=0 >>
Mon Oct  2 08:52:29 EDT 2017

[root@node1 ~]#

(2) 是否支持管道

  • command 不支持管道
  • shell 支持管道
  • raw 支持管道
代码语言:javascript复制
[root@node1 ~]# ansible 192.168.80.133 -m shell -a "ps -ef |wc -l"
192.168.80.133 | SUCCESS | rc=0 >>
96

[root@node1 ~]# ansible 192.168.80.133 -m raw -a "ps -ef |wc -l"
192.168.80.133 | SUCCESS | rc=0 >>
93
Shared connection to 192.168.80.133 closed.

[root@node1 ~]# ansible 192.168.80.133 -m command -a "ps -ef |wc -l"
192.168.80.133 | FAILED | rc=1 >>
error: garbage option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).

[root@node1 ~]#

command不支持命令行通配符

代码语言:javascript复制
[root@node1 ~]# ansible 192.168.80.133 -m command -a 'ls -l /tmp/*.sh'
192.168.80.133 | FAILED | rc=2 >>
ls: cannot access /tmp/*.sh: No such file or directory

[root@node1 ~]# ansible 192.168.80.133 -m shell -a 'ls -l /tmp/*.sh'
192.168.80.133 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 root root 17 Sep 28 10:49 /tmp/test.sh

[root@node1 ~]# ansible 192.168.80.133 -m raw -a 'ls -l /tmp/*.sh'
192.168.80.133 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 root root 17 Sep 28 10:49 /tmp/test.sh
Shared connection to 192.168.80.133 closed.


[root@node1 ~]#

2017-11-17 补充:

代码语言:javascript复制
[root@hadron web_store]# ansible nb1 -m command -a "/etc/init.d/zookeeper-server status"
nb1 | SUCCESS | rc=0 >>
Mode: leaderZooKeeper JMX enabled by default
Using config: /etc/zookeeper/conf/zoo.cfg

[root@hadron web_store]# ansible nb1 -m shell -a "/etc/init.d/zookeeper-server status"
nb1 | SUCCESS | rc=0 >>
Mode: leaderZooKeeper JMX enabled by default
Using config: /etc/zookeeper/conf/zoo.cfg

[root@hadron web_store]# ansible nb1 -m raw -a "/etc/init.d/zookeeper-server status"
nb1 | SUCCESS | rc=0 >>
ZooKeeper JMX enabled by default
Using config: /etc/zookeeper/conf/zoo.cfg
Mode: leader
Shared connection to nb1 closed.


[root@hadron web_store]# 

3、 常用模块之copy

使用copy模块,可以实现向目标机器进行远程copy的能力。

参数

说明

src

被复制到远程主机的本地对象文件或者文件夹,可以是绝对路径,也可以是相对路径。

dest

被复制到远程主机的本地对象文件或者文件夹

mode

复制对象的设定权限

backup

在文件存在的时候可以选择覆盖之前,将源文件备份.设定值:yes/no 缺省为yes

force

是否强制覆盖.设定值:yes/no 缺省为yes

其余请自行ansible-doc -s copy

default的情况下,force是yes的,所以什么都不写,文件存在的情况是会被覆盖的

注意: script模块 ,在远程主机执行主控端的shell/python脚本。也就是说 script模块实现了将主控节点的脚本复制到远程节点,然后在远程节点执行脚本。

代码语言:javascript复制
[root@hadron ~]# cat /root/test/test.sh
#!/bin/bash
date
ls /root
[root@hadron ~]# ansible 192.168.1.161 -m script -a "/root/test/test.sh"
192.168.1.161 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.1.161 closed.rn", 
    "stdout": "2017年 11月 03日 星期五 09:16:34 CSTrnanaconda-ks.cfg  books.txtt  scriptsttest1t       zsh_test.txtrnbooks.bakt mapred-site.xml  sshKeyGen.shtyarn-site.xmlrn", 
    "stdout_lines": [
        "2017年 11月 03日 星期五 09:16:34 CST", 
        "anaconda-ks.cfg  books.txtt  scriptsttest1t       zsh_test.txt", 
        "books.bakt mapred-site.xml  sshKeyGen.shtyarn-site.xml"
    ]
}
[root@hadron ~]# 

4、setup模块

setup模块,用于收集对象机器的基本设定信息 (1) 不用option的情况会输出所有相关的对象机器的facts

代码语言:javascript复制
[root@node1 ~]# ansible 192.168.80.133 -m setup
192.168.80.133 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.80.133"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::20c:29ff:fe56:76ac"
        ], 
        "ansible_apparmor": {
            "status": "disabled"
        }, 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "07/02/2015", 
        "ansible_bios_version": "6.00", 
        "ansible_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-514.el7.x86_64", 
            "LANG": "en_US.UTF-8", 
            "crashkernel": "auto", 
            "quiet": true, 
            "rd.lvm.lv": "cl/swap", 
            "rhgb": true, 
            "ro": true, 
            "root": "/dev/mapper/cl-root"
        }, 
        "ansible_date_time": {
            "date": "2017-10-02", 
            "day": "02", 
            "epoch": "1506952781", 
            "hour": "09", 
            "iso8601": "2017-10-02T13:59:41Z", 
            "iso8601_basic": "20171002T095941588584", 
            "iso8601_basic_short": "20171002T095941", 
            "iso8601_micro": "2017-10-02T13:59:41.588683Z", 
            "minute": "59", 
            "month": "10", 
            "second": "41", 
            "time": "09:59:41", 
            "tz": "EDT", 
            "tz_offset": "-0400", 
            "weekday": "Monday", 
            "weekday_number": "1", 
            "weeknumber": "40", 
            "year": "2017"
        }, 

...
...

(2) setup常用Option:filter 比如收集对象机器的环境变量信息

代码语言:javascript复制
[root@node1 ~]# ansible 192.168.80.133 -m setup -a "filter=ansible_env"
192.168.80.133 | SUCCESS => {
    "ansible_facts": {
        "ansible_env": {
            "CLASSPATH": ".::/opt/jdk1.8.0_112/lib", 
            "HADOOP_HOME": "/opt/hadoop-2.7.3", 
            "HBASE_HOME": "/opt/hbase-1.2.6", 
            "HIVE_HOME": "/opt/hive-2.1.1", 
            "HOME": "/root", 
            "JAVA_HOME": "/opt/jdk1.8.0_112", 
            "LANG": "en_US.UTF-8", 
            "LESSOPEN": "||/usr/bin/lesspipe.sh %s", 
            "LOGNAME": "root", 
            "LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:", 
            "MAIL": "/var/mail/root", 
            "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/jdk1.8.0_112/bin:/opt/hadoop-2.7.3/bin:/opt/hadoop-2.7.3/sbin:/opt/zookeeper-3.4.10/bin:/opt/hbase-1.2.6/bin:/opt/hive-2.1.1/bin", 
            "PWD": "/root", 
            "SHELL": "/bin/bash", 
            "SHLVL": "2", 
            "SSH_CLIENT": "192.168.80.131 58450 22", 
            "SSH_CONNECTION": "192.168.80.131 58450 192.168.80.133 22", 
            "SSH_TTY": "/dev/pts/1", 
            "TERM": "xterm", 
            "USER": "root", 
            "XDG_RUNTIME_DIR": "/run/user/0", 
            "XDG_SESSION_ID": "15", 
            "ZOO_HOME": "/opt/zookeeper-3.4.10", 
            "_": "/usr/bin/python"
        }
    }, 
    "changed": false
}
[root@node1 ~]# 

5、user/group模块

  • user模块,用于管理用户。
  • group模块,用于管理group。
代码语言:javascript复制
[root@node1 ~]# echo [slaves] >> /etc/ansible/hosts
[root@node1 ~]# echo node2 >> /etc/ansible/hosts
[root@node1 ~]# echo node3 >> /etc/ansible/hosts

使用user模块添加用户

代码语言:javascript复制
[root@node1 ~]# ansible node3 -m command -a "id test01"
node3 | FAILED | rc=1 >>
id: test01: no such user

[root@node1 ~]# ansible node3 -m user -a "name=test01 group=root"
node3 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 0, 
    "home": "/home/test01", 
    "name": "test01", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1000
}
[root@node1 ~]# ansible node3 -m command -a "id test01"
node3 | SUCCESS | rc=0 >>
uid=1000(test01) gid=0(root) groups=0(root)

[root@node1 ~]#

使用user模块删除用户

代码语言:javascript复制
[root@node1 ~]# ansible node3 -m user -a "name=test01 state=absent remove=yes"
node3 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "test01", 
    "remove": true, 
    "state": "absent"
}
[root@node1 ~]# ansible node3 -m command -a "id test01"
node3 | FAILED | rc=1 >>
id: test01: no such user

[root@node1 ~]# 

使用group 模块添加group

代码语言:javascript复制
[root@node1 ~]# ansible node3 -m shell -a "cat /etc/group |grep testgrp01"
node3 | FAILED | rc=1 >>


[root@node1 ~]# ansible node3 -m group -a "name=testgrp01"
node3 | SUCCESS => {
    "changed": true, 
    "gid": 1000, 
    "name": "testgrp01", 
    "state": "present", 
    "system": false
}
[root@node1 ~]# ansible node3 -m shell -a "cat /etc/group |grep testgrp01"
node3 | SUCCESS | rc=0 >>
testgrp01:x:1000:

[root@node1 ~]# 

使用group模块删除group

代码语言:javascript复制
[root@node1 ~]# ansible node3 -m group -a "name=testgrp01 state=absent"
node3 | SUCCESS => {
    "changed": true, 
    "name": "testgrp01", 
    "state": "absent"
}
[root@node1 ~]# ansible node3 -m shell -a "cat /etc/group |grep testgrp01"
node3 | FAILED | rc=1 >>


[root@node1 ~]# 

6、yum模块

使用yum包管理器来管理软件包,其选项有: name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径 state:状态(present-已安装,absent-未安装(卸载),latest-最新的)

代码语言:javascript复制
#确保vim 包已经安装,但不更新
ansible ip -m yum -a "name=vim state=present"
#确保安装包到一个特定的版本
ansible  ip -m yum -a "name=vim -7.4 state=present"
#确保一个软件包是最新版本
ansible  ip -m yum -a "name=vim state=latest"
#确保一个软件包没有被安装
ansible ip -m yum -a "name=vim state=absent"

使用yum模块删除httpd

代码语言:javascript复制
[root@node1 ~]# ansible node3 -m shell -a "rpm -qa |grep httpd"
 [WARNING]: Consider using yum, dnf or zypper module rather than running rpm

node3 | SUCCESS | rc=0 >>
httpd-tools-2.4.6-67.el7.centos.2.x86_64
httpd-2.4.6-67.el7.centos.2.x86_64

[root@node1 ~]#
代码语言:javascript复制
[root@node1 ~]# ansible node3 -m yum -a "name=httpd state=absent"
node3 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirrornResolving Dependenciesn--> Running transaction checkn---> Package httpd.x86_64 0:2.4.6-67.el7.centos.2 will be erasedn--> Finished Dependency ResolutionnnDependencies Resolvednn================================================================================n Package      Arch          Version                       Repository       Sizen================================================================================nRemoving:n httpd        x86_64        2.4.6-67.el7.centos.2         @updates        9.4 MnnTransaction Summaryn================================================================================nRemove  1 PackagennInstalled size: 9.4 MnDownloading packages:nRunning transaction checknRunning transaction testnTransaction test succeedednRunning transactionn  Erasing    : httpd-2.4.6-67.el7.centos.2.x86_64                           1/1 n  Verifying  : httpd-2.4.6-67.el7.centos.2.x86_64                           1/1 nnRemoved:n  httpd.x86_64 0:2.4.6-67.el7.centos.2                                          nnComplete!n"
    ]
}
[root@node1 ~]# ansible node3 -m shell -a "rpm -qa |grep httpd"
 [WARNING]: Consider using yum, dnf or zypper module rather than running rpm

node3 | SUCCESS | rc=0 >>
httpd-tools-2.4.6-67.el7.centos.2.x86_64

[root@node1 ~]# 

7、service模块

service模块用于管理服务 该模块包含如下选项:

arguments:给命令行提供一些选项 enabled:是否开机启动 yes|no name:必选项,服务名称 state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)

代码语言:javascript复制
[root@node1 ~]# ansible node2 -m service -a "name=httpd state=started enabled=yes"
node2 | SUCCESS => {
    "changed": false, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started", 
    "status": {
        "ActiveEnterTimestamp": "Mon 2017-11-13 08:40:59 EST", 
        "ActiveEnterTimestampMonotonic": "2785271709", 
        "ActiveExitTimestamp": "Mon 2017-11-13 08:40:58 EST", 
        "ActiveExitTimestampMonotonic": "2784178548", 
        "ActiveState": "active", 
        "After": "nss-lookup.target tmp.mount network.target system.slice systemd-journald.socket remote-fs.target basic.target -.mount", 
        "AllowIsolate": "no", 
        "AssertResult": "yes", 
        "AssertTimestamp": "Mon 2017-11-13 08:40:59 EST", 
        "AssertTimestampMonotonic": "2785210640", 
        "Before": "multi-user.target shutdown.target", 
        "BlockIOAccounting": "no", 
        "BlockIOWeight": "18446744073709551615", 
        "CPUAccounting": "no", 
        "CPUQuotaPerSecUSec": "infinity", 
        "CPUSchedulingPolicy": "0", 
        "CPUSchedulingPriority": "0", 
        "CPUSchedulingResetOnFork": "no", 
        "CPUShares": "18446744073709551615", 
        "CanIsolate": "no", 
        "CanReload": "yes", 
        "CanStart": "yes", 
        "CanStop": "yes", 
        "CapabilityBoundingSet": "18446744073709551615", 
        "ConditionResult": "yes", 
        "ConditionTimestamp": "Mon 2017-11-13 08:40:59 EST", 
        "ConditionTimestampMonotonic": "2785210639", 
        "Conflicts": "shutdown.target", 
        "ControlGroup": "/system.slice/httpd.service", 
        "ControlPID": "0", 
        "DefaultDependencies": "yes", 
        "Delegate": "no", 
        "Description": "The Apache HTTP Server", 
        "DevicePolicy": "auto", 
        "Documentation": "man:httpd(8) man:apachectl(8)", 
        "EnvironmentFile": "/etc/sysconfig/httpd (ignore_errors=no)", 
        "ExecMainCode": "0", 
        "ExecMainExitTimestampMonotonic": "0", 
        "ExecMainPID": "2913", 
        "ExecMainStartTimestamp": "Mon 2017-11-13 08:40:59 EST", 
        "ExecMainStartTimestampMonotonic": "2785211376", 
        "ExecMainStatus": "0", 
        "ExecReload": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -k graceful ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", 
        "ExecStart": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -DFOREGROUND ; ignore_errors=no ; start_time=[Mon 2017-11-13 08:40:59 EST] ; stop_time=[n/a] ; pid=2913 ; code=(null) ; status=0/0 }", 
        "ExecStop": "{ path=/bin/kill ; argv[]=/bin/kill -WINCH ${MAINPID} ; ignore_errors=no ; start_time=[Mon 2017-11-13 08:40:58 EST] ; stop_time=[Mon 2017-11-13 08:40:58 EST] ; pid=2908 ; code=exited ; status=0 }", 
        "FailureAction": "none", 
        "FileDescriptorStoreMax": "0", 
        "FragmentPath": "/usr/lib/systemd/system/httpd.service", 
        "GuessMainPID": "yes", 
        "IOScheduling": "0", 
        "Id": "httpd.service", 
        "IgnoreOnIsolate": "no", 
        "IgnoreOnSnapshot": "no", 
        "IgnoreSIGPIPE": "yes", 
        "InactiveEnterTimestamp": "Mon 2017-11-13 08:40:59 EST", 
        "InactiveEnterTimestampMonotonic": "2785209929", 
        "InactiveExitTimestamp": "Mon 2017-11-13 08:40:59 EST", 
        "InactiveExitTimestampMonotonic": "2785211427", 
        "JobTimeoutAction": "none", 
        "JobTimeoutUSec": "0", 
        "KillMode": "control-group", 
        "KillSignal": "18", 
        "LimitAS": "18446744073709551615", 
        "LimitCORE": "18446744073709551615", 
        "LimitCPU": "18446744073709551615", 
        "LimitDATA": "18446744073709551615", 
        "LimitFSIZE": "18446744073709551615", 
        "LimitLOCKS": "18446744073709551615", 
        "LimitMEMLOCK": "65536", 
        "LimitMSGQUEUE": "819200", 
        "LimitNICE": "0", 
        "LimitNOFILE": "4096", 
        "LimitNPROC": "7208", 
        "LimitRSS": "18446744073709551615", 
        "LimitRTPRIO": "0", 
        "LimitRTTIME": "18446744073709551615", 
        "LimitSIGPENDING": "7208", 
        "LimitSTACK": "18446744073709551615", 
        "LoadState": "loaded", 
        "MainPID": "2913", 
        "MemoryAccounting": "no", 
        "MemoryCurrent": "18446744073709551615", 
        "MemoryLimit": "18446744073709551615", 
        "MountFlags": "0", 
        "Names": "httpd.service", 
        "NeedDaemonReload": "no", 
        "Nice": "0", 
        "NoNewPrivileges": "no", 
        "NonBlocking": "no", 
        "NotifyAccess": "main", 
        "OOMScoreAdjust": "0", 
        "OnFailureJobMode": "replace", 
        "PermissionsStartOnly": "no", 
        "PrivateDevices": "no", 
        "PrivateNetwork": "no", 
        "PrivateTmp": "yes", 
        "ProtectHome": "no", 
        "ProtectSystem": "no", 
        "RefuseManualStart": "no", 
        "RefuseManualStop": "no", 
        "RemainAfterExit": "no", 
        "Requires": "-.mount basic.target", 
        "RequiresMountsFor": "/var/tmp", 
        "Restart": "no", 
        "RestartUSec": "100ms", 
        "Result": "success", 
        "RootDirectoryStartOnly": "no", 
        "RuntimeDirectoryMode": "0755", 
        "SameProcessGroup": "no", 
        "SecureBits": "0", 
        "SendSIGHUP": "no", 
        "SendSIGKILL": "yes", 
        "Slice": "system.slice", 
        "StandardError": "inherit", 
        "StandardInput": "null", 
        "StandardOutput": "journal", 
        "StartLimitAction": "none", 
        "StartLimitBurst": "5", 
        "StartLimitInterval": "10000000", 
        "StartupBlockIOWeight": "18446744073709551615", 
        "StartupCPUShares": "18446744073709551615", 
        "StatusErrno": "0", 
        "StatusText": "Total requests: 10; Current requests/sec: 0; Current traffic:   0 B/sec", 
        "StopWhenUnneeded": "no", 
        "SubState": "running", 
        "SyslogLevelPrefix": "yes", 
        "SyslogPriority": "30", 
        "SystemCallErrorNumber": "0", 
        "TTYReset": "no", 
        "TTYVHangup": "no", 
        "TTYVTDisallocate": "no", 
        "TimeoutStartUSec": "1min 30s", 
        "TimeoutStopUSec": "1min 30s", 
        "TimerSlackNSec": "50000", 
        "Transient": "no", 
        "Type": "notify", 
        "UMask": "0022", 
        "UnitFilePreset": "disabled", 
        "UnitFileState": "enabled", 
        "WantedBy": "multi-user.target", 
        "Wants": "system.slice", 
        "WatchdogTimestamp": "Mon 2017-11-13 08:40:59 EST", 
        "WatchdogTimestampMonotonic": "2785271669", 
        "WatchdogUSec": "0"
    }
}
[root@node1 ~]# 

8、cron模块

cron模块用于管理计划任务

backup:对远程主机上的原任务计划内容修改之前做备份 cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划 day:日(1-31,,/2,……) hour:小时(0-23,,/2,……) minute:分钟(0-59,,/2,……) month:月(1-12,,/2,……) weekday:周(0-7,*,……) job:要执行的任务,依赖于state=present name:该任务的描述 special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly state:确认该任务计划是创建还是删除 user:以哪个用户的身份执行

代码语言:javascript复制
[root@node1 ~]# ansible node2 -m cron -a 'name="hello" weekday="1" minute=0 hour=12 user="root" job="/root/hello.sh"'
node2 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello"
    ]
}
[root@node1 ~]# 

0 人点赞