ansible常用命令

来源:互联网 发布:烟花算法 编辑:程序博客网 时间:2024/04/30 02:50
常用模块:
!所有示例以webserver为匹配目标主机。
1.ping
ansible all -m ping
!检测机器是否可登录,ping模块不需要传送参数
!注:这里的ping模块并非调用了系统的ping命令,而是类似于登录到远程机器再echo出一个信息。

2.command
!可以执行任意命令,但不接受管道命令和重定向符号,如果想使用这些,则需要使用Shell模块。
ansible all -m command -a "ls" #在所有匹配主机上执行ls命令

playbook:
- name: test for command
command: ls

3.copy
#复制一个文件到远程服务器

ansible all -m copy -a "src=/tmp/text.txt dest=/home/tmp/"
#将本地text.txt文件复制到所有匹配主机的/home/tmp/路径下。

playbook:
- name: test for copy
copy: src=/tmp/text.txt dest=/home/tmp/ force=yes

4.file
这个模块这次构建系统中并没有用到,官方的解释是用来设置文件、文件夹或快链的属性,或者删除文件、快链、文件夹。
创建一个文件夹,如果目标不存在
ansible webservers -m file -a "path=/tmp/tdir state=directory mode=0755"
playbook
- name: create a directory if it doesn't exist
- file:
path: /etc/some_directory
state: directory
mode: 0755


5.get_url
!从服务器上下载一个文件到远程主机指定的目录
ansible webservers -m get_url -a "url='http://baidu.com dest=/tmp/ba.html"

playbook
- name: download foo.conf
get_url:
url: http://example.com/path/file.conf
dest: /etc/foo.conf
mode: 0440

6.yum
特别适用于批量安装一些依赖包的时候

ansible webservers -m yum -a "name=httpd state=latest"
ansible webservers -m yum -a "name=httpd state=absent" !删除包

playbook
- name: install the latest version of Apache
yum:
name: httpd
state: latest

7.service
控制远程服务器上的服务
ansible webservers -m service -a "name=nginx state=restart"
playbook
- name: restart rmote nginx
service: name=nginx state=restart

8.setup
收集远程服务器信息
ansible all -m setup
在playbook中,这项任务默认是执行的,不需要列出。
0 0
原创粉丝点击