我正在尝试使用Ansible Hostname模块来更新主机名。在CentOS上运行正常,但在CloudLinux操作系统上会抛出致命错误。
msg:主机名模块不能在平台Linux(Cloudlinux)上使用
CloudLinux是CentOS的替代品,因此该模块可以使用。该错误是在运行命令之前进行的预检检查,该检查内置于模块中。
问题 - 我如何告诉Ansible'CloudLinux'是该模块的有效操作系统。
显然我可以使用命令模块来更改主机名 - 但是如果可能的话我想使用正确的模块。
答案 0 :(得分:1)
在Ansible包中找到主机名模块的代码(例如hostname.py
in Ansible 2.2.1);
在Ansible项目目录(包含playbook的目录)中创建一个library
文件夹,并将上述文件复制到其中;
在此文件中找到a section where distribution-specific settings are set(例如class CentOSHostname
,class CentOSLinuxHostname
);
在下方或上方添加以下行,或靠近CentOS:
class CloudlinuxHostname(Hostname):
platform = 'Linux'
distribution = 'Cloudlinux'
strategy_class = RedHatStrategy
运行剧本。
答案 1 :(得分:0)
在较新版本的 ansible 上
# ansible --version
ansible 2.9.6
# ansible-doc -s hostname
- name: Manage hostname
hostname:
name: # (required) Name of the host
use: # Which strategy to use to update the hostname. If not set we try to autodetect, but this can be problematic, specially with containers as they can
present misleading information.
示例剧本
# cat host-setup.yml
- hosts: all
gather_facts: true
tasks:
- name: set hostname
hostname:
name: "host.example.com"
use: systemd
# ansible-playbook -v host-setup.yml
<块引用>
# ansible-doc hostname
> HOSTNAME (/usr/lib/python3/dist-packages/ansible/modules/system/hostname.py)
Set system's hostname, supports most OSs/Distributions, including those using systemd. Note, this module does *NOT* modify
`/etc/hosts'. You need to modify it yourself using other modules like template or replace. Windows, HP-UX and AIX are not
currently
supported.
* This module is maintained by The Ansible Community OPTIONS (= is
mandatory):
= name
Name of the host
- use
Which strategy to use to update the hostname.
If not set we try to autodetect, but this can be problematic, specially with containers as they can present misleading information.
(Choices: generic, debian, sles, redhat, alpine, systemd, openrc, openbsd, solaris, freebsd)[Default: (null)]
version_added: 2.9