others - 在动态库存中,ansible如何从标签获取主机

104 5

我需要针对2台主机运行脚本。我了解如何使用yaml或INI格式的静态清单来执行此操作,但是尝试时使用动态清单(特别是VMWare动态清单-vmware_inventory.py)会迷路。运行动态清单的结果如下所示:


{


"_meta": {


"hostvars": {


"foo_420be125-0a38-6dcd-247c-1d1839717804": {


"ansible_connection":"ssh",


"ansible_user":"root",


"config.cpuHotAddEnabled": false,


"config.cpuHotRemoveEnabled": false,


"config.hardware.numCPU": 4,


"config.instanceUuid":"500b86dc-b51e-25fb-165d-e51c62ecd725",


"config.name":"foo",


"config.template": false,


"guest.guestId": null,


"guest.guestState":"notRunning",


"guest.hostName":"foo.bar.com",


"guest.ipAddress":"1.2.3.4",


"name":"foo",


"runtime.maxMemoryUsage": null,


"stage":"dev"


 },


"bar_fffe-6f29-3e32-0ce9a80d0ad3": {


"ansible_connection":"ssh",


 ...


 }


 },


"activedirectory-devops": {


"hosts": [


"foo_420be125-0a38-6dcd-247c-1d1839717804",


"bar_fffe-6f29-3e32-0ce9a80d0ad3"


 ]


 },


"all": {


"children": [


"activedirectory-devops",


"centos64Guest",


"centos7_64Guest",


"com.vmware.vr.HasVrDisks",


"other3xLinux64Guest",


"otherGuest",


 ...




注:activedirectory-devops是VMWare vCenter中的"标签",它是作为ansible"主机组"返回的。

我怎样才能针对activedirectory-devops中的主机运行playbook? 毕竟,"foo_420be125-0a38-6dcd-247c-1d1839717804"不能通过DNS来解析。


ansible-playbook -i vmware.yml site.yml



时间: 原作者:

69 4

你可以尝试在动态广告资源.ini文件中使用“ skip_keys ”删除不需要的属性。(在ini文件中取消注释skip_keys,并指定不需要的属性),

对于使用动态库存的playbook,请执行以下操作:


ANSIBLE_HOSTS variable to always use the VMWare inventory:


export ANSIBLE_HOSTS="/home/blabla/vmware-ansible/query.py"



Or using playbook:


ansible-playbook example.yml -i inventory



其中包含库存目录。


inventory/


 01-openstack.yml # configure inventory plugin to get hosts from Openstack cloud


 02-dynamic-inventory.py # add additional hosts with dynamic inventory script


 03-static-inventory # add static hosts


 group_vars/


 all.yml # assign variables to all hosts



原作者:
125 3


ansible activedirectory-devops -m ping --connection=local -i vmware.yml



结果:


foo_420be125-0a38-6dcd-247c-1d1839717804 | SUCCESS => {


"changed": false,


"ping":"pong"


}


bar_fffe-6f29-3e32-0ce9a80d0ad3 | SUCCESS => {


"changed": false,


"ping":"pong"


}



原作者:
...