没有合适的资源?快使用搜索试试~ 我知道了~
首页Ansible常见模块详解.pdf
Ansible常见模块详解.pdf
需积分: 50 28 下载量 70 浏览量
更新于2023-03-16
评论 1
收藏 1.08MB PDF 举报
@author 刘振伟 @QQ 570962906 Ansible拥有大量的模块(module library)来操作远程主机节点或者通过playbook对节点操作。
资源详情
资源评论
资源推荐
Ansible Modules
@author 刘振伟
@QQ 570962906
Ansible拥有大量的模块(module library)来操作远程主机节点或者通过playbook对节点操作。
用户也可以开发自己的模块,这些模块可以控制系统资源,如系统服务,包管理,文件操作或者执行系统命令等等。
Ansible中每个模块都会完成特定的任务。在每个playbook任务定义的任务都会依次得到执行,当然也可以指定值运行某个特定的操作。
ansible webservers -m service -a "name=httpd state=started"
ansible webservers -m ping
ansible webservers -m command -a "/sbin/reboot -t now"
如上边第一个命令是使用service模块在webservres组内的所有主机节点上启动httpd服务,-m指定模块名称,-a指定模块所需的参数, key=value方式
指定模块参数,多个参数以空格隔开。有些模块是不需要带参数的。command或者shell模块则需要指定一个字符串参数,表示要运行的命令。
Ansible通过playbook中所定义的操作以同样的方式执行每个模块:
- name: reboot the servers
action: command /sbin/reboot -t now
也可以以下边的方式定义:
- name: reboot the servers
command: /sbin/reboot -t now
给模块传递参数:
- name: restart webserver
service:
name: httpd
state: restarted
所有的模块包括自己开发的模块都会或者应该返回一个JSON格式的数据。使用playbook的时候,某一个任务可以触发另一个任务的执行。
查看某个某块的文档:
ansible-doc yum
列出所有已经安装的模块:
ansible-doc -l
核心模块核心模块
Ansible会维护一些较为核心的模块,这些模块托管在github上 ansible-modules-core.
附加模块附加模块
这些模块目前随着Ansible安装使用,但以后可能也会独立安装。这些模块大多数由社区维护。非核心模块功能也是较为齐全的,但是其维护及相应效
率有可能没有核心模块那么高。 附加模块也是托管在github上的 ansible-modules-extras。
返回值返回值
Ansible模块通常会返回一个有一定格式的数据(JSON格式的数据),可以将其赋值给一个变量,也可以直接输出到标准输出上。
行为
(Facts)
一些模块会给Ansible返回一个facts,通过ansible_facts,当前主机可以将其直接作为一个变量使用。
状态
(status)
每个模块都必须返回一个状态,来说明模块是否执行成功,或者是说明操作对象是否发生变化(如上传文件到远程机,根据状态可以判断该文件是否
修改过,只有修改过的文件Ansible才会重新上传覆盖)
其他返回信息
通常,模块还会返回成功或者失败的信息。一些模块会直接返回标准输出和标准错误信息,如command和shell模块。如果Ansible见到标准输出信息,
会将其追加到stdou_lines列表中,并打印出来。
介绍几个较为常用的模块介绍几个较为常用的模块
Commands Modules(命令模块命令模块 )
command ---
在远程节点上执行命令
Command模块后边紧跟这要执行的命令,命令的参数以空格隔开。指定的命令会在所选的所有的节点上执行。命令并不是通过shell执行的,所以并不
能使用$HOME 等环境变量和一些操作符(<,>,|,&) 。shell模块支持环境变量和操作符。
选项:
chdir 在运行命令之前,先切换到指定的目录
[root@web1 ~]# ansible webservers -m command -a "ls -l chdir=/tmp"
192.168.1.65 | success | rc=0 >>
total 68
-rw-r--r--. 1 root root 61530 Jul 13 15:59 5kcrm.sql
srwx------. 1 mongod mongod 0 Jul 22 06:28 mongodb-27017.sock
-rw-r--r--. 1 root root 0 Jul 22 11:59 test
drwx------. 2 root root4096 Jul 21 22:49 vmware-root
上边的命令,先切换到/tmp目录下,然后执行ls -l 命令。
creates 后边可以直接指定一个文件名(目录名),或者是以正则模式匹配一系列文件名。如果所指定的文件存在的话,则不执行指定的命令。
[root@web1 ~]# ansible webservers -m command -a "ls -l /tmp creates=/tmp/test"
192.168.1.65 | success | rc=0 >>
skipped, since /tmp/test exists
[root@web1 ~]# ansible webservers -m command -a "ls -l /tmp creates=/tmp/test2"
192.168.1.65 | success | rc=0 >>
total 68
-rw-r--r--. 1 root root 61530 Jul 13 15:59 5kcrm.sql
srwx------. 1 mongod mongod 0 Jul 22 06:28 mongodb-27017.sock
-rw-r--r--. 1 root root 0 Jul 22 11:59 test
drwx------. 2 root root4096 Jul 21 22:49 vmware-root
上边的命令,如果/tmp/test存在的话,则不执行ls -l /tmp 命令。/tmp/test2不存在,执行了ls -l /tmp命令。示例中的test和test2可以是目录也可
以是文件。
removes 后边可以直接指定一个文件名(或目录名),或者是以正则模式匹配一系列文件名。如果所指定的文件不存在的话,则不运行命令。
[root@web1 ~]# ansible webservers -m command -a "ls -l /tmp removes=/tmp/testdir2"
192.168.1.65 | success | rc=0 >>
skipped, since /tmp/testdir2 does not exist
[root@web1 ~]# ansible webservers -m command -a "ls -l /tmp removes=/tmp/testdir"
192.168.1.65 | success | rc=0 >>
total 72
-rw-r--r--. 1 root root 61530 Jul 13 15:59 5kcrm.sql
srwx------. 1 mongod mongod 0 Jul 22 06:28 mongodb-27017.sock
-rw-r--r--. 1 root root 0 Jul 22 11:59 test
drwxr-xr-x. 2 root root4096 Jul 22 12:03 testdir
drwx------. 2 root root4096 Jul 21 22:49 vmware-root
/tmp/testdir2不存在则不执行指定的命令。/tmp/testdir存在则运行命令。
warn 这个是在1.8版本中新增的选项。如果再ansible.cfg配置文件中开启了警告信息,可以只用此选项对当前命令关闭警告,默认为True。
Playbooks示例:
# Example from Ansible Playbooks.
- command: /sbin/shutdown -t now
# Run the command if the specified file does not exist.
- command: /usr/bin/make_database.sh arg1 arg2 creates=/path/to/database
# You can also use the 'args' form to provide the options. This command
# will change the working directory to somedir/ and will only run when
# /path/to/database doesn't exist.
- command: /usr/bin/make_database.sh arg1 arg2
args:
chdir: somedir/
creates: /path/to/database
[root@web1 ~]# cat /etc/ansible/playbook.yml
---
- hosts: webservers
remote_user: root
tasks:
- name: ls -l /tmp
command: /bin/ls -l /tmp removes=/tmp/testdir
[root@web1 ~]# ansible-playbook /etc/ansible/playbook.yml
PLAY [webservers] *************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.1.65]
TASK: [ls -l /tmp] ************************************************************
changed: [192.168.1.65]
PLAY RECAP ********************************************************************
192.168.1.65 : ok=2changed=1unreachable=0failed=0
script
在远程机上运行本地脚本
script模块的-a选项直接跟一个本地脚本的绝对路径,脚本的参数以空格隔开。该模块首先将指定的脚本传到远程节点上,然后在远程节点的shell环
境下执行该脚本。
[root@web1 ~]# cat /root/test.sh
#!/bin/bash
echo $1
[root@web1 ~]# ansible webservers -m script -a "/root/test.sh 12"
192.168.1.65 | success >> {
"changed": true,
"rc": 0,
"stderr": "",
"stdout": "12\n"
}
creates和removes参数与command模块的参数类似:
[root@web1 ~]# ansible webservers -m script -a "/root/test.sh 12 creates=/tmp/test"
192.168.1.65 | success >> {
"changed": false,
"msg": "skipped, since /tmp/test exists"
}
[root@web1 ~]# ansible webservers -m script -a "/root/test.sh 12 creates=/tmp/test2"
192.168.1.65 | success >> {
"changed": true,
"rc": 0,
"stderr": "",
"stdout": "12\n"
}
[root@web1 ~]# ansible webservers -m script -a "/root/test.sh 12 removes=/tmp/test2"
192.168.1.65 | success >> {
"changed": false,
"msg": "skipped, since /tmp/test2 does not exist"
}
[root@web1 ~]# ansible webservers -m script -a "/root/test.sh 12 removes=/tmp/test"
192.168.1.65 | success >> {
"changed": true,
"rc": 0,
"stderr": "",
"stdout": "12\n"
}
Playbooks示例:-v参数显示详细信息
[root@web1 ~]# cat /etc/ansible/playbook.yml
---
- hosts: webservers
remote_user: root
tasks:
# - name: ls -l /tmp
# command: /bin/ls -l /tmp removes=/tmp/testdir
- name: test script module
script: /root/test.sh test
[root@web1 ~]# ansible-playbook /etc/ansible/playbook.yml -v
PLAY [webservers] *************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.1.65]
TASK: [test script module] ****************************************************
changed: [192.168.1.65] => {"changed": true, "rc": 0, "stderr": "", "stdout": "test\n"}
PLAY RECAP ********************************************************************
192.168.1.65 : ok=2changed=1unreachable=0failed=0
通常情况都是将某个功能开发成一个Ansible的模块来使用,而并不是像上边那样在远程节点上运行本地的脚本。
shell
在远程节点执行命令
shell模块的参数为命令名称,命令本身的参数以空格隔开。像command模块那样在远程节点执行命令,但shell 模块再远程节点是通过shell环境
(/bin/bash)执行命令的,该模块也可以执行一个shell脚本,但该脚本必须在远程节点上存在。
chdir、creates 、removes参数与command模块的参数一样。
[root@web1 ~]# ansible webservers -m shell -a "echo $HOME"
192.168.1.65 | success | rc=0 >>
/root
[root@web1 ~]# ansible webservers -m shell -a "echo $HOME removes=/tmp/test"
192.168.1.65 | success | rc=0 >>
/root
[root@web1 ~]# ansible webservers -m shell -a "/root/remote.sh hello removes=/tmp/test"
192.168.1.65 | success | rc=0 >>
I am on remote server:hello
上边执行的/root/remote.sh要在远程节点上存在。
PlayBooks示例:
[root@web1 ~]# cat /etc/ansible/shell.yml
---
- hosts: webservers
remote_user: root
tasks:
- name: shell module 1
#file on the remote
shell: /root/remote.sh world
- name: shell module 2
shell: ls -l
args:
chdir: /tmp/testdir/
[root@web1 ~]# ansible-playbook /etc/ansible/shell.yml -v
PLAY [webservers] *************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.1.65]
TASK: [shell module 1] ********************************************************
changed: [192.168.1.65] => {"changed": true, "cmd": "/root/remote.sh world", "delta": "0:00:00.014310", "end": "2015-07-22 14:17:29.724289", "rc": 0, "start": "2015-07-22 14:17:29.709979", "stderr": "", "stdout": "I am on remote server:world", "warnings": []}
TASK: [shell module 2] ********************************************************
changed: [192.168.1.65] => {"changed": true, "cmd": "ls -l", "delta": "0:00:00.018538", "end": "2015-07-22 14:17:31.158544", "rc": 0, "start": "2015-07-22 14:17:31.140006", "stderr": "", "stdout": "total 0\n-rw-r--r--. 1 root root 0 Jul 22 14:15 testfile", "warnings": []}
PLAY RECAP ********************************************************************
192.168.1.65 : ok=3changed=2unreachable=0failed=0
官方文档
文件相关模块文件相关模块(Files Modules)
官方文档
copy
复制本地文件到远程路径下
copy模块将本地文件复制到远程路径下。fetch 模块将远程文件复制到本地。
copy的选项:
dest 必选参数,为目标文件指定远程节点上的一个绝对路径。如果src 是一个目录,那么该参数也必须是个目录。
src 本地文件的绝对路径,或者相对路径。如果是个路径则会递归复制,路径是以/ 结尾的话,只复制目录里面的内容,如果不以/ 几位的话会复制目
录本身和里面的内容。类似Rsync那样。
backup 可选参数,为源文件创建一个备份文件,被给备份文件添加一个时间戳信息。值为:yes/no,默认为no。
[root@web1 ~]# ansible webservers -m copy -a "src=/root/link/test.sh dest=/root/"
192.168.1.65 | success >> {
"changed": true,
"checksum": "946786e8a3f5f48367c117896960a3516a0d183c",
"dest": "/root/test.sh",
"gid": 0,
"group": "root",
"md5sum": "8d2fb7913d42f3e38ff61ea47bcbc8d0",
"mode": "0644",
剩余63页未读,继续阅读
qq_36937234
- 粉丝: 16
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 27页智慧街道信息化建设综合解决方案.pptx
- 计算机二级Ms-Office选择题汇总.doc
- 单链表的插入和删除实验报告 (2).docx
- 单链表的插入和删除实验报告.pdf
- 物联网智能终端项目设备管理方案.pdf
- 如何打造品牌的模式.doc
- 样式控制与页面布局.pdf
- 武汉理工Java实验报告(二).docx
- 2021线上新品消费趋势报告.pdf
- 第3章 Matlab中的矩阵及其运算.docx
- 基于Web的人力资源管理系统的必要性和可行性.doc
- 基于一阶倒立摆的matlab仿真实验.doc
- 速运公司物流管理模式研究教材
- 大数据与管理.pptx
- 单片机课程设计之步进电机.doc
- 大数据与数据挖掘.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0