仅当主机名包含字符串时才运行Ansible任务 [英] Run an Ansible task only when the hostname contains a string

查看:98
本文介绍了仅当主机名包含字符串时才运行Ansible任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扮演的多个任务如下.我不想创建另一个yml文件来处理此任务.我已经有一个包含Web服务器的附件,但是我们的一些Perl服务器需要安装一些Web软件包.

I have multiple tasks in a role as follows. I do not want to create another yml file to handle this task. I already have an include for the web servers, but a couple of our Perl servers require some web packages to be installed.

- 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

推荐答案

下面应该解决这个问题:

Below should do the trick:

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

请注意,在运行Playbook时,您会跳过几个主机.

Note you'll have a couple of skipped hosts during playbook run.

inventory_hostname是Ansible的魔术"变量之一:

inventory_hostname is one of Ansible's "magic" variables:

此外,inventory_hostname是主机名,名称为 配置在Ansible的广告资源托管文件中.这可能对 当您不想依赖发现的主机名时 ansible_hostname或其他神秘原因.如果你有很长的时间 FQDN,inventory_hostname_short也包含直到第一个部分 期间,没有域的其余部分.

Additionally, inventory_hostname is the name of the hostname as configured in Ansible’s inventory host file. This can be useful for when you don’t want to rely on the discovered hostname ansible_hostname or for other mysterious reasons. If you have a long FQDN, inventory_hostname_short also contains the part up to the first period, without the rest of the domain.

来源: Ansible Docs-魔术变量以及如何访问有关其他主机的信息

这篇关于仅当主机名包含字符串时才运行Ansible任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆