前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ansible 常用模块详细介绍

Ansible 常用模块详细介绍

作者头像
老七Linux
发布2018-05-31 12:44:54
1K0
发布2018-05-31 12:44:54
举报

Ansible模块的学习已经是很久的事情了,今天正好周末,来图书馆整理下资料!

常用模块(module_name):
ping 模块:测试连通性
代码语言:javascript
复制
[[email protected] ~]# ansible all -m ping 
172.16.1.8 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.16.1.41 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.16.1.31 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

连接正常返回 ping 通过帮助信息可以获得 ↓

通过 ansible-doc -v ping 可以获得该模块的说明

ansible-doc -s file 参看模块的具体信息

代码语言:javascript
复制
[[email protected] ~]# ansible-doc -v ping
Using /etc/ansible/ansible.cfg as config file
> PING    (/usr/lib/python2.6/site-packages/ansible/modules/system/ping.py)

  A trivial test module, this module always returns `pong' on successful contact. It does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to verify the ability to login and that a usable  python is configured. This is NOT ICMP ping, this is just a trivial test module.
command 模块: 默认模块
image
image

不指定模块的时候默认使用的模块就是command ↓

代码语言:javascript
复制
[[email protected] ~]# ansible all -a "date"
172.16.1.41 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:15 CST 2017

172.16.1.31 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:15 CST 2017

172.16.1.8 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:15 CST 2017

使用ansible自带模块执行命令 如果要用 > < | & ‘ ‘ 使用shell 模块, command是不支持管道符之类的。

代码语言:javascript
复制
[[email protected] ~]# ansible all -m command -a "date"
172.16.1.8 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:27 CST 2017

172.16.1.31 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:28 CST 2017

172.16.1.41 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:27 CST 2017

chdir 参数的使用:

代码语言:javascript
复制
[[email protected] ~]# ansible web -m command -a "chdir=/tmp pwd"
172.16.1.31 | SUCCESS | rc=0 >>
/tmp

172.16.1.8 | SUCCESS | rc=0 >>
/tmp

172.16.1.41 | SUCCESS | rc=0 >>
/tmp

creates 文件是否存在,不存在就执行命令

代码语言:javascript
复制
[[email protected] ~]# ansible web -m command -a "creates=/etc/hosts date"
172.16.1.31 | SUCCESS | rc=0 >>
skipped, since /etc/hosts exists

removes 文件是否存在,不存在就不执行命令,

代码语言:javascript
复制
[[email protected] ~]# ansible web -m command -a "removes=/etc/hosts date"
172.16.1.31 | SUCCESS | rc=0 >>
Fri Oct 20 13:32:40 CST 2017
shell 模块:万能模块

执行linux命令时可以用

远程节点执行命令

说明: shell 模块在远程执行脚本时,远程主机上一定要有相应的脚本

代码语言:javascript
复制
[[email protected] ~]# ansible web -m shell -a "/bin/sh /server/scripts/ssh-key.sh"
172.16.1.31 | SUCCESS | rc=0 >>
fenfa 172.16.1.31 [  OK  ]

fenfa 172.16.1.41 [  OK  ]

fenfa 172.16.1.8 [  OK  ]
script 模块:执行脚本模块

执行本地脚本,将脚本的输出结果输出到远程节点上,例如执行一个脚本输出的结果是把时间写入到tmp目录下的一个txt文档中,执行后,远程的机器/tmp/目录下面就会有个txt的文档!!

代码语言:javascript
复制
[[email protected] ~]# ansible all -m script -a "/server/scripts/free.sh"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 172.16.1.8 closed.\r\n",
    "stdout": "             total       used       free     shared    buffers     cached\r\nMem:          474M       377M        97M       532K        54M       202M\r\n-/+ buffers/cache:       120M       354M\r\nSwap:         767M         0B       767M\r\n",
    "stdout_lines": [
        "             total       used       free     shared    buffers     cached",
        "Mem:          474M       377M        97M       532K        54M       202M",
        "-/+ buffers/cache:       120M       354M",
        "Swap:         767M         0B       767M"
    ]
}

说明:

使用scripts模块,不用将脚本传输到远程节点,脚本本身不用进行授权,即可利用script模块执行。直接执行脚本即可,不需要使用sh

copy模块:把本地文件发送到远端

常用参数:

image
image

说明: src和content不能同时使用

copy常用命令参数测试

使用copy 模块,将/etc/hosts 文件 传输到各个服务器送,权限修改为0600 属主属组为root

代码语言:javascript
复制
[[email protected] ~]# ansible web -m copy -a "src=/etc/hosts dest=/tmp/ mode=0600 owner=root group=root "
172.16.1.8 | SUCCESS => {
    "changed": true,
    "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
    "dest": "/tmp/hosts",
    "gid": 500,
    "group": "root",
    "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",
    "mode": "0600",
    "owner": "root",
    "size": 357,
    "src": "/root/.ansible/tmp/ansible-tmp-1508410846.63-224022812989166/source",
    "state": "file",
    "uid": 500
}
……

检查结果

代码语言:javascript
复制
[[email protected] ~]# ansible all -m shell -a "ls -l /tmp/hosts"
172.16.1.31 | SUCCESS | rc=0 >>
-rw------- 1 root root 357 Oct 19 19:00 /tmp/hosts

172.16.1.41 | SUCCESS | rc=0 >>
-rw------- 1 root root 357 Oct 11 15:12 /tmp/hosts

172.16.1.8 | SUCCESS | rc=0 >>
-rw------- 1 root root 357 Oct 19 19:00 /tmp/hosts

移动远程主机上的文件 remote_src=true 参数

代码语言:javascript
复制
[[email protected] ~]# ansible web -m copy -a " src=/server/scripts/ssh-key.sh  dest=/tmp/ remote_src=true"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "checksum": "d27bd683bd37e15992d2493b50c9410e0f667c9c",
    "dest": "/tmp/ssh-key.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "dc88a3a419e3657bae7d3ef31925cbde",
    "mode": "0644",
    "owner": "root",
    "size": 397,
    "src": "/server/scripts/ssh-key.sh",
    "state": "file",
    "uid": 0
}

定义文件中的内容 content=okay686.cn 默认没有换行

代码语言:javascript
复制
[[email protected] ~]# ansible web -m copy -a "content=okay686.cn dest=/tmp/okay686.txt"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "checksum": "291694840cd9f9c464263ea9b13421d8e74b7d00",
    "dest": "/tmp/okay686.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "0a6bb40847793839366d0ac014616d69",
    "mode": "0644",
    "owner": "root",
    "size": 13,
    "src": "/root/.ansible/tmp/ansible-tmp-1508466752.1-24733562369639/source",
    "state": "file",
    "uid": 0
}
file模块: 设置文件属性

常用参数:

image
image

注意:重命名和创建多级目录不能同时实现

创建目录

代码语言:javascript
复制
[[email protected] ~]# ansible web -m file -a "dest=/tmp/okay686_dir state=directory"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/tmp/okay686_dir",
    "size": 4096,
    "state": "directory",
    "uid": 0
}

创建文件

代码语言:javascript
复制
[[email protected] ~]# ansible web -m file -a "dest=/tmp/okay686_file state=touch"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "dest": "/tmp/okay686_file",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "state": "file",
    "uid": 0
}

创建软连接

代码语言:javascript
复制
[[email protected] ~]# ansible web -m file -a "src=/tmp/okay686_file dest=/tmp/okay686_file_link state=link"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "dest": "/tmp/okay686_file_link",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "size": 16,
    "src": "/tmp/okay686_file",
    "state": "link",
    "uid": 0
}

删除目录文件信息

代码语言:javascript
复制
[[email protected] ~]# ansible web -m file -a "dest=/tmp/okay686_dir state=absent"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "path": "/tmp/okay686",
    "state": "absent"

[[email protected] ~]# ansible web -m file -a "dest=/tmp/okay686_file state=absent"
172.16.1.31 | SUCCESS => {
    "changed": true,
    "path": "/tmp/okay686_file",
    "state": "absent"

创建多级目录(当然不要忘记shell)

代码语言:javascript
复制
[[email protected] ~]# ansible web -m copy -a "src=/etc/hosts dest=/tmp/01/0/0/0/0/0/0/0/"
172.16.1.31 | SUCCESS => {
    "changed": true,
    "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
    "dest": "/tmp/01/0/0/0/0/0/0/0/hosts",
    "gid": 0,
    "group": "root",
    "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",
    "mode": "0644",
    "owner": "root",
    "size": 357,
    "src": "/root/.ansible/tmp/ansible-tmp-1508466973.39-99676412390473/source",
    "state": "file",
    "uid": 0
}

注意:重命名和创建多级目录不能同时实现

fetch 模块:拉取文件

常用参数:

image
image

从远程拉取出来文件

代码语言:javascript
复制
[root@m01 cp]# ansible web -m fetch -a "dest=/tmp/backup src=/etc/hosts"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
    "dest": "/tmp/backup/172.16.1.8/etc/hosts",
    "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",
    "remote_checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
    "remote_md5sum": null
}
代码语言:javascript
复制
[[email protected] cp]# tree /tmp/backup/
/tmp/backup/
├── 172.16.1.31
│   └── etc
│       └── hosts
├── 172.16.1.41
│   └── etc
│       └── hosts
└── 172.16.1.8
    └── etc
        └── hosts

flat 参数,拉取的时候不创建目录(同名文件会覆盖)

代码语言:javascript
复制
[[email protected] tmp]# ansible web -m fetch -a "dest=/tmp/backup/ src=/etc/hosts flat=yes"
172.16.1.8 | SUCCESS => {
    "changed": false,
    "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
    "dest": "/tmp/backup/hosts",
    "file": "/etc/hosts",
    "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308"
mount模块:配置挂载点模块

常用参数:

image
image

挂载

代码语言:javascript
复制
[[email protected] tmp]# ansible 172.16.1.8 -m mount -a "fstype=nfs opts=rw path=/mnt/  src=172.16.1.31:/data/ state=mounted"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "nfs",
    "name": "/mnt/",
    "opts": "rw",
 "passno": "0",
  "src": "172.16.1.31:/data/"
}

卸载

代码语言:javascript
复制
[[email protected] tmp]# ansible 172.16.1.8 -m mount -a "fstype=nfs opts=rw path=/mnt/  src=172.16.1.31:/data/ state=unmounted"
172.16.1.8 | SUCCESS => {
   "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "nfs",
    "name": "/mnt/",
    "opts": "rw",
    "passno": "0",
    "src": "172.16.1.31:/data/"
}
cron模块:定时任务

常用参数:

image
image

添加定时任务

代码语言:javascript
复制
[[email protected] ~]# ansible web -m cron -a "minute=0 hour=0 job='/bin/sh  /server/scripts/hostname.sh &>/dev/null' name=okay686"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
     "okay686"
    ]
}

删除定时任务

只用名字就可以删除

代码语言:javascript
复制
[[email protected] ~]# ansible web -m cron -a "name=okay686  state=absent"
172.16.1.31 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": []
}

注释定时任务

注意: 注释定时任务的时候必须有job的参数

代码语言:javascript
复制
[[email protected] ~]# ansible web -m cron -a "name=okay686 job='/bin/sh  /server/scripts/hostname.sh &>/dev/null'  disabled=yes"
172.16.1.31 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
    "okay686"
    ]
}

取消注释

代码语言:javascript
复制
[[email protected] ~]# ansible web -m cron -a "name=okay686 job='/bin/sh  /server/scripts/hostname.sh &>/dev/null'  disabled=no"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "envs": [],
   "jobs": [
       "okay686"
    ]
}
yum 模块

常用参数:

image
image
代码语言:javascript
复制
[[email protected] ~]# ansible web -m yum -a "name=nmap state=installed  "
172.16.1.31 | SUCCESS => {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror, security\nSetting up Install Process\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * epel: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package nmap.x86_64 2:5.51-6.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch             Version                   Repository      Size\n================================================================================\nInstalling:\n nmap           x86_64           2:5.51-6.el6              base           2.8 M\n\nTransaction Summary\n================================================================================\nInstall       1 Package(s)\n\nTotal download size: 2.8 M\nInstalled size: 9.7 M\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : 2:nmap-5.51-6.el6.x86_64                                     1/1 \n\r  Verifying  : 2:nmap-5.51-6.el6.x86_64                                     1/1 \n\nInstalled:\n  nmap.x86_64 2:5.51-6.el6                                                      \n\nComplete!\n"
    ]
}
service模块:服务管理

常用参数:

image
image

说明 :service 管理的服务必须存在在/etc/init.d/下有的服务脚本

重启定时任务

代码语言:javascript
复制
[[email protected] ~]# ansible web -m service -a "name=crond state=restarted"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "name": "crond",
    "state": "started"
}
其他模块补充
hostname:修改主机名模块
代码语言:javascript
复制
[[email protected] ~]# ansible 172.16.1.8 -m hostname -a "name=web01"
172.16.1.8 | SUCCESS => {
    "ansible_facts": {
        "ansible_domain": "etiantian.org",
        "ansible_fqdn": "www.etiantian.org",
       "ansible_hostname": "web01",
        "ansible_nodename": "web01"
    },
    "changed": false,
    "name": "web01"
}
selinux:管理模块
代码语言:javascript
复制
[[email protected] ~]# ansible 172.16.1.8 -m selinux -a "state=disabled"
172.16.1.8 | SUCCESS => {
    "changed": false,
    "configfile": "/etc/selinux/config",
    "msg": "",
    "policy": "targeted",
    "state": "disabled"
}
get_url 模块 == 【wget】
代码语言:javascript
复制
[[email protected] ~]# ansible 172.16.1.8 -m get_url -a "url=http://lan.Okay686.com/RDPWrap-v1.6.1.zip dest=/tmp/"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "checksum_dest": null,
    "checksum_src": "ad402705624d06a6ff4b5a6a98c55fc2453b3a70",
    "dest": "/tmp/RDPWrap-v1.6.1.zip",
    "gid": 0,
    "group": "root",
    "md5sum": "b04dde546293ade71287071d187ed92d",
    "mode": "0644",
    "msg": "OK (1567232 bytes)",
    "owner": "root",
    "size": 1567232,
    "src": "/tmp/tmp4X4Von",
    "state": "file",
    "status_code": 200,
    "uid": 0,
    "url": "http://lan.Okay686.com/RDPWrap-v1.6.1.zip"
}


url= 下载文件的地址   dest= 下载到哪里

timeout 超时时间

url_password 密码

url_username 用户名
总结:
image
image

ok,如上就是参照之前的笔记+internet整理而来的ansible常用模块介绍!

需多加练习方可体会其中之奥秘!!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/12/17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 常用模块(module_name):
    • ping 模块:测试连通性
      • command 模块: 默认模块
        • shell 模块:万能模块
          • script 模块:执行脚本模块
            • copy模块:把本地文件发送到远端
              • file模块: 设置文件属性
                • fetch 模块:拉取文件
                  • mount模块:配置挂载点模块
                    • cron模块:定时任务
                      • yum 模块
                        • service模块:服务管理
                        • 其他模块补充
                          • hostname:修改主机名模块
                            • selinux:管理模块
                              • get_url 模块 == 【wget】
                              • 总结:
                              领券
                              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档