手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Ansible服务部署与使用

Ansible模块

[日期:2017-10-28] 来源:Linux社区  作者:znix [字体:大 中 小]

第3章 Ansible中的模块说明

3.1 ping 模块:测试连通性

[root@m01 ~]# 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"
}

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

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

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

[root@m01 ~]# 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.

3.2 command 模块 默认模块

3.2.1 command命令常用参数说明

参数

参数说明

chdir

在执行命令之前,通过cd命令进入到指定目录中

# ansible linuxidc -m command -a "chdir=/tmp ls"

create

定义一个文件是否存在,如果不存在运行相应命令;如果存在跳过此步骤

executable

改变shell使用command进行执行,并且执行时要使用绝对路径

free_form

命令模块采用自由形式命令运行;即可以输入任意linux命令

removes

定义一个文件是否存在,如果存在运行相应命令;如果不存在跳过此步骤

warn

(added in 1.8)

如果ansible配置文件中定义了命令警告,如果参数设置了no/false,将不会警告此行命令

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

[root@m01 ~]# 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模块

[root@m01 ~]# 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参数的使用:

[root@m01 ~]# ansible linuxidc -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 文件是否存在,不存在就执行命令

[root@m01 ~]# ansible linuxidc -m command -a "creates=/etc/hosts date"
172.16.1.31 | SUCCESS | rc=0 >>
skipped, since /etc/hosts exists

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

[root@m01 ~]# ansible linuxidc -m command -a "removes=/etc/hosts date"
172.16.1.31 | SUCCESS | rc=0 >>
Fri Oct 20 13:32:40 CST 2017

3.3 shell模块 万能模块

执行linux命令时可以用

远程节点执行命令

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

[root@m01 ~]# ansible linuxidc -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  ]

3.4 script 模块 执行脚本模块

在本地执行脚本时,将脚本中的内容传输到远程节点上运行

[root@m01 ~]# 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

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

3.5.1 copy模块常用参数

选项参数

选项说明

backup(重要参数)

在覆盖远端服务器文件之前,将远端服务器源文件备份,备份文件包含时间信息。有两个选项:yes|no

content

用于替代"src”,可以直接设定指定文件的值

dest

必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录

directory_mode

递归设定目录的权限,默认为系统默认权限

forces

如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖。

如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes。别名:thirsty

others

所有的file模块里的选项都可以在这里使用

src

被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用"/"来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则包含目录在内的整个内容全部复制,类似于rsync。

mode

定义文件或目录的权限;注意:是4位

owner

修改属主

group

修改属组

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

3.5.2 copy常用命令参数测试

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

[root@m01 ~]# ansible linuxidc -m copy -a "src=/etc/hosts dest=/tmp/ mode=0600 owner=linuxidc group=oldboy "
172.16.1.8 | SUCCESS => {
    "changed": true,
    "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
    "dest": "/tmp/hosts",
    "gid": 500,
    "group": "linuxidc",
    "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",
    "mode": "0600",
    "owner": "linuxidc",
    "size": 357,
    "src": "/root/.ansible/tmp/ansible-tmp-1508410846.63-224022812989166/source",
    "state": "file",
    "uid": 500
}
……

检查结果

[root@m01 ~]# ansible all -m shell -a "ls -l /tmp/hosts"
172.16.1.31 | SUCCESS | rc=0 >>
-rw------- 1 linuxidc oldboy 357 Oct 19 19:00 /tmp/hosts

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

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

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

[root@m01 ~]# ansible linuxidc -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=linuxidcedu.com 默认没有换行

[root@m01 ~]# ansible linuxidc -m copy -a "content=linuxidcedu.com dest=/tmp/linuxidc666.txt"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "checksum": "291694840cd9f9c464263ea9b13421d8e74b7d00",
    "dest": "/tmp/linuxidc666.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
}

3.6 file模块 设置文件属性

3.6.1 file模块常用参数

参数

参数说明

owner

设置复制传输后的数据属主信息

group

设置复制传输后的数据属组信息

mode

设置文件数据权限信息

dest

要创建的文件或目录命令,以及路径信息

src

指定要创建软链接的文件信息

state

state参数信息

directory

创建目录

file

创建文件

link

创建软链接

hard

创建出硬链接

absent

目录将被递归删除以及文件,而链接将被取消链接

touch

创建文件;如果路径不存在将创建一个空文件

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

3.6.2 常用参数测试

创建目录

[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_dir state=directory"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/tmp/linuxidc_dir",
    "size": 4096,
    "state": "directory",
    "uid": 0
}

创建文件

[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_file state=touch"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "dest": "/tmp/linuxidc_file",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "state": "file",
    "uid": 0
} 

创建软连接

[root@m01 ~]# ansible linuxidc -m file -a "src=/tmp/linuxidc_file dest=/tmp/linuxidc_file_link state=link"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "dest": "/tmp/linuxidc_file_link",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "size": 16,
    "src": "/tmp/linuxidc_file",
    "state": "link",
    "uid": 0
}

删除目录文件信息

[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_dir state=absent"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "path": "/tmp/linuxidc_dir",
    "state": "absent"

[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_file state=absent"
172.16.1.31 | SUCCESS => {
    "changed": true,
    "path": "/tmp/linuxidc_file",
    "state": "absent"

创建多级目录

[root@m01 ~]# ansible linuxidc -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
}

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

3.7 fetch 模块  拉取文件

3.7.1 fetch常用参数说明

参数

参数说明

dest

将远程主机拉取过来的文件保存在本地的路径信息

src

指定从远程主机要拉取的文件信息,只能拉取文件

flat

默认设置为no,如果设置为yes,将不显示172.16.1.8/etc/信息

3.7.2 常用参数实例

从远程拉取出来文件

[root@m01 cp]# ansible linuxidc -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
}

[root@m01 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 参数,拉去的时候不创建目录(同名文件会覆盖)

[root@m01 tmp]# ansible linuxidc -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"

3.8 mount模块 配置挂载点模块

3.8.1 mount模块常用参数

参数

参数说明

fstype

指定挂载文件类型 -t nfs == fstype=nfs

opts 

设定挂载的参数选项信息 -o ro  == opts=ro

path 

挂载点路径          path=/mnt

src 

要被挂载的目录信息  src=172.16.1.31:/data

state 

state状态参数

unmounted

加载/etc/fstab文件 实现卸载

absent 

在fstab文件中删除挂载配置

present 

在fstab文件中添加挂载配置

mounted

1.将挂载信息添加到/etc/fstab文件中

2.加载配置文件挂载

3.8.2 mount参数实例

挂载

[root@m01 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/"
} 

卸载

[root@m01 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/"
}

3.9 cron模块 定时任务

3.9.1 cron模块常用参数

参数

参数说明

minute  分

Minute when the job should run ( 0-59, *, */2, etc )

hour    时

Hour when the job should run ( 0-23, *, */2, etc )

day    日

Day of the month the job should run ( 1-31, *, */2, etc )

month  月

Month of the year the job should run ( 1-12, *, */2, etc )

weekday 周

Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )

job     

工作 ;要做的事情

name   

定义定时任务的描述信息

disabled

注释定时任务

state   

state 状态参数

absent

删除定时任务

present

创建定时任务

默认为present

3.9.2 cron模块参数实践

添加定时任务

[root@m01 ~]# ansible linuxidc -m cron -a "minute=0 hour=0 job='/bin/sh  /server/scripts/hostname.sh &>/dev/null' name=linuxidc01"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
     "linuxidc01"
    ]
}

删除定时任务

[root@m01 ~]# ansible linuxidc -m cron -a "minute=00 hour=00 job='/bin/sh  /server/scripts/hostname.sh &>/dev/null' name=linuxidc01 state=absent"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": []
}

只用名字就可以删除

[root@m01 ~]# ansible linuxidc -m cron -a "name=linuxidc01  state=absent"
172.16.1.31 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": []
}

注释定时任务

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

[root@m01 ~]# ansible linuxidc -m cron -a "name=linuxidc01 job='/bin/sh  /server/scripts/hostname.sh &>/dev/null'  disabled=yes"
172.16.1.31 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
    "linuxidc01"
    ]
}

取消注释

[root@m01 ~]# ansible linuxidc -m cron -a "name=linuxidc01 job='/bin/sh  /server/scripts/hostname.sh &>/dev/null'  disabled=no"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "envs": [],
   "jobs": [
       "linuxidc01"
    ]
}

3.10  yum 模块

3.10.1 yum 模块常用参数

参数

参数说明

name=name   

指定安装的软件

state=installed

安装

3.10.2 yum模块参数实践

[root@m01 ~]# ansible linuxidc -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"
    ]
}

3.11 service模块 服务管理

3.11.1 service模块常用参数说明

参数

参数说明

name=service name

服务的名称

state=参数

停止服务 服务状态信息为过去时

stared/stoped/restarted/reloaded

enabled=yes 

设置开机自启动

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

3.11.2 service 模块参数实践

重启定时任务

[root@m01 ~]# ansible linuxidc -m service -a "name=crond state=restarted"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "name": "crond",
    "state": "started"
}

3.12 ansible中的常用模块

常用模块

模块说明

command  (重要模块)

执行命令模块,ansible命令执行默认模块

shell    (重要模块) 

行shell脚本模块

script   (重要模块)

把脚本发到客户端,然后执行;执行脚本命令在远端服务器上

copy      (重要模块) 

把本地文件发送到远端

file             

设定文件属性模块

services         

系统服务管理模块

cron             

计划任务管理模块

yum             

yum软件包安装管理模块

synchronize       

使用rsync同步文件模块

mount             

挂载模块

3.13 其他模块补充

3.13.1 hostname 修改主机名模块

[root@m01 ~]# 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"
}

3.13.2 selinux 管理模块

[root@m01 ~]# 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"
}

3.13.3 get_url 模块 == 【wget】

[root@m01 ~]# ansible 172.16.1.8 -m get_url -a "url=http://lan.linuxidc.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.linuxidc.com/RDPWrap-v1.6.1.zip"
}

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

  timeout 超时时间

  url_password   密码

  url_username  用户名

linux
【内容导航】
第1页:Ansible使用前提 第2页:Ansible介绍
第3页:Ansible模块 第4页:ansible-playbook剧本
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款