我在哪里可以得到Ansible预定义variables的列表?

我看到Ansible提供了一些我们可以在剧本和模板文件中使用的预定义variables。 例如,主机IP地址是ansible_eth0.ipv4.address。 谷歌和search文档我没有find所有可用variables的列表。 有人会列出他们吗?

哎呀! 从常见问题解答 :

我如何看到所有ansible_variables的列表? Ansible默认收集关于pipe理机器的“事实”,这些事实可以在Playbook和模板中访问。 要查看有关机器的所有可用事实的列表,可以运行“设置”模块作为临时操作:

ansible -m setup hostname 

这将打印一个字典的所有可用的特定主机的事实。

这是我的stream浪虚拟机scdev的输出:

 scdev | success >> { "ansible_facts": { "ansible_all_ipv4_addresses": [ "10.0.2.15", "192.168.10.10" ], "ansible_all_ipv6_addresses": [ "fe80::a00:27ff:fe12:9698", "fe80::a00:27ff:fe74:1330" ], "ansible_architecture": "i386", "ansible_bios_date": "12/01/2006", "ansible_bios_version": "VirtualBox", "ansible_cmdline": { "BOOT_IMAGE": "/vmlinuz-3.2.0-23-generic-pae", "quiet": true, "ro": true, "root": "/dev/mapper/precise32-root" }, "ansible_date_time": { "date": "2013-09-17", "day": "17", "epoch": "1379378304", "hour": "00", "iso8601": "2013-09-17T00:38:24Z", "iso8601_micro": "2013-09-17T00:38:24.425092Z", "minute": "38", "month": "09", "second": "24", "time": "00:38:24", "tz": "UTC", "year": "2013" }, "ansible_default_ipv4": { "address": "10.0.2.15", "alias": "eth0", "gateway": "10.0.2.2", "interface": "eth0", "macaddress": "08:00:27:12:96:98", "mtu": 1500, "netmask": "255.255.255.0", "network": "10.0.2.0", "type": "ether" }, "ansible_default_ipv6": {}, "ansible_devices": { "sda": { "holders": [], "host": "SATA controller: Intel Corporation 82801HM/HEM (ICH8M/ICH8M-E) SATA Controller [AHCI mode] (rev 02)", "model": "VBOX HARDDISK", "partitions": { "sda1": { "sectors": "497664", "sectorsize": 512, "size": "243.00 MB", "start": "2048" }, "sda2": { "sectors": "2", "sectorsize": 512, "size": "1.00 KB", "start": "501758" }, }, "removable": "0", "rotational": "1", "scheduler_mode": "cfq", "sectors": "167772160", "sectorsize": "512", "size": "80.00 GB", "support_discard": "0", "vendor": "ATA" }, "sr0": { "holders": [], "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)", "model": "CD-ROM", "partitions": {}, "removable": "1", "rotational": "1", "scheduler_mode": "cfq", "sectors": "2097151", "sectorsize": "512", "size": "1024.00 MB", "support_discard": "0", "vendor": "VBOX" }, "sr1": { "holders": [], "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)", "model": "CD-ROM", "partitions": {}, "removable": "1", "rotational": "1", "scheduler_mode": "cfq", "sectors": "2097151", "sectorsize": "512", "size": "1024.00 MB", "support_discard": "0", "vendor": "VBOX" } }, "ansible_distribution": "Ubuntu", "ansible_distribution_release": "precise", "ansible_distribution_version": "12.04", "ansible_domain": "", "ansible_eth0": { "active": true, "device": "eth0", "ipv4": { "address": "10.0.2.15", "netmask": "255.255.255.0", "network": "10.0.2.0" }, "ipv6": [ { "address": "fe80::a00:27ff:fe12:9698", "prefix": "64", "scope": "link" } ], "macaddress": "08:00:27:12:96:98", "module": "e1000", "mtu": 1500, "type": "ether" }, "ansible_eth1": { "active": true, "device": "eth1", "ipv4": { "address": "192.168.10.10", "netmask": "255.255.255.0", "network": "192.168.10.0" }, "ipv6": [ { "address": "fe80::a00:27ff:fe74:1330", "prefix": "64", "scope": "link" } ], "macaddress": "08:00:27:74:13:30", "module": "e1000", "mtu": 1500, "type": "ether" }, "ansible_form_factor": "Other", "ansible_fqdn": "scdev", "ansible_hostname": "scdev", "ansible_interfaces": [ "lo", "eth1", "eth0" ], "ansible_kernel": "3.2.0-23-generic-pae", "ansible_lo": { "active": true, "device": "lo", "ipv4": { "address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0" }, "ipv6": [ { "address": "::1", "prefix": "128", "scope": "host" } ], "mtu": 16436, "type": "loopback" }, "ansible_lsb": { "codename": "precise", "description": "Ubuntu 12.04 LTS", "id": "Ubuntu", "major_release": "12", "release": "12.04" }, "ansible_machine": "i686", "ansible_memfree_mb": 23, "ansible_memtotal_mb": 369, "ansible_mounts": [ { "device": "/dev/mapper/precise32-root", "fstype": "ext4", "mount": "/", "options": "rw,errors=remount-ro", "size_available": 77685088256, "size_total": 84696281088 }, { "device": "/dev/sda1", "fstype": "ext2", "mount": "/boot", "options": "rw", "size_available": 201044992, "size_total": 238787584 }, { "device": "/vagrant", "fstype": "vboxsf", "mount": "/vagrant", "options": "uid=1000,gid=1000,rw", "size_available": 42013151232, "size_total": 484145360896 } ], "ansible_os_family": "Debian", "ansible_pkg_mgr": "apt", "ansible_processor": [ "Pentium(R) Dual-Core CPU E5300 @ 2.60GHz" ], "ansible_processor_cores": "NA", "ansible_processor_count": 1, "ansible_product_name": "VirtualBox", "ansible_product_serial": "NA", "ansible_product_uuid": "NA", "ansible_product_version": "1.2", "ansible_python_version": "2.7.3", "ansible_selinux": false, "ansible_swapfree_mb": 766, "ansible_swaptotal_mb": 767, "ansible_system": "Linux", "ansible_system_vendor": "innotek GmbH", "ansible_user_id": "neves", "ansible_userspace_architecture": "i386", "ansible_userspace_bits": "32", "ansible_virtualization_role": "guest", "ansible_virtualization_type": "virtualbox" }, "changed": false } 

现在的文档现在有一个完整的章节列出所有的variables和事实

 ansible -m setup hostname 

只获取由设置模块收集的事实。

Gilles Cornu发布了一个模板技巧来列出特定主机的所有variables。

模板(以后称为dump_variables ):

 HOSTVARS (ANSIBLE GATHERED, group_vars, host_vars) : {{ hostvars[inventory_hostname] | to_yaml }} PLAYBOOK VARS: {{ vars | to_yaml }} 

Playbook使用它:

 - hosts: all tasks: - template: src: templates/dump_variables dest: /tmp/ansible_variables - fetch: src: /tmp/ansible_variables dest: "{{inventory_hostname}}_ansible_variables" 

之后,您将在每个主机上转储所有variables,并在您的tmp文件夹中的本地工作站上创build每个文本转储文件的副本。 如果您不需要本地副本,则可以删除获取语句。

这包括收集的事实,主机variables和组variables。 因此,您可以看到诸如group_namesinventory_hostnameansible_ssh_host有用的默认variables。

Ansible中有三个variables来源:

  1. 从事实收集的variables。 您可以通过运行命令来获取它们: ansible -m setup hostname

  2. 内置(预定义)Ansiblevariables(AKA“魔术”variables)。 他们被logging在Ansible文档中: http ://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts
    以下是从Ansible 1.9文档中提取的列表:

    • GROUP_NAMES
    • inventory_hostname
    • ansible_hostname
    • inventory_hostname_short
    • play_hosts
    • delegate_to
    • inventory_dir
    • inventory_file
  3. 通过命令行传递给variables 但显然你知道他们是什么

我使用这个简单的剧本:

 --- # vars.yml # # Shows the value of all variables/facts. # # Example: # # ansible-playbook vars.yml -e 'hosts=localhost' # - hosts: localhost tasks: - fail: "You must specify a value for `hosts` variable - eg: ansible-playbook vars.yml -e 'hosts=localhost'" when: hosts is not defined - hosts: "{{ hosts }}" tasks: - debug: var=vars - debug: var=hostvars[inventory_hostname] 

请注意,有关连接configurationvariables或“行为”variables(未列在主variables中)的官方文档似乎是“ 库存”文档中的“行为清单参数列表” 。

PS sudo选项在那里没有logging(是的,它的sudoansible_sudo ,你所期望的…),可能还有一些不是,但这是我发现的最好的文档。

有很多定义为事实的variables – http://docs.ansible.com/ansible/playbooks_variables.html#information-discovered-from-systems-facts

 "ansible_all_ipv4_addresses": [ "REDACTED IP ADDRESS" ], "ansible_all_ipv6_addresses": [ "REDACTED IPV6 ADDRESS" ], "ansible_architecture": "x86_64", "ansible_bios_date": "09/20/2012", "ansible_bios_version": "6.00", "ansible_cmdline": { "BOOT_IMAGE": "/boot/vmlinuz-3.5.0-23-generic", "quiet": true, "ro": true, "root": "UUID=4195bff4-e157-4e41-8701-e93f0aec9e22", "splash": true }, "ansible_date_time": { "date": "2013-10-02", "day": "02", "epoch": "1380756810", "hour": "19", "iso8601": "2013-10-02T23:33:30Z", "iso8601_micro": "2013-10-02T23:33:30.036070Z", "minute": "33", "month": "10", "second": "30", "time": "19:33:30", "tz": "EDT", "year": "2013" }, "ansible_default_ipv4": { "address": "REDACTED", "alias": "eth0", "gateway": "REDACTED", "interface": "eth0", "macaddress": "REDACTED", "mtu": 1500, "netmask": "255.255.255.0", "network": "REDACTED", "type": "ether" }, "ansible_default_ipv6": {}, "ansible_devices": { "fd0": { "holders": [], "host": "", "model": null, "partitions": {}, "removable": "1", "rotational": "1", "scheduler_mode": "deadline", "sectors": "0", "sectorsize": "512", "size": "0.00 Bytes", "support_discard": "0", "vendor": null }, "sda": { "holders": [], "host": "SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)", "model": "VMware Virtual S", "partitions": { "sda1": { "sectors": "39843840", "sectorsize": 512, "size": "19.00 GB", "start": "2048" }, "sda2": { "sectors": "2", "sectorsize": 512, "size": "1.00 KB", "start": "39847934" }, "sda5": { "sectors": "2093056", "sectorsize": 512, "size": "1022.00 MB", "start": "39847936" } }, "removable": "0", "rotational": "1", "scheduler_mode": "deadline", "sectors": "41943040", "sectorsize": "512", "size": "20.00 GB", "support_discard": "0", "vendor": "VMware," }, "sr0": { "holders": [], "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)", "model": "VMware IDE CDR10", "partitions": {}, "removable": "1", "rotational": "1", "scheduler_mode": "deadline", "sectors": "2097151", "sectorsize": "512", "size": "1024.00 MB", "support_discard": "0", "vendor": "NECVMWar" } }, "ansible_distribution": "Ubuntu", "ansible_distribution_release": "precise", "ansible_distribution_version": "12.04", "ansible_domain": "", "ansible_env": { "COLORTERM": "gnome-terminal", "DISPLAY": ":0", "HOME": "/home/mdehaan", "LANG": "C", "LESSCLOSE": "/usr/bin/lesspipe %s %s", "LESSOPEN": "| /usr/bin/lesspipe %s", "LOGNAME": "root", "LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:", "MAIL": "/var/mail/root", "OLDPWD": "/root/ansible/docsite", "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "PWD": "/root/ansible", "SHELL": "/bin/bash", "SHLVL": "1", "SUDO_COMMAND": "/bin/bash", "SUDO_GID": "1000", "SUDO_UID": "1000", "SUDO_USER": "mdehaan", "TERM": "xterm", "USER": "root", "USERNAME": "root", "XAUTHORITY": "/home/mdehaan/.Xauthority", "_": "/usr/local/bin/ansible" }, "ansible_eth0": { "active": true, "device": "eth0", "ipv4": { "address": "REDACTED", "netmask": "255.255.255.0", "network": "REDACTED" }, "ipv6": [ { "address": "REDACTED", "prefix": "64", "scope": "link" } ], "macaddress": "REDACTED", "module": "e1000", "mtu": 1500, "type": "ether" }, "ansible_form_factor": "Other", "ansible_fqdn": "ubuntu2.example.com", "ansible_hostname": "ubuntu2", "ansible_interfaces": [ "lo", "eth0" ], "ansible_kernel": "3.5.0-23-generic", "ansible_lo": { "active": true, "device": "lo", "ipv4": { "address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0" }, "ipv6": [ { "address": "::1", "prefix": "128", "scope": "host" } ], "mtu": 16436, "type": "loopback" }, "ansible_lsb": { "codename": "precise", "description": "Ubuntu 12.04.2 LTS", "id": "Ubuntu", "major_release": "12", "release": "12.04" }, "ansible_machine": "x86_64", "ansible_memfree_mb": 74, "ansible_memtotal_mb": 991, "ansible_mounts": [ { "device": "/dev/sda1", "fstype": "ext4", "mount": "/", "options": "rw,errors=remount-ro", "size_available": 15032406016, "size_total": 20079898624 } ], "ansible_nodename": "ubuntu2.example.com", "ansible_os_family": "Debian", "ansible_pkg_mgr": "apt", "ansible_processor": [ "Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz" ], "ansible_processor_cores": 1, "ansible_processor_count": 1, "ansible_processor_threads_per_core": 1, "ansible_processor_vcpus": 1, "ansible_product_name": "VMware Virtual Platform", "ansible_product_serial": "REDACTED", "ansible_product_uuid": "REDACTED", "ansible_product_version": "None", "ansible_python_version": "2.7.3", "ansible_selinux": false, "ansible_ssh_host_key_dsa_public": "REDACTED KEY VALUE" "ansible_ssh_host_key_ecdsa_public": "REDACTED KEY VALUE" "ansible_ssh_host_key_rsa_public": "REDACTED KEY VALUE" "ansible_swapfree_mb": 665, "ansible_swaptotal_mb": 1021, "ansible_system": "Linux", "ansible_system_vendor": "VMware, Inc.", "ansible_user_id": "root", "ansible_userspace_architecture": "x86_64", "ansible_userspace_bits": "64", "ansible_virtualization_role": "guest", "ansible_virtualization_type": "VMware" 

debug模块可以用来分析variables。 小心运行以下命令。 在我们的设置中,它产生了444709行16MB:

 ansible -m debug -a 'var=hostvars' localhost 

我不确定,但可能有必要启用事实caching。

如果只需要一台主机,则使用主机名作为hostvars散列的关键字:

 ansible -m debug -a 'var=hostvars.localhost' localhost 

这个命令也会显示组和主机variables。

https://github.com/f500/ansible-dumpall
仅供参考:这个github项目展示了如何在所有主机上列出90%的variables。 我觉得它比单个主机命令更有用。 自述文件包含有关构build简单库存报告的说明。 在剧本结尾处运行以查看所有事实更加有价值。 还要debugging任务行为使用registry:

结果是缺less一些项目: – 包括YAML文件variables – extra-vars – 这里描述的一些Ansible内部variables: Ansible Behaviors Params

某些variables在每个主机上都不可用,例如ansible_domaindomain 。 如果情况需要debugging,我login到服务器并发出:

 user@server:~$ ansible -m setup localhost | grep domain [WARNING]: provided hosts list is empty, only localhost is available "ansible_domain": "prd.example.com", 

我知道这个问题已经得到了回答,但是我觉得还有一整套预先定义的variables不包含在ansible_ *事实中。 这个文档页面涵盖了指令 (修改Ansible的行为的variables),当我遇到这个页面的时候我正在寻找这个指令

这包括一些常见的和一些特定的用例指令:

  • 成为:控制权限升级(sudo)
  • delegate_to:在另一个主机上运行任务(如在本地主机上运行)
  • 串行:允许您在移动到下一组之前在特定数量/百分比的主机上运行播放