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客户端写成脚本

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.ssh-keygen非交互式创建秘钥对: 具体命令:ssh-keygen -f ~/.ssh/id_rsa ...
    1220阅读 1,021评论 0 0
  • SSH批量管理项目如何一键一秒钟完成:一秒完成 1.ssh-keygen非交互式创建秘钥对: 具体命令: 参数讲解...
    藏鋒1013阅读 616评论 0 3
  • 1)安装2)常用模块3)inventory4)playbook(role\tag\template)5) yaml...
    秦记阅读 4,268评论 2 5
  • 0、运维发展历史 人肉运维——自动化运维——平台化——可视化运维——智能化运维(aiops)开发自动化——开发平台...
    puurutsjdy阅读 324评论 0 0
  • 2018-07-17 梦到一行人,有我和妹妹,嫂子和小五万,臭蛋和他哥,大舅和一个记不清的人,回到了老家,在一个半...
    不知方方阅读 170评论 0 0