1 ansible命令详解

ansible命令的语法格式如下:

ansible <host-pattern> [-m module_name] [-a args]

host-pattern使用说明:

# 匹配所有主机all
ansible all -m ping # 通配符
ansible "*" -m ping <==匹配所有主机同all
ansible 10.0.0.* -m ping <==匹配10.0.0网段的所有主机 # 与:在webservers组;并且在dbservers中的主机;
ansible "webservers:&dbservers" -m ping # 或:在webservers组,或者在appservers中的主机;
ansible "webservers:appservers" -m ping # 非:在webservers组,但不在apps组中的主机
ansible 'webservers:!apps' -m ping #属于web或db但不属于app排除ftp组内的主机
ansible 'web:db:&app:!ftp' -m ping # 正则表达式:匹配以web或者db服务支持的所有example.com域名
ansible "~(web|db).*\.example\.com" -m ping

选项说明如下:

选项 说明
-m module 指定模块,默认为command
-a args 模块参数,没有参数可忽略
--version 显示版本
--list-hosts 显示主机列表,可简写 --list
-k, --ask-pass 当使用ssh密码认证时,提示输入ssh连接密码,默认基于秘钥验证
-K, --ask-become-pass 提示输入sudo时的口令
-C, --check 仅检查,并不执行
-T, --timeout=TIMEOUT 执行命令的超时时间,默认10s
-U, --user=REMOTE_USER 远程执行命令的用户
-b, --become 代替旧版的sudo 切换
-v 详细过程 –vv 、-vvv更详细
  • 示例一:使用秘钥验证ansible是否成功安装,使用ping模块检测:

    [root@xuzhichao ~]# ansible all -m ping
    192.168.20.23 | SUCCESS => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
    }
    192.168.20.22 | SUCCESS => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
    }
  • 示例二:使用ssh密码的方式管理被控端:

    [root@xuzhichao ~]# ansible all -m ping -k
    SSH password:
    192.168.20.22 | SUCCESS => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
    }
    192.168.20.23 | SUCCESS => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
    }
  • 示例三:使用普通用户xu在远程主机在使用sudo切换到root身份执行命令:

    [root@nginx02 ~]# visudo
    xu ALL=(ALL) ALL #第一个密码是ssh的密码(可以通过key验证解决),第二个密码是sudo的密码(可以通过visudo中的NOPASSWD选项解决)
    [root@xuzhichao ~]# ansible all -u xu -a "id" -k -K -b
    SSH password:
    BECOME password[defaults to SSH password]:
    192.168.20.22 | CHANGED | rc=0 >>
    uid=0(root) gid=0(root) groups=0(root)
    192.168.20.23 | CHANGED | rc=0 >>
    uid=0(root) gid=0(root) groups=0(root)

2 ansible-doc显示模块帮助信息

ansible-doc语法格式如下:

ansible-doc [options] [module…]

常用选项如下:

选项 说明
-a 显示所有模块的文档
-l, --list 列出可用模块
-s, --snippet 显示指定模块的playbook片段
  • 示例一:查看shell模块的帮助信息:

    [root@xuzhichao ~]# ansible-doc shell
    > SHELL (/usr/lib/python2.7/site-packages/ansible/modules/commands/shell.py) The `shell' module takes the command name followed by a list of space-delimited arguments. Either a free form command
    or `cmd' parameter is required, see the examples. It is almost exactly like the [command] module but runs the command
    through a shell (`/bin/sh') on the remote node. For Windows targets, use the [win_shell] module instead. * This module is maintained by The Ansible Core Team
    * note: This module has a corresponding action plugin. OPTIONS (= is mandatory): <==模块选项 - chdir
    Change into this directory before running the command.
    [Default: (null)]
    type: path
    version_added: 0.6 - cmd
    The command to run followed by optional arguments.
    [Default: (null)]
    type: str - creates
    A filename, when it already exists, this step will *not* be run.
    [Default: (null)]
    type: path EXAMPLES: <==使用示例 - name: Execute the command in remote shell; stdout goes to the specified file on the remote.
    shell: somescript.sh >> somelog.txt - name: Change the working directory to somedir/ before executing the command.
    shell: somescript.sh >> somelog.txt
    args:
    chdir: somedir/ # You can also use the 'args' form to provide the options.
    - name: This command will change the working directory to somedir/ and will only run when somedir/somelog.txt doesn't exist.
    shell: somescript.sh >> somelog.txt
    args:
    chdir: somedir/
    creates: somelog.txt # You can also use the 'cmd' parameter instead of free form format.
    - name: This command will change the working directory to somedir/.
    shell:
    cmd: ls -l | grep log
    chdir: somedir/ - name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards together but bash does)
    shell: cat < /tmp/*txt
    args:
    executable: /bin/bash - name: Run a command using a templated variable (always use quote filter to avoid injection)
    shell: cat {{ myfile|quote }} # You can use shell to run other executables to perform actions inline
    - name: Run expect to wait for a successful PXE boot via out-of-band CIMC
    shell: |
    set timeout 300
    spawn ssh admin@{{ cimc_host }} expect "password:"
    send "{{ cimc_password }}\n"
  • 示例二:查看ansible共加载了多少模块:

    [root@xuzhichao ~]# ansible-doc -l | wc -l
    3387
  • 示例三:查看模块的简要说明,主要包括用法和选项:

    [root@xuzhichao ~]# ansible-doc -s shell
    - name: Execute shell commands on targets
    shell:
    chdir: # Change into this directory before running the command.
    cmd: # The command to run followed by optional arguments.
    creates: # A filename, when it already exists, this step will *not* be run.
    executable: # Change the shell used to execute the command. This expects an absolute path to the executable.
    free_form: # The shell module takes a free form command to run, as a string. There is no actual parameter named 'free form'. See the
    examples on how to use this module.
    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.
    stdin_add_newline: # Whether to append a newline to stdin data.
    warn: # Whether to enable task warnings.

3 ansible-playbook

功能:用于执行配置好的剧本。

语法格式为:

ansible-playbook <filename.yml> ... [options]

常用选项如下:

选项 说明
-C,--check 只检测可能会发生的改变,但不真正执行操作
--list-hosts 列出运行任务的主机
--limit 主机列表 只针对主机列表中的主机执行
-v 显示过程 -vv -vvv 更详细
--syntax-check 检查语法
-e 向playbook命令中传递变量
-i 指定inventory主机清单文件,默认为/etc/ansible/roles

示例:

#只检测
ansible-playbook file.yml --check #执行剧本
ansible-playbook file.yml #执行剧本,只针对hosts中的websrvs组
ansible-playbook file.yml --limit websrvs

4 ansible-galaxy

  • 主要功能:管理从 https://galaxy.ansible.com 下载的各种roles

  • 获取galaxy

    https://galaxy.ansible.com获取,选取相应roles,复制下载命令

  • 列出所有已安装的galaxy

    [root@xuzhichao ~]# ansible-galaxy list
    # /root/.ansible/roles
    - geerlingguy.apache, 3.1.4
    # /usr/share/ansible/roles
    # /etc/ansible/roles
  • 安装galaxy

    [root@xuzhichao ~]# ansible-galaxy install geerlingguy.apache
    - downloading role 'apache', owned by geerlingguy
    - downloading role from https://github.com/geerlingguy/ansible-role-apache/archive/3.1.4.tar.gz
    - extracting geerlingguy.apache to /root/.ansible/roles/geerlingguy.apache
    - geerlingguy.apache (3.1.4) was installed successfully
  • 删除galaxy:

    [root@xuzhichao ~]# ansible-galaxy remove geerlingguy.apache
    - successfully removed geerlingguy.apache

5 ansible-console

Ansible-console:2.0+新增,可交互执行命令。

使用示例如下:

[root@xuzhichao ~]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands. root@all (2)[f:5]# forks 5 <==设置并发数
root@all (2)[f:5]# cd NginxWebs <==切换主机组
root@NginxWebs (2)[f:5]# list <==列出主机组的成员
192.168.20.22
192.168.20.23
root@NginxWebs (2)[f:5]# shell df <==直接输入模块和服务名,不需要加-m和-a
192.168.20.23 | CHANGED | rc=0 >>
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 485896 0 485896 0% /dev
tmpfs 497840 0 497840 0% /dev/shm
tmpfs 497840 7864 489976 2% /run
tmpfs 497840 0 497840 0% /sys/fs/cgroup
/dev/mapper/centos-root 52403200 3284320 49118880 7% /
/dev/sda1 1038336 139940 898396 14% /boot
/dev/mapper/centos-home 154057220 119636 153937584 1% /data
tmpfs 99572 0 99572 0% /run/user/0
192.168.20.22 | CHANGED | rc=0 >>
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 485896 0 485896 0% /dev
tmpfs 497840 0 497840 0% /dev/shm
tmpfs 497840 7924 489916 2% /run
tmpfs 497840 0 497840 0% /sys/fs/cgroup
/dev/mapper/centos-root 52403200 3338584 49064616 7% /
/dev/sda1 1038336 139940 898396 14% /boot
/dev/mapper/centos-home 154057220 202628 153854592 1% /data
tmpfs 99572 0 99572 0% /run/user/0
root@NginxWebs (2)[f:5]# help <==列出所有的内置命令 Documented commands (type help <topic>):
========================================
EOF
a10
a10_server
a10_server_axapi3
a10_service_group
a10_virtual_server
accelerate
aci
......

ansible(3)--ansible的相关命令行工具的更多相关文章

  1. TimesTen ODBC 链接库差异及相关命令行工具的使用注意事项

    1. TimesTen有两种访问模式:Direct模式和Client/Server模式,以下为来自Operations Guide 的描述 Connecting using TimesTen ODBC ...

  2. mysql命令行工具

    mysql包相关命令行工具 [root@manage ~]# rpm -qa|grep mysql mysql-server-5.1.73-5.el6_7.1.x86_64 mysql-5.1.73- ...

  3. python开发简单的命令行工具

    介绍 Python模块argparse,这是一个命令行选项,参数和子命令的解释器,使用该模块可以编写友好的命令行工具,在程序中定义好需要的参数,argparse将弄清楚如何解析 sys.argv中的参 ...

  4. Linux 性能监控之命令行工具

    引言 对于系统和网络管理员来说每天监控和调试Linux系统的性能问题是一项繁重的工作.这些命令行工具可以在各种Linux系统下使用,可以用于监控和查找产生性能问题的原因.这个命令行工具列表提供了足够的 ...

  5. MySQL 命令行工具之 mysqldump 深入研究

    mysqldump 是MySQL的一个命令行工具,用于逻辑备份.可以将数据库和表的结构,以及表中的数据分别导出成:create database, create table, insert into的 ...

  6. 从零开始打造个人专属命令行工具集——yargs完全指南

    前言 使用命令行程序对程序员来说很常见,就算是前端工程师或者开发gui的,也需要使用命令行来编译程序或者打包程序 熟练使用命令行工具能极大的提高开发效率,linux自带的命令行工具都非常的有用,但是这 ...

  7. 20个命令行工具监控Linux系统性能

    作为Linux/Unix 系统管理员需要掌握一些常用的工具用于检测系统性能.在这里,dodo为大家推荐非常20个有用的并且最常用的命令行系统监视工具: 1. top -Linux系统进程监控 top ...

  8. 如何用SQL命令行工具删除dedecms指定id文章

    用dedecms采集时标题字段设置错了,出现了注释符号<!---->,导致后台的文章列表出现错误,也无法直接从列表中删除,可以远程登录数据库去操作,这个相对比较麻烦,想着直接从后台的SQL ...

  9. Nmcli 网络管理命令行工具基础

    介绍 在本教程中,我们会在CentOS / RHEL 7中讨论网络管理命令行工具NetworkManager command line tool,也叫nmcli.那些使用ifconfig的用户应该在C ...

  10. ZooKeeper系列3:ZooKeeper命令、命令行工具及简单操作

    问题导读1.ZooKeeper包含哪些常用命令?2.通过什么命令可以列出服务器 watch 的详细信息?3.ZooKeeper包含哪些操作?4.ZooKeeper如何创建zookeeper? 常用命令 ...

随机推荐

  1. 学习Python前要了解的tips

    学习Python前要了解的tips 对后续的学习来说很重要,否则后续会出现一些奇奇怪怪的问题,而且很难找到解决方法.嘿嘿,就不要问我怎么知道的了吧,好多都是我踩过的坑 卸载电脑内软件 之前我一直用电脑 ...

  2. 【已解决】Python使用sql语句模糊查询报错:python中TypeError: not enough arguments for format string

    2021-03-26-19:54:13 今天下午对世界疫情详情数据库进行模糊查询. 首先要明确一点:使用模糊查询就会用到WHERE语句中的LIKE语句,主要说一下在使用 % 的时候要注意的点: 在Py ...

  3. C++设计模式 - 解析器模式(Interpreter)

    领域规则模式 在特定领域中,某些变化虽然频繁,但可以抽象为某种规则.这时候,结合特定领域,将问题抽象为语法规则,从而给出在该领域下的一般性解决方案. 典型模式 Interpreter Interpre ...

  4. 5W1H聊开源之What——开源协议有哪些?

    开源许可协议是指开源社区为了维护作者和贡献者的合法权利,保证软件不被一些商业机构或个人窃取,影响软件的发展而开发的协议.开源协议规定了用户在使用开源软件时的权利和责任,虽然不一定具备法律效力,但是当涉 ...

  5. #杜教筛,欧拉函数,整除分块#HDU 6683 Rikka with Geometric Sequen

    题目 由\(1,2,\dots,n-1,n\)组成的序列中有多少个子序列是等比数列\((n\leq 5*10^{17})\) 分析 分类讨论,先设公比为\(q=\frac{i}{j}[gcd(i,j) ...

  6. C# 继承、多态性、抽象和接口详解:从入门到精通

    C# 继承 在 C# 中,可以将字段和方法从一个类继承到另一个类.我们将"继承概念"分为两类: 派生类(子类) - 从另一个类继承的类 基类(父类) - 被继承的类 要从一个类继承 ...

  7. VS 2020制作安装包

    VS制作安装包的一般步骤. 一·新建项目 (1)新建 (2)界面跳转 二·添加引用 (1)添加卸载程序 1.在'C:WINDOWSsystem32'路径下,找到msiexec.exe . 2.将msi ...

  8. Windows下Net6开源akstream项目vs2022调试GB28181协议对接摄像头全流程

    一.背景介绍 笔者经历多个项目对接摄像头需求,不同项目具体要求又有所不同,碰到的摄像头对接开发问题,整理记录.此篇主要用于记录备用及给有缘人提供解决思路等. 1.   同一局域网对接(海康摄像头),如 ...

  9. 自动编号工具类:NumAutoUtils详解

    在软件开发中,经常需要生成唯一的编号,例如订单号.发票号.实验编号等.为了简化这一过程,本文将介绍一个Java工具类NumAutoUtils,它可以帮助我们生成带有前缀和日期的自动编号. 概述 Num ...

  10. 华为Push用户增长服务:精准触达,加速增长

    速戳了解华为Push用户增长服务:通过精细化运营,助力开发者高效实现用户增长,提升用户活跃度和粘性! 合作咨询请点此链接 了解更多详情>> 访问华为开发者联盟官网 获取开发指导文档 华为移 ...