Ansible安装完成之后就自带很多命令,其中较常用的有7个:

  • ansible
  • ansible-doc
  • ansible-galaxy
  • ansible-init
  • ansible-playbook
  • ansible-pull
  • ansible-vault

ansible

ansible -h
Usage: ansible <host-pattern> [options]

  对本机执行一个命令:

ansible 127.0.0.1 -a "date"

127.0.0.1 | SUCCESS | rc=0 >>
Fri Apr 6 18:45:01 CST 2018

  在远程主机上执行命令;

ansible test -a "date"

172.16.1.10 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added '172.16.1.10' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).\r\n",
"unreachable": true
}

由上可知:在远程主机上执行命令的时候没有权限(密码不知道),所以我们可以在上述命令的基础上加上-k参数:

ansible test -a "date" -k

SSH password:
172.16.1.10 | SUCCESS | rc=0 >>
Fri Apr 6 18:56:52 CST 2018

  ansible命令的常用参数;

-a MODULE_ARGS, --args=MODULE_ARGS module arguments  命令行参数
--ask-vault-pass ask for vault password
-B SECONDS, --background=SECONDS
-C, --check don't make any changes; instead, try to predict some
-D, --diff when changing (small) files and templates, show the
-e EXTRA_VARS, --extra-vars=EXTRA_VARS
-f FORKS, --forks=FORKS # 指定执行任务时并发数量
-i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY #指定库存主机文件的路径,默认为/etc/ansible/hosts
-l SUBSET, --limit=SUBSET
--list-hosts outputs a list of matching hosts; does not execute
-m MODULE_NAME, --module-name=MODULE_NAME module name to execute (default=command) # 执行模块的名称,默认使用command模块
-M MODULE_PATH, --module-path=MODULE_PATH
-u REMOTE_USER, --user=REMOTE_USER # 执行用户,使用这个远程用户而不是当前用户
-U SUDO_USER, --sudo-user=SUDO_USER # sudo到哪个用户,默认为root
-k, --ask-pass ask for connection password # 登陆密码,提示输入ssh密码
-K, --ask-become-pass # 提示密码使用sudo -s --sudo sudo运行
--new-vault-id=NEW_VAULT_ID the new vault identity to use for rekey
--new-vault-password-file=NEW_VAULT_PASSWORD_FILES new vault password file for rekey
-o, --one-line condense output
-P POLL_INTERVAL, --poll=POLL_INTERVAL set the poll interval if using -B (default=15)
--syntax-check perform a syntax check on the playbook, but do not execute it
-t TREE, --tree=TREE log output to this directory
--vault-id=VAULT_IDS the vault identity to use
--vault-password-file=VAULT_PASSWORD_FILES vault password file
-v, --verbose verbose mode (-vvv for more, -vvvv to enable connection debugging)
--version show program's version number and exit
--private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
-c CONNECTION, --connection=CONNECTION
-T TIMEOUT, --timeout=TIMEOUT
--ssh-common-args=SSH_COMMON_ARGS
--sftp-extra-args=SFTP_EXTRA_ARGS
--scp-extra-args=SCP_EXTRA_ARGS
--ssh-extra-args=SSH_EXTRA_ARGS
-s, --sudo run operations with sudo (nopasswd) (deprecated, use become)
-S, --su run operations with su (deprecated, use become)
-R SU_USER, --su-user=SU_USER
-b, --become run operations with become (does not imply password prompting)
--become-method=BECOME_METHOD privilege escalation method to use (default=sudo), valid choices: [ sudo | su | pbrun | pfexec | doas | dzdo | ksu | runas | pmrun ]
--become-user=BECOME_USER run operations as this user (default=root)
--ask-sudo-pass ask for sudo password (deprecated, use become)
--ask-su-pass ask for su password (deprecated, use become)

ansible-doc

ansible-doc -h
Usage: ansible-doc [-l|-s] [options] [-t <plugin type] [plugin] plugin documentation tool Options:
-a, --all **For internal testing only** Show documentation for
all plugins.
-h, --help show this help message and exit
-l, --list List available plugins
-M MODULE_PATH, --module-path=MODULE_PATH
prepend colon-separated path(s) to module library
(default=[u'/root/.ansible/plugins/modules',
u'/usr/share/ansible/plugins/modules'])
-s, --snippet Show playbook snippet for specified plugin(s)
-t TYPE, --type=TYPE Choose which plugin type (defaults to "module")
-v, --verbose verbose mode (-vvv for more, -vvvv to enable
connection debugging)
--version show program's version number and exit See man pages for Ansible CLI options or website for tutorials
https://docs.ansible.com

ansible-doc 相当于一个帮助文档,结合-s参数

ansible-doc -s file

- name: Sets attributes of files
file:
attributes: # Attributes the file or directory should have. To get supported flags look at the man page for `chattr' on the
target system. This string should contain the attributes in the same order as the
one displayed by `lsattr'.
follow: # This flag indicates that filesystem links, if they exist, should be followed.
force: # force the creation of the symlinks in two cases: the source file does not exist (but will appear later); the
destination exists and is a file (so, we need to unlink the "path" file and create
symlink to the "src" file in place of it).
group: # Name of the group that should own the file/directory, as would be fed to `chown'.
mode: # Mode the file or directory should be. For those used to `/usr/bin/chmod' remember that modes are actually octal
numbers (like 0644). Leaving off the leading zero will likely have unexpected
results. As of version 1.8, the mode may be specified as a symbolic mode (for
example, `u+rwx' or `u=rw,g=r,o=r').
owner: # Name of the user that should own the file/directory, as would be fed to `chown'.
path: # (required) path to the file being managed. Aliases: `dest', `name'
recurse: # recursively set the specified file attributes (applies only to state=directory)
selevel: # Level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the `range'. `_default'
feature works as for `seuser'.
serole: # Role part of SELinux file context, `_default' feature works as for `seuser'.
setype: # Type part of SELinux file context, `_default' feature works as for `seuser'.
seuser: # User part of SELinux file context. Will default to system policy, if applicable. If set to `_default', it will use
the `user' portion of the policy if available.
src: # path of the file to link to (applies only to `state=link'). Will accept absolute, relative and nonexisting paths.
Relative paths are not expanded.
state: # If `directory', all immediate subdirectories will be created if they do not exist, since 1.7 they will be created
with the supplied permissions. If `file', the file will NOT be created if it does
not exist, see the [copy] or [template] module if you want that behavior. If
`link', the symbolic link will be created or changed. Use `hard' for hardlinks. If
`absent', directories will be recursively deleted, and files or symlinks will be
unlinked. Note that `absent' will not cause `file' to fail if the `path' does not
exist as the state did not change. If `touch' (new in 1.4), an empty file will be
created if the `path' does not exist, while an existing file or directory will
receive updated file access and modification times (similar to the way `touch`
works from the command line).
unsafe_writes: # Normally this module uses atomic operations to prevent data corruption or inconsistent reads from the target
files, sometimes systems are configured or just broken in ways that prevent this.
One example are docker mounted files, they cannot be updated atomically and can
only be done in an unsafe manner. This boolean option allows ansible to fall back
to unsafe methods of updating files for those cases in which you do not have any
other choice. Be aware that this is subject to race conditions and can lead to data
corruption.

  ansible新版已经添加了操作docker的相关模块:

ansible-doc -l |grep docker

docker                                    manage docker containers
docker_container manage docker containers
docker_image Manage docker images.
docker_image_facts Inspect docker images
docker_login Log into a Docker registry.
docker_network Manage Docker networks
docker_secret Manage docker secrets.
docker_service Manage docker services and containers.
docker_volume Manage Docker volumes

ansible-galaxy

  从官网下载其他第三方模块和插件。

ansible-galaxy install username.rolename

ansible-init

  ansible-init是对Ansible的playbook进行语法检查的一个工具。

ansible-playbook

  该命令时使用最多的命令,其通过读取playbook文件后,执行相应的动作。

ansible-pull

  ansible的另一种模式-- pull模式。跟平常使用的push模式刚好相反,适用于以下场景;

  • 操作巨大数量的主机,即使使用非常高的线程还是需要很长时间;
  • 在没有网络连接的主机上运行Ansible时;

ansible-vault

  ansible-vault主要应用于配置文件中含有敏感信息的时候,不希望别人看到,这时使用这个命令可以将某些配置文件加密,属于高级用法。主要对于playbook里涉及到配置密码或者其他敏感信息时,可以使用该命令加密。在使用类似cat等查看命令查看加密后的配置文件时,需要输入密码才可以查看。这种playbook执行时,需要加上--ask-vault-pass参数,同样需要输入密码之后才能执行。

Ansible基本命令的更多相关文章

  1. ansible基本命令及剧本

    ansible常用命令 1. -v, –verbose 详细模式,如果命令执行成功,输出详细的结果(-vv –vvv -vvvv) 2. -i, –inventory=PATH 指定host文件的路径 ...

  2. ansible 基本命令学习与踩坑

    1. 命令行参数 -v,–verbose 详细模式,如果命令执行成功,输出详细的结果(-vv –vvv -vvvv) -i PATH,–inventory=PATH 指定host文件的路径,默认是在/ ...

  3. 初探ansible

    Ansible 基于ssh的自动化运维工具 ansible 配置文件详解 ansible.cfg 文件 文件默认放置在/etc/ansible下,ansible读取配置文件的顺序是: 当前命令执行目录 ...

  4. 【原创】Centos8使用ansible

    目录 使用ansible发布公钥 ansible基本命令 ansbile配置文件详解 一.使用ansible发布公钥 1.0 生成秘钥对 1.生成命令 ssh-keygen -t rsa# 推送单个公 ...

  5. ansible指路篇-安装及基本命令使用

    ansible指路篇-安装及基本命令使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是ansible ansible是新出现的自动化运维工具,基于Python开发,集合 ...

  6. ansible服务部署与使用

    第1章 ssh+key实现基于密钥连接(ansible使用前提) 说明:    ansible其功能实现基于SSH远程连接服务    使用ansible需要首先实现ssh密钥连接 1.1 部署ssh ...

  7. 自动化工具-ansible服务部署与使用

    1.前言 1.1ansible软件介绍 python 语言是运维人员必须会的语言 ansible 是一个基于python 开发的自动化运维工具 其功能实现基于ssh远程连接服务 ansible 可以实 ...

  8. ansible服务的部署与使用

      简介:   ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序 ...

  9. Ansible自动化运维工具

    ansible软件介绍 python语言是运维人员必会的语言!  ansible是一个基于Python开发的自动化运维工具!(saltstack)  其功能实现基于SSH远程连接服务!  ans ...

随机推荐

  1. go 格式化时间戳

    func main() { //获取时间戳 timestamp := time.Now().Unix() fmt.Println(timestamp) //格式化为字符串,tm为Time类型 tm : ...

  2. centos 网卡聚合及Cisco交换机链路聚合

    一.配置环境 centos 系统.网卡1口和2口做链路聚合.    交换机网口 6口和7口. 二.服务器操作步骤 centos 6 1.创建一个channel bonding interface #v ...

  3. Segments(叉积)

    Segments http://poj.org/problem?id=3304 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: ...

  4. c++流操作

    非缓冲标准出错流对象cerr和缓冲标准出错流对象clog,它们都是来自于ostream类的对象,用于输出错信息.cerr和clog之间的不同之处在于cerr是不经过缓冲区直接向显示器输出有关信息,而c ...

  5. 18-从n个数中选m个

    #include <iostream>using namespace std; int f(int n, int m){        if(n < m)         //这个条 ...

  6. 通过dockerfile构建nginx

    上次 利用命令行的形式来构建nginx服务, http://www.cnblogs.com/loveyouyou616/p/6806788.html 这次利用dockerfile文件来构建nginx服 ...

  7. jetty 8.0 add filter example

    http://zyn010101.iteye.com/blog/1679798 package com.cicc.gaf.sso.server;import java.io.IOException;i ...

  8. java 错误: 找不到或无法加载主类

    这个问题应该很常见的,笔者经常手工编译一些测试代码或者小工具,经常用到 javac和java来编译并运行一些简单的小工具. 以Hello World来测试. HelloWorld.java publi ...

  9. Python Socket 编程详细介绍(转)

    Python 提供了两个基本的 socket 模块: Socket 它提供了标准的BSD Socket API. SocketServer 它提供了服务器重心,可以简化网络服务器的开发. 下面讲解下 ...

  10. .zip/.rar打包与解压

    Linux下如何解压.zip和.rar文件,对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们: 1)对于zip linux下提供了zip和unzip程序,zip是 ...