仅当主机名包含字符串时才运行 Ansible 任务

2023-12-30

我的角色有多项任务,如下所示。我不想创建另一个yml文件来处理此任务。我已经包含了 Web 服务器,但是我们的几个 Perl 服务器需要安装一些 Web 软件包。

- name: Install Perl Modules
  command: <command>
  with_dict: perl_modules

- name: Install PHP Modules
  command: <command>
  with_dict: php_modules
  when: <Install php modules only if hostname contains the word "batch">

主机清单文件

[webs]
web01
web02
web03

[perl]
perl01
perl02
perl03
perl-batch01
perl-batch02

下面应该可以解决这个问题:

- name: Install PHP Modules
  command: <command>
  with_dict: php_modules
  when: "'batch' in inventory_hostname"

请注意,在剧本运行期间您将跳过几个主机。

inventory_hostname是 Ansible 的“神奇”变量之一:

此外,inventory_hostname 是主机名,如下所示 在 Ansible 的清单主机文件中配置。这对于 当您不想依赖发现的主机名时 ansible_hostname 或出于其他神秘原因。如果你有一个长 FQDN、inventory_hostname_short 还包含直到第一个的部分 期间,没有域的其余部分。

Source: Ansible 文档 - 魔法变量 https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#magic-variables

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

仅当主机名包含字符串时才运行 Ansible 任务 的相关文章

随机推荐