2019独角兽企业重金招聘Python工程师标准>>>

<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>

转载于:https://my.oschina.net/songmingming/blog/269199

ansible(二)—— inventory相关推荐

  1. python3连接mysql获取ansible动态inventory脚本

    Ansible Inventory 是包含静态 Inventory 和动态 Inventory 两部分的,静态 Inventory 指的是在文件中指定的主机和组,动态 Inventory 指通过外部脚 ...

  2. Ansible 二(新手上路)

    Ansible 二(新手上路) 1.远程连接概述 远程控制: ansible1.2版本及以前的版本,默认使用python写的paramiko模块进行控制linux主机. ansible1.3版本以后, ...

  3. Ansible之Inventory定义

    一.Inventory介绍   在Ansible中描述主机的默认方法是将它们列在一个文本文件中,这个文本文件叫作inventory文件.Ansible Inventory 是包含静态 Inventor ...

  4. Ansible的Inventory管理

    Ansible将可管理的服务器集合成为Inventory,Inventory的管理便是服务器的管理. hosts文件的位置: /etc/ansible/hosts 在命令行通过-i参数指定 通过/et ...

  5. python3连接mysql获取ansible动态inventory

    脚本地址:https://github.com/AlbertCQY/scripts/tree/master/ansible 脚本用法:README.txt 1.脚本用法 bestpay用户 cd /t ...

  6. Ansible动态Inventory格式

    Ansible可从服务器接口获取动态Inventroy配置 动态Inventory格式如下: {"group1": {"hosts": ["host1 ...

  7. ansible动态inventory里的针对AWS中国的设置

    如果你想使用ansible来动态管理AWS里的机器和设施,不幸的你用的是国内的AWS服务,你会发现一开始怎么用例子里的ec2.py都会报错说 AWS_ACCESS_KEY_ID 和 AWS_SECRE ...

  8. ansible自动化运维(二)——环境部署及常用模块的使用

    实验环境 主机 ip server1(主控端) 172.25.6.1 server2(节点) 172.25.6.2 server3(节点) 172.25.6.3 一.环境部署 ansible的配置文件 ...

  9. Ansible Inventory指北进阶

    Ansible的inventory文件定义了它要操作的一些主机,它可以通过inventory对这些主机进行操控.默认的inventory文件是/etc/ansible/hosts .也可通过-i选项, ...

  10. Ansible(二)——基本环境的部署以及常用模块的使用

    1.搭建实验环境 主机名 IP 服务 server4 172.25.35.4 主控节点 server5 172.25.35.5 被控节点 server6 172.25.35.6 被控节点 由于软件包具 ...

最新文章

  1. “undefined reference to JNI_GetCreatedJavaVM”和“File format not recognized”错误原因分析...
  2. jsp servlet table 集合list 数据 绑定
  3. 百度 UNIT 技术负责人揭秘:如何让你的对话系统更智能
  4. VS2010 + OpenGL 及 第一个实例
  5. OpenCV—图像椒盐噪声生成器
  6. 美图HTTPS优化探索与实践
  7. rabbitnq 源码安装_linux下源码安装rabbitMq
  8. Unity官方教程Ruby大冒险的自学笔记
  9. linux忘记管理员密码,如何登陆?
  10. 相亲之战,以少胜多乎?
  11. URL编码(urlencoding)中关于空格的问题
  12. 图解设计模式,看完秒懂!!!
  13. 什么叫定向广告?定向传播有哪些好处
  14. 同一个tomcat部署多个项目
  15. 数据分析师之路-数据埋点
  16. 未来十年,流程管理发展的趋势和挑战
  17. 本命年第三季度阶段反思
  18. R语言导入数据文件 (Excel文件、csv文件导入R)
  19. 从黑格尔的正反合理论看人生的三个阶段
  20. 第五届蓝桥杯—— 基础练习:特殊的数字

热门文章

  1. 美国6岁病童圆梦做一天海豹突击队员
  2. 李冰冰:英文是这样炼成的
  3. 戴尔R730服务器,安装好系统后,多出一个OEMDRV的可移动存储设备。
  4. 北京科技大学 王志良老师物联网工作小组视频
  5. 伽罗华域GF,GF(256)来源
  6. 刘萌150206318
  7. 游戏策划设计时所要涉及的基本因数(转)
  8. PerformancePoint Server 词汇表[转自MSDN]
  9. Unity Camera根据鼠标或触摸移动
  10. 完全用Linux工作,摈弃Windows【转】