ansible(二)—— inventory
ansible(二)—— inventory
异类深呼吸 发表于4年前
ansible(二)—— inventory
  • 发表于 4年前
  • 阅读 403
  • 收藏 2
  • 点赞 0
  • 评论 0

【腾讯云】买域名送云解析+SSL证书+建站!>>>   

摘要: 粗略的看了一些ansible的文档,然后觉得官方的例子也挺好,就没有必要自己折腾一套了,直接搬过来

<h2 style="font-size: 24px; -webkit-print-color-adjust: exact; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding: 0px; -webkit-font-smoothing: antialiased; cursor: text; position: relative; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, sans-serif; margin-top: 0px !important;">ansible(二) -- inventory</h2> <blockquote style="border-left-color: #dddddd; margin: 15px 0px; padding: 0px 15px; -webkit-print-color-adjust: exact; color: #777777; font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 22px;"> <p style="-webkit-print-color-adjust: exact; margin: 0px;">例子直接从官网上摘抄。地址:http://docs.ansible.com/intro_inventory.html#id10</p> </blockquote> <h2 style="font-size: 24px; -webkit-print-color-adjust: exact; margin: 20px 0px 10px; padding: 0px; -webkit-font-smoothing: antialiased; cursor: text; position: relative; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, sans-serif;">1、Ad-hoc commands</h2> <blockquote style="border-left-color: #dddddd; margin: 15px 0px; padding: 0px 15px; -webkit-print-color-adjust: exact; color: #777777; font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 22px;"> <p style="-webkit-print-color-adjust: exact; margin: 0px 0px 15px;">对于一些快速执行的而又无需写入保存的任务,ansible提供了这种特殊命令行的操作;这里官方地址:http://docs.ansible.com/intro_adhoc.html</p> <p style="-webkit-print-color-adjust: exact; margin: 15px 0px 0px;">官网给的解释很有趣,既然playbook如此强大,为何还要提供这种特殊的命令行呢,所以举例说明,假如你只想圣诞节间关机的情况下,只执行一条命令的效率很显然比写playbook文件的效果要更好</p> </blockquote> <h4 style="font-size: 16px; -webkit-print-color-adjust: exact; margin: 20px 0px 10px; padding: 0px; -webkit-font-smoothing: antialiased; cursor: text; position: relative; font-family: Helvetica, arial, sans-serif;">准备工作</h4> <blockquote style="border-left-color: #dddddd; margin: 15px 0px; padding: 0px 15px; -webkit-print-color-adjust: exact; color: #777777; font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 22px;"> <p style="-webkit-print-color-adjust: exact; margin: 0px;">在做之前,当然就是实现ssh无秘钥登录,即拷贝公钥到要管理的目标主机上</p> </blockquote> <p style="-webkit-print-color-adjust: exact; margin: 15px 0px; font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 22px;">基本语法:</p> <pre style="margin: 15px 0px; font-size: 13px; -webkit-print-color-adjust: exact; background-color: #f8f8f8; border: 1px solid #cccccc; line-height: 19px; overflow: auto; padding: 6px 10px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><code style="-webkit-print-color-adjust: exact; margin: 0px; padding: 0px; border: none; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;">ansible &lt;pattern_goes_here&gt; -m &lt;module_name&gt; -a &lt;arguments&gt; -i:指定 inventory 文件,使用当前目录下的 hosts all:针对 hosts 定义的所有主机执行,这里也可以指定组名或模式 -m:指定所用的模块,我们使用 Ansible 内置的 ping 模块来检查能否正常管理远端机器 -u:指定远端机器的用户 -a:参数部分 </code></pre> <h3 style="font-size: 18px; -webkit-print-color-adjust: exact; margin: 20px 0px 10px; padding: 0px; -webkit-font-smoothing: antialiased; cursor: text; position: relative; font-family: Helvetica, arial, sans-serif;">模式</h3> <blockquote style="border-left-color: #dddddd; margin: 15px 0px; padding: 0px 15px; -webkit-print-color-adjust: exact; color: #777777; font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 22px;"> <p style="-webkit-print-color-adjust: exact; margin: 0px;">了解下命令行中的特殊模式</p> </blockquote> <pre style="margin: 15px 0px; font-size: 13px; -webkit-print-color-adjust: exact; background-color: #f8f8f8; border: 1px solid #cccccc; line-height: 19px; overflow: auto; padding: 6px 10px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><code style="-webkit-print-color-adjust: exact; margin: 0px; padding: 0px; border: none; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;">all 等价于inventory中的所有hosts <br />one.example.com \\一台主机 <br />one.example.com:two.example.com \\两台主机 <br />192.168.1.50 \\也可以已IP地址指定 <br />192.168.1.* \\通配符 </code></pre> <pre style="margin: 15px 0px; font-size: 13px; -webkit-print-color-adjust: exact; background-color: #f8f8f8; border: 1px solid #cccccc; line-height: 19px; overflow: auto; padding: 6px 10px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><code style="-webkit-print-color-adjust: exact; margin: 0px; padding: 0px; border: none; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;">#组的指定 <br />webservers <br />webservers:dbservers <br /><br />#指定一个组,但是却不在另外一个组中 <br />webservers:!phoenix <br /><br />#指定交叉,加入webservers同时也必须加入staging中 <br />webservers:&amp;staging <br /><br />#上面也可以综合起来写 <br />webservers:dbservers:&amp;staging:!phoenix <br /><br />#传递变量,必须加上参数-e,但是一般不会用 <br />webservers:!{{excluded}}:&amp;{{required}} </code></pre> <pre style="margin: 15px 0px; font-size: 13px; -webkit-print-color-adjust: exact; background-color: #f8f8f8; border: 1px solid #cccccc; line-height: 19px; overflow: auto; padding: 6px 10px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><code style="-webkit-print-color-adjust: exact; margin: 0px; padding: 0px; border: none; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;">#模糊匹配 <br />*.example.com *.com <br /><br />#混合写法 <br />one*.com:dbservers <br /><br />#正则写法 <br />~(web|db).*\.example\.com</code></pre> <h2 style="font-size: 24px; -webkit-print-color-adjust: exact; margin: 20px 0px 10px; padding: 0px; -webkit-font-smoothing: antialiased; cursor: text; position: relative; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, sans-serif;">2、inventory</h2> <blockquote style="border-left-color: #dddddd; margin: 15px 0px; padding: 0px 15px; -webkit-print-color-adjust: exact; color: #777777; font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 22px;"> <p style="-webkit-print-color-adjust: exact; margin: 0px;">inventory 文件用来定义你要管理的主机。其默认位置在 /etc/ansible/hosts ,如果不保存在默认位置,也可通过-i选项指定。</p> </blockquote> <p style="-webkit-print-color-adjust: exact; margin: 15px 0px; font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 22px;">参照官网,这里inventory可以灵活多变的写成多样的形式来满足不同的需求</p> <pre style="margin: 15px 0px; font-size: 13px; -webkit-print-color-adjust: exact; background-color: #f8f8f8; border: 1px solid #cccccc; line-height: 19px; overflow: auto; padding: 6px 10px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><code style="-webkit-print-color-adjust: exact; margin: 0px; padding: 0px; border: none; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;">mail.example.com \\未分组的机器需要在hosts的顶部保留 <br />[webservers] <br />foo.example.com <br />bar.example.com <br /><br />[dbservers] <br />one.example.com <br />two.example.com <br />three.example.com <br /><br />[test:children] \\支持嵌套 <br />webservers <br />dbservers </code></pre> <pre style="margin: 15px 0px; font-size: 13px; -webkit-print-color-adjust: exact; background-color: #f8f8f8; border: 1px solid #cccccc; line-height: 19px; overflow: auto; padding: 6px 10px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><code style="-webkit-print-color-adjust: exact; margin: 0px; padding: 0px; border: none; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;">#支持基于非标准的SSH端口 <br />badwolf.example.com:5309 <br /><br />#支持跳转 <br />jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50 </code></pre> <pre style="margin: 15px 0px; font-size: 13px; -webkit-print-color-adjust: exact; background-color: #f8f8f8; border: 1px solid #cccccc; line-height: 19px; overflow: auto; padding: 6px 10px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><code style="-webkit-print-color-adjust: exact; margin: 0px; padding: 0px; border: none; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;">#支持通配 <br />[webservers]<br />www[01:50].example.com \\表示www01-50.example.com的50个目标 <br /><br />[databases] <br />db-[a:f].example.com \\同理db-a - db-f.example.com的目标 </code></pre> <pre style="margin: 15px 0px; font-size: 13px; -webkit-print-color-adjust: exact; background-color: #f8f8f8; border: 1px solid #cccccc; line-height: 19px; overflow: auto; padding: 6px 10px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><code style="-webkit-print-color-adjust: exact; margin: 0px; padding: 0px; border: none; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;">#基于不同的主机不同的连接类型 <br />[targets] <br />localhost ansible_connection=local <br />other1.example.com ansible_connection=ssh ansible_ssh_user=mpdehaan <br />other2.example.com ansible_connection=ssh ansible_ssh_user=mdehaan </code></pre> <h3 style="font-size: 18px; -webkit-print-color-adjust: exact; margin: 20px 0px 10px; padding: 0px; -webkit-font-smoothing: antialiased; cursor: text; position: relative; font-family: Helvetica, arial, sans-serif;">Host变量</h3> <pre style="margin: 15px 0px; font-size: 13px; -webkit-print-color-adjust: exact; background-color: #f8f8f8; border: 1px solid #cccccc; line-height: 19px; overflow: auto; padding: 6px 10px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><code style="-webkit-print-color-adjust: exact; margin: 0px; padding: 0px; border: none; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;">#主机变量很容易被稍后的playbook引用 <br />[atlanta] <br />host1 http_port=80 maxRequestsPerChild=808 <br />host2 http_port=303 maxRequestsPerChild=909 </code></pre> <h3 style="font-size: 18px; -webkit-print-color-adjust: exact; margin: 20px 0px 10px; padding: 0px; -webkit-font-smoothing: antialiased; cursor: text; position: relative; font-family: Helvetica, arial, sans-serif;">Group变量</h3> <pre style="margin: 15px 0px; font-size: 13px; -webkit-print-color-adjust: exact; background-color: #f8f8f8; border: 1px solid #cccccc; line-height: 19px; overflow: auto; padding: 6px 10px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><code style="-webkit-print-color-adjust: exact; margin: 0px; padding: 0px; border: none; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;">#变量可以直接在组中全部应用 <br />[atlanta] <br />host1 <br />host2 <br /><br />[atlanta:vars] <br />ntp_server=ntp.atlanta.example.com \\直接引用上面的host1和host2 <br />proxy=proxy.atlanta.example.com <br /><br />#同样支持嵌套 <br />[atlanta] <br />host1 <br />host2 <br /><br />[raleigh] <br />host2 <br />host3 <br /><br />[southeast:children] <br />atlanta <br />raleigh <br /><br />[southeast:vars] <br />some_server=foo.southeast.example.com <br />halon_system_timeout=30 <br />self_destruct_countdown=60 <br />escape_pods=2 <br /><br />[usa:children] <br />southeast <br />northeast <br />southwest <br />northwest </code></pre> <h2 style="font-size: 24px; -webkit-print-color-adjust: exact; margin: 20px 0px 10px; padding: 0px; -webkit-font-smoothing: antialiased; cursor: text; position: relative; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, sans-serif;">3、Dynamic Inventory</h2> <p style="-webkit-print-color-adjust: exact; margin: 15px 0px; font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 22px;">在不同的系统平台上,ansible也提供了基于文本的形式来描述inventory,这部分还没太看明白,日后看明白,补充上这部分的内容</p> <p style="-webkit-print-color-adjust: exact; margin-top: 15px; margin-right: 0px; margin-left: 0px; font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 22px; margin-bottom: 0px !important;">常用指令在上一篇已经提到,这里就不多做更多演示,而要完成日常任务都是依赖于各个模块提供的功能,我基本上日常中只用到了ansible的这些功能,关于ansible的核心,playbook,也会在后面的文章中去学习,和各位分享学习过程</p>

标签: ansible inventory
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 5
博文 46
码字总数 42368
×
异类深呼吸
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: