ansible实战应用系列教程8:magic variables

Magic variables

您可以访问有关Ansible操作的信息,包括正在使用的python版本,invnetory中的主机和组,以及play和role的目录,使用“magic”变量。和连接变量一样,magic变量也是特殊变量。magic变量名是保留的-不要用这些名称设置变量。变量environment 也被保留。

最常用的魔法变量是hostvars、groups、group_names和inventory_hostname。使用hostvars,您可以在剧本中的任何一点访问为剧本中的任何主机定义的变量。你也可以使用hostvars变量访问Ansible事实,但只有在你收集(或缓存)事实之后。注意,在play对象中定义的变量不是为特定主机定义的,因此没有映射到hostvars。

如果你想使用来自另一个节点的’ fact '的值来配置你的数据库服务器,或者分配给另一个节点的库存变量的值,你可以在模板或操作行中使用hostvars:

{{ hostvars['test.example.com']['ansible_facts']['distribution'] }}

对于组(目录中所有组(和主机)的列表),您可以枚举组中的所有主机。例如:

{% for host in groups['app_servers'] %}
   # something that applies to all app servers.
{% endfor %}

可以同时使用组和hostvars来查找组中的所有IP地址。

{% for host in groups['app_servers'] %}
   {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}
{% endfor %}

您可以使用这种方法将前端代理服务器指向应用程序服务器组中的所有主机,在服务器之间设置正确的防火墙规则,等等。在填写模板的任务之前,必须为这些主机缓存事实或收集事实。

使用group_names,当前主机所在的所有组的列表(数组),您可以根据主机的组成员关系(或角色)创建不同的模板文件:

{% if 'webserver' in group_names %}
   # some part of a configuration file that only applies to webservers
{% endif %}

当禁用事实收集时,可以使用magic的变量inventory_hostname(在目录中配置的主机名)作为ansible_hostname的替代。如果您有一个较长的FQDN,您可以使用inventory_hostname_short,它包含到第一个句点的部分,而不包含域的其余部分。

其他有用的魔法变量是指当前的剧本或剧本。这些变量对于填写带有多个主机名的模板或将列表注入到负载均衡器的规则中很有用。

ansible_play_hosts是当前播放中仍然活跃的所有主机的列表。

ansible_play_batch是当前戏剧“批处理”范围内的主机名列表。

batch 大小是由serial定义的,当不设置时,它相当于整个play(使其与ansible_play_hosts相同)。

ansible_playbook_python是python可执行文件的路径,用于调用Ansible命令行工具。

inventory_dir是存放Ansible库存主机文件的目录的路径名。

inventory_file是指向Ansible的库存主机的路径名和文件名。

role_path包含当前角色的路径名,只在角色内部生效。

ansible_check_mode是一个布尔值,如果你使用–check运行Ansible,则设置为True。

ansible version:New in version 1.8.

为了适应剧本行为的不同版本的Ansible,你可以使用变量ansible_version,它有以下结构:

# ansible localhost -m debug -a 'var=ansible_version'
{
    "ansible_version": {
        "full": "2.10.1",
        "major": 2,
        "minor": 10,
        "revision": 1,
        "string": "2.10.1"
    }
}

Special Variables

magic variables

这些变量不能由用户直接设置;Ansible将始终覆盖它们以反映内部状态。

  • ansible_check_mode

    Boolean that indicates if we are in check mode or not

  • ansible_config_file

    The full path of used Ansible configuration file

  • ansible_dependent_role_names

    The names of the roles currently imported into the current play as dependencies of other plays

  • ansible_diff_mode

    Boolean that indicates if we are in diff mode or not

  • ansible_forks

    Integer reflecting the number of maximum forks available to this run

  • ansible_inventory_sources

    List of sources used as inventory

  • ansible_limit

    Contents of the --limit CLI option for the current execution of Ansible

  • ansible_loop

    A dictionary/map containing extended loop information when enabled through loop_control.extended

  • ansible_loop_var

    The name of the value provided to loop_control.loop_var. Added in 2.8

  • ansible_index_var

    The name of the value provided to loop_control.index_var. Added in 2.9

  • ansible_parent_role_names

    When the current role is being executed by means of an include_role or import_role action, this variable contains a list of all parent roles, with the most recent role (in other words, the role that included/imported this role) being the first item in the list.When multiple inclusions occur, this list lists the last role (in other words, the role that included this role) as the first item in the list. It is also possible that a specific role exists more than once in this list.For example: When role A includes role B, inside role B, ansible_parent_role_names will equal to ['A']. If role B then includes role C, the list becomes ['B', 'A'].

  • ansible_parent_role_paths

    When the current role is being executed by means of an include_role or import_role action, this variable contains a list of all parent roles paths, with the most recent role (in other words, the role that included/imported this role) being the first item in the list.Please refer to ansible_parent_role_names for the order of items in this list.

  • ansible_play_batch

    List of active hosts in the current play run limited by the serial, aka ‘batch’. Failed/Unreachable hosts are not considered ‘active’.

  • ansible_play_hosts

    List of hosts in the current play run, not limited by the serial. Failed/Unreachable hosts are excluded from this list.

  • ansible_play_hosts_all

    List of all the hosts that were targeted by the play

  • ansible_play_role_names

    The names of the roles currently imported into the current play. This list does not contain the role names that areimplicitly included through dependencies.

  • ansible_playbook_python

    The path to the python interpreter being used by Ansible on the controller

  • ansible_role_names

    The names of the roles currently imported into the current play, or roles referenced as dependencies of the rolesimported into the current play.

  • ansible_role_name

    The fully qualified collection role name, in the format of namespace.collection.role_name

  • ansible_collection_name

    The name of the collection the task that is executing is a part of. In the format of namespace.collection

  • ansible_run_tags

    Contents of the --tags CLI option, which specifies which tags will be included for the current run. Note that if --tags is not passed, this variable will default to ["all"].

  • ansible_search_path

    Current search path for action plugins and lookups, in other words, where we search for relative paths when you do template: src=myfile

  • ansible_skip_tags

    Contents of the --skip-tags CLI option, which specifies which tags will be skipped for the current run.

  • ansible_verbosity

    Current verbosity setting for Ansible

  • ansible_version

    Dictionary/map that contains information about the current running version of ansible, it has the following keys: full, major, minor, revision and string.

  • group_names

    List of groups the current host is part of

  • groups

    A dictionary/map with all the groups in inventory and each group has the list of hosts that belong to it

  • hostvars

    A dictionary/map with all the hosts in inventory and variables assigned to them

  • inventory_hostname

    The inventory name for the ‘current’ host being iterated over in the play

  • inventory_hostname_short

    The short version of inventory_hostname

  • inventory_dir

    The directory of the inventory source in which the inventory_hostname was first defined

  • inventory_file

    The file name of the inventory source in which the inventory_hostname was first defined

  • omit

    Special variable that allows you to ‘omit’ an option in a task, for example - user: name=bob home={{ bobs_home|default(omit) }}

  • play_hosts

    Deprecated, the same as ansible_play_batch

  • ansible_play_name

    The name of the currently executed play. Added in 2.8. (name attribute of the play, not file name of the playbook.)

  • playbook_dir

    The path to the directory of the current playbook being executed. NOTE: This might be different than directory of the playbook passed to the ansible-playbook command line when a playbook contains a import_playbook statement.

  • role_name

    The name of the role currently being executed.

  • role_names

    Deprecated, the same as ansible_play_role_names

  • role_path

    The path to the dir of the currently running role

Facts

这些变量包含与当前主机相关的信息(inventory_hostname)。只有先收集它们才能使用。

ansible_facts

Contains any facts gathered or cached for the inventory_hostnameFacts are normally gathered by the setup module automatically in a play, but any module can return facts.

ansible_local - 自定义facts

Contains any ‘local facts’ gathered or cached for the inventory_hostname.The keys available depend on the custom facts created.See the setup module and facts.d or local facts for more details

Connection variables

Connection variables are normally used to set the specifics on how to execute actions on a target. Most of them correspond to connection plugins, but not all are specific to them; other plugins like shell, terminal and become are normally involved.Only the common ones are described as each connection/become/shell/etc plugin can define its own overrides and specific variables.See Controlling how Ansible behaves: precedence rules for how connection variables interact with configuration settings, command-line options, and playbook keywords.

  • ansible_become_user

    The user Ansible ‘becomes’ after using privilege escalation. This must be available to the ‘login user’.

  • ansible_connection

    The connection plugin actually used for the task on the target host.

  • ansible_host

    The ip/name of the target host to use instead of inventory_hostname.

  • ansible_python_interpreter

    The path to the Python executable Ansible should use on the target host.

  • ansible_user

    The user Ansible ‘logs in’ as.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

太极淘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值