day39 ansible 模块

ansible模块

查找模块
ansible-doc -l
查看某个模块的具体参数帮助
ansible-doc -s command

一、command模块

1.功能说明:

command Executes a command on a remote node
功能说明:执行一个命令在远程节点上

操作实践:

ansible oldboy -m command -a "free -m"
ansible oldboy -m command -a "df -h"
ansible oldboy -m command -a "ls /root"
ansible oldboy -m command -a "cat redhat-release"
ansible oldboy -m command -a "cat /etc/redhat-release"
最通用的功能。
[root@m01 ~]# ansible oldboy -m command -a "cat /etc/redhat-release"
172.16.1.7 | CHANGED | rc=0 >>
CentOS Linux release 7.6.1810 (Core) 

172.16.1.31 | CHANGED | rc=0 >>
CentOS Linux release 7.6.1810 (Core) 

172.16.1.41 | CHANGED | rc=0 >>
CentOS Linux release 7.6.1810 (Core) 

[root@m01 ~]# cat /server/scripts/cmd.sh 
for n in 31 41
do
   echo "=====172.16.1.$n======"
   ssh 172.16.1.$n "$1"
done
[root@m01 ~]# sh /server/scripts/cmd.sh "cat /etc/redhat-release"
=====172.16.1.31======
CentOS Linux release 7.6.1810 (Core) 
=====172.16.1.41======
CentOS Linux release 7.6.1810 (Core) 

特殊:不支持的东西,例如 > < | &等 $HOME,替代方案用shell模块
ansible oldboy -m shell -a "ps -ef|grep ssh"
ansible oldboy -m shell -a "echo oldboy >/tmp/a.log"

2.常用参数说明及实践

[root@m01 ~]# ansible-doc -s command
- name: Executes a command on a remote node
  command:
      argv:                  # Allows the user to provide the command as a list vs. a string.  Only the
                               string or the list form can be provided, not
                               both.  One or the other must be provided.
      chdir:                 # Change into this directory before running the command.
      creates:               # A filename or (since 2.0) glob pattern. If it already exists, this step
                               *won't* be run.
      free_form:             # (required) 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. If it already exists, this step *will*  be run.
      stdin:                 # Set the stdin of the command directly to the specified value.
      warn:                  # If command_warnings are on in ansible.cfg, do not warn about this particular
                               line if set to `no'.

参数:chdir=/tmp配置相当于cd /tmp
[root@m01 ~]# ansible oldboy  -m command -a "pwd chdir=/etc"
ansible oldboy  -m shell -a "cd /etc/;pwd"
 
参数:creates=/etc  相当于条件测试  [ -e /etc ]||pwd 和下面removes相反
[root@m01 ~]# ansible oldboy  -m command -a "pwd creates=/etc"

参数:removes=/root 相当于条件测试 [ -e /root ]&&ls /root
ansible oldboy  -m command -a "ls /root removes=/root"
ansible oldboy  -m shell -a "[ -d /etc ]||pwd"
[root@m01 ~]# ansible oldboy  -m command -a "cat /etc/hosts removes=/etc/hosts"

参数:warn=False 忽略警告
[root@m01 ~]# ansible oldboy  -m command -a "chmod 000 /etc/hosts warn=False"

二、shell模块

功能说明:

shell Execute commands in nodes.
功能说明:执行一个命令在远程节点上

2.常见参数:

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:             # (required) 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.
      stdin:                 # Set the stdin of the command directly to the specified value.
      warn:                  # if command warnings are on in ansible.cfg, do not warn about
                               this particular line if set to  no/false.

三、copy模块

1.功能说明:

功能说明:复制文件到远程主机

2.常见参数:

四、file模块

1.功能说明

功能说明:设置文件属性

2.常见参数:

五、script模块

1.功能说明

功能说明:远程节点上执行本地脚本

六、yum模块

1.功能说明

功能说明:yum包管理模块

七、systemd模块(C7)

1.功能说明

功能说明:管理服务模块

八、cron模块

1.功能说明

功能说明:管理定时任务条目信息模块

2.常见参数:

项目实践:

rsync服务端写成脚本r1.sh
rsync服务端写成脚本
nfs服务端写成脚本n1.sh
nfs客户端写成脚本
sersync服务端写成脚本s1.sh
sersync客户端写成脚本

你可能感兴趣的:(day39 ansible 模块)