03-ansible管理命令

             <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-211130ba7a.css">
                        <div id="content_views" class="markdown_views prism-tomorrow-night-eighties">
                <!-- flowchart 箭头图标 勿删 -->
                <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                    <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                </svg>
                                        <h4><a id="Ansible__0" target="_blank"></a>一、Ansible 管理命令</h4>

Ansible 管理命令有如下几个:
(1)ansible:这个命令是日常工作中使用率非常高的命令之一,主要用于临时一次性操作;
(2)ansible-doc:Ansible 模块文档说明,针对每个模块都有详细的用法说明和应用案例介绍;
(3)ansible-galaxy:可以简单的理解为 Github 或 PIP 的功能,是 Ansible 官方一个分享 role 的功能平台。可以通过 ansible-galaxy 命令很简单的实现 role 的分享和安装;
(4)ansible-playbook:是日常应用中使用频率最高的命令,其工作机制是,通过读取预先编写好的playbook文件实现批量管理;
(5)ansible-pull:Ansible 的另一种工作模式,pull 模式,Ansible 默认使用 push 模式;
(6)ansible-vault:主要用于配置文件加密;
(7)ansible-console:让用户可以在 ansible-console 虚拟出来的终端上像 Shell 一样使用 Ansible 内置的各种命令。

下面对常用的管理命令做介绍。

二、ansible 命令

ansible 是日常工作中使用率非常高的命令之一,其主要在如下场景使用:
(1)非固化需求
(2)临时一次性操作
(3)二次开发接口调用

该命令使用方式如下:

ansible <host-pattern> [options]

 
 
  • 1

比如:工作中我临时想查看 web1 服务器是否存活,或我想临时复制本地的 /etc/fstab 到 web1 服务器的 /tmp 目录下做测试等等。

具体的命令使用如下:
检查服务器存活

ansible web1 -m ping

 
 
  • 1

复制本地文件到远程

ansible web1 -m copy -a "src=/etc/fstab dest=/tmp/fstab owner=root group=root mode=644 backup=yes"

 
 
  • 1

详细参数可以查看帮助:

[root@ansible-manager ~]# ansible -h
Usage: ansible <host-pattern> [options]

Define and run a single task ‘playbook’ against a set of hosts

Options:
-a MODULE_ARGS, args=MODULE_ARGS
module arguments
ask-vault-pass ask for vault password
-B SECONDS, background=SECONDS
run asynchronously, failing after X seconds
(default=N/A)
-C, check don’t make any changes; instead, try to predict some
of the changes that may occur
-D, diff when changing (small) files and templates, show the
differences in those files; works great with check
-e EXTRA_VARS, extra-vars=EXTRA_VARS
set additional variables as key=value or YAML/JSON, if
filename prepend with @
-f FORKS, forks=FORKS
specify number of parallel processes to use
(default=5)

Some modules do not make sense in Ad-Hoc (include, meta, etc)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

相关选项都有解释,这里不再详述。

三、ansible-doc 命令

ansible-doc 是 Ansible 模块文档说明,针对每个模块都有详细的用法说明及应用案例介绍,功能和 Linux 系统 man 命令类似。该命令使用方式如下:

ansible-doc [options] [module...]

 
 
  • 1

ansible-doc 命令后跟[options]参数或[模块名],显示模块用法说明,具体示例如下:

列出支持的模块

ansible-doc -l

 
 
  • 1

ping 模块功能说明

ansible-doc ping

 
 
  • 1
四、ansible-playbook 命令

ansible-playbook 是日常应用中使用频率最高的命令,其工作机制是通过读取预先编写好的 playbook文件实现批量管理。要实现的功能与命令 ansible 是一样的,可以理解为按一定条件组成的 ansible 任务集。

ansible-playbook 命令后跟 YML 格式的 playbook 文件,执行事先编排好的任务集,命令使用方式如下:

ansible-playbook playbook.yml

 
 
  • 1

具体示例如下:
执行 nginx.yml 这个 playbook 中定义的所有任务集

ansible-playbook nginx.yml

 
 
  • 1

playbook 具有编写简单、可定制性高、灵活方便同时可固化日常所有操作的特点,运维人员应熟练掌握。

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
                                <div data-report-view="{&quot;mod&quot;:&quot;popu_387&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/xie_qi_chao/article/details/105810121&quot;,&quot;extend1&quot;:&quot;pc&quot;,&quot;ab&quot;:&quot;new&quot;}"></div>
                    
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
            </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit "><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}" target="_blank">解启超</a></span>
                    <!-- 等级,level -->
                                            <img class="identity-icon" src="https://csdnimg.cn/identity/blog5.png">                                            </div>
                <div class="text"><span>原创文章 409</span><span>获赞 75</span><span>访问量 6万+</span></div>
            </div>
                            <div class="right-message">
                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}">已关注</a>
                                                            <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm bt-button personal-letter">私信
                    </a>
                                </div>
                        </div>
                    
    </div>
</article>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹汇川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值