o nnhp@sddlmZdZdZddlZddlZddlZddlZddlm m m Z ddl mZmZmZddlmZddlmZddlmZmZdd lmZmZd d d d ddddddddd d ZGdddeZGdddeZGdddeZGdddeZ Gddde Z!Gdd d eZ"Gd!d"d"e Z#Gd#d$d$eZ$Gd%d&d&eZ%Gd'd(d(e Z&Gd)d*d*eZ'Gd+d,d,eZ(Gd-d.d.eZ)Gd/d0d0eZ*Gd1d2d2e*Z+Gd3d4d4e*Z,Gd5d6d6e*Z-Gd7d8d8e*Z.Gd9d:d:e*Z/Gd;d<dd>e*Z1Gd?d@d@e*Z2GdAdBdBe*Z3GdCdDdDe*Z4GdEdFdFe*Z5GdGdHdHe*Z6GdIdJdJe*Z7GdKdLdLe*Z8GdMdNdNe*Z9GdOdPdPe*Z:GdQdRdRe*Z;GdSdTdTe*ZGdYdZdZe*Z?Gd[d\d\e*Z@Gd]d^d^e*ZAGd_d`d`e*ZBGdadbdbe*ZCGdcdddde*ZDGdedfdfe*ZEGdgdhdhe*ZFGdidjdje*ZGGdkdldle*ZHGdmdndne*ZIGdodpdpe*ZJGdqdrdre*ZKGdsdtdte*ZLGdudvdve*ZMdwdxZNeOdykreNdSdS)z) annotationsa --- module: hostname author: - Adrian Likins (@alikins) - Hideki Saito (@saito-hideki) version_added: "1.4" short_description: Manage hostname requirements: [ hostname ] description: - Set system's hostname. Supports most OSs/Distributions including those using C(systemd). - Windows, HP-UX, and AIX are not currently supported. notes: - This module does B(NOT) modify C(/etc/hosts). You need to modify it yourself using other modules such as M(ansible.builtin.template) or M(ansible.builtin.replace). - On macOS, this module uses C(scutil) to set C(HostName), C(ComputerName), and C(LocalHostName). Since C(LocalHostName) cannot contain spaces or most special characters, this module will replace characters when setting C(LocalHostName). options: name: description: - Name of the host. - If the value is a fully qualified domain name that does not resolve from the given host, this will cause the module to hang for a few seconds while waiting for the name resolution attempt to timeout. type: str required: true use: description: - Which strategy to use to update the hostname. - If not set we try to autodetect, but this can be problematic, particularly with containers as they can present misleading information. - Note that 'systemd' should be specified for RHEL/EL/CentOS 7+. Older distributions should use 'redhat'. choices: ['alpine', 'debian', 'freebsd', 'generic', 'macos', 'macosx', 'darwin', 'openbsd', 'openrc', 'redhat', 'sles', 'solaris', 'systemd'] type: str version_added: '2.9' extends_documentation_fragment: - action_common_attributes - action_common_attributes.facts attributes: check_mode: support: full diff_mode: support: full facts: support: full platform: platforms: posix z - name: Set a hostname ansible.builtin.hostname: name: web01 - name: Set a hostname specifying strategy ansible.builtin.hostname: name: web01 use: systemd N) AnsibleModuleget_distributionget_distribution_version)get_platform_subclass)ServiceMgrFactCollector)get_file_linesget_file_content) to_nativeto_textAlpineSystemdFreeBSDBaseDarwinOpenBSDOpenRCRedHatSLESSolaris) alpinedebianfreebsdgenericmacosmacosxdarwinopenbsdopenrcredhatslessolarissystemdc@LeZdZddZddZddZddZd d Zd d Zd dZ ddZ dS) BaseStrategycCs||_d|_dS)NF)modulechangedselfr%r)C/usr/local/lib/python3.10/dist-packages/ansible/modules/hostname.py__init__gs zBaseStrategy.__init__cC|||jSN)update_current_hostnameupdate_permanent_hostnamer&r(r)r)r*%update_current_and_permanent_hostnameksz2BaseStrategy.update_current_and_permanent_hostnamecC<|jjd}|}||kr|jjs||d|_dSdSNnameT)r%paramsget_current_hostname check_modeset_current_hostnamer&)r(r4 current_namer)r)r*r.p   z$BaseStrategy.update_current_hostnamecCr2r3)r%r5get_permanent_hostnamer7set_permanent_hostnamer&)r(r4permanent_namer)r)r*r/xr:z&BaseStrategy.update_permanent_hostnamecCs|Sr-)r;r0r)r)r*r6sz!BaseStrategy.get_current_hostnamecCdSr-r)r(r4r)r)r*r8z!BaseStrategy.set_current_hostnamecCtr-NotImplementedErrorr0r)r)r*r;r@z#BaseStrategy.get_permanent_hostnamecCrAr-rBr?r)r)r*r<r@z#BaseStrategy.set_permanent_hostnameN) __name__ __module__ __qualname__r+r1r.r/r6r8r;r<r)r)r)r*r$fs r$c@r#)UnimplementedStrategycC |dSr-unimplemented_errorr0r)r)r*r1 z;UnimplementedStrategy.update_current_and_permanent_hostnamecCrHr-rIr0r)r)r*r.rKz-UnimplementedStrategy.update_current_hostnamecCrHr-rIr0r)r)r*r/rKz/UnimplementedStrategy.update_permanent_hostnamecCrHr-rIr0r)r)r*r6rKz*UnimplementedStrategy.get_current_hostnamecCrHr-rIr?r)r)r*r8rKz*UnimplementedStrategy.set_current_hostnamecCrHr-rIr0r)r)r*r;rKz,UnimplementedStrategy.get_permanent_hostnamecCrHr-rIr?r)r)r*r<rKz,UnimplementedStrategy.set_permanent_hostnamecCs>t}t}|durd||f}n|}|jjd|ddS)Nz%s (%s)z-hostname module cannot be used on platform %smsg)platformsystemrr% fail_json)r(rO distribution msg_platformr)r)r*rJs z)UnimplementedStrategy.unimplemented_errorN) rDrErFr1r.r/r6r8r;r<rJr)r)r)r*rGs rGcs@eZdZdZfddZddZddZdd Zd d ZZ S) CommandStrategyhostnamec&tt|||j|jd|_dSNT)superrSr+r% get_bin_pathCOMMAND hostname_cmdr' __class__r)r*r+zCommandStrategy.__init__cCF|jg}|j|\}}}|dkr|jjd|||fdt|SNr$Command failed rc=%d, out=%s, err=%srLrZr% run_commandrPr stripr(cmdrcouterrr)r)r*r6  z$CommandStrategy.get_current_hostnamecCD|j|g}|j|\}}}|dkr |jjd|||fddSdSr_rZr%rbrPr(r4rerfrgrhr)r)r*r8 z$CommandStrategy.set_current_hostnamecCsdS)NUNKNOWNr)r0r)r)r*r;r@z&CommandStrategy.get_permanent_hostnamecCr>r-r)r?r)r)r*r<r@z&CommandStrategy.set_permanent_hostname) rDrErFrYr+r6r8r;r< __classcell__r)r)r[r*rSs rSc@s eZdZdZddZddZdS) FileStrategy /etc/hostnamec Cshtj|js dSz t|jdddWSty3}z|jjdt|t dWYd}~dSd}~ww)NT)defaultrcfailed to read hostname: %srM exception) ospathisfileFILEr Exceptionr%rPr traceback format_exc)r(er)r)r*r;s z#FileStrategy.get_permanent_hostnamec Csz"t|jd}|d|WdWdS1swYWdStyC}z|jjdt|tdWYd}~dSd}~ww)Nw+z%s failed to update hostname: %sru) openrzwriter{r%rPr r|r})r(r4fr~r)r)r*r<s& z#FileStrategy.set_permanent_hostnameN)rDrErFrzr;r<r)r)r)r*rps rpc@seZdZdZdZdS) SLESStrategyzW This is a SLES Hostname strategy class - it edits the /etc/HOSTNAME file. z /etc/HOSTNAMEN)rDrErF__doc__rzr)r)r)r*rsrc@$eZdZdZdZddZddZdS)RedHatStrategyzb This is a Redhat Hostname strategy class - it edits the /etc/sysconfig/network file. z/etc/sysconfig/networkc Csz-t|jD]}t|}|dr!|d\}}|WSq|jd|jWdStyN}z|jjdt|t dWYd}~dSd}~ww)NHOSTNAME=z%Unable to locate HOSTNAME entry in %srtru) r NETWORK_FILEr rc startswithsplitr%rPr{r|r})r(linekvr~r)r)r*r;s"    z%RedHatStrategy.get_permanent_hostnamec CszXg}d}t|jddp d}|dD]}t|}|dr*|d|d}q||q|s9|d|t|jd}||WdWdS1sQwYWdSt yy}z|j j dt|t d WYd}~dSd}~ww) NFrcrrTrz HOSTNAME=%s rrru)r r splitlinesr rcrappendr writelinesr{r%rPr|r})r(r4linesfoundcontentrrr~r)r)r*r<s,  & z%RedHatStrategy.set_permanent_hostnameN)rDrErFrrr;r<r)r)r)r*rs  rcs(eZdZdZdZdZfddZZS)AlpineStrategyz This is a Alpine Linux Hostname manipulation strategy class - it edits the /etc/hostname file then run hostname -F /etc/hostname. rqrTcsftt|||j|jd}|d|jg}|j|\}}}|dkr1|jjd|||fddSdS)NTz-Frr`rL) rWrr8r%rXrYrzrbrP)r(r4rZrerfrgrhr[r)r*r8s z#AlpineStrategy.set_current_hostname)rDrErFrrzrYr8ror)r)r[r*rs rcsLeZdZdZdZfddZddZddZd d Zd d Z d dZ Z S)SystemdStrategyzg This is a Systemd hostname manipulation strategy class - it uses the hostnamectl command. hostnamectlcrUrV)rWrr+r%rXrYhostnamectl_cmdr'r[r)r*r+0r]zSystemdStrategy.__init__cCJ|jddg}|j|\}}}|dkr|jjd|||fdt|S)N --transientstatusrr`rLrr%rbrPr rcrdr)r)r*r64  z$SystemdStrategy.get_current_hostnamecCsbt|dkr |jjdd|jdd|g}|j|\}}}|dkr/|jjd|||fddSdS)N@Oname cannot be longer than 64 characters on systemd servers, try a shorter namerLr set-hostnamerr`lenr%rPrrbrlr)r)r*r8;s z$SystemdStrategy.set_current_hostnamecCr)N--staticrrr`rLrrdr)r)r*r;Crz&SystemdStrategy.get_permanent_hostnamecCsdt|dkr |jjdd|jddd|g}|j|\}}}|dkr0|jjd|||fddSdS) NrrrLz--prettyrrrr`rrlr)r)r*r<Js z&SystemdStrategy.set_permanent_hostnamecCr,r-)r/r.r&r0r)r)r*r1Rsz5SystemdStrategy.update_current_and_permanent_hostname) rDrErFrrYr+r6r8r;r<r1ror)r)r[r*r(s rc@r)OpenRCStrategyzv This is a Gentoo (OpenRC) Hostname manipulation strategy class - it edits the /etc/conf.d/hostname file. z/etc/conf.d/hostnamec Ctj|js dSz t|jD]}|}|dr&|dddWSqWdStyJ}z|jj dt |t dWYd}~dSd}~wwNrr hostname= "rtru rwrxryrzrrcrr{r%rPr r|r}r(rr~r)r)r*r;b   z%OpenRCStrategy.get_permanent_hostnamec CszEddt|jD}t|D]\}}|dr d|||<nqt|jd}|d|dWdWdS1s>wYWdStyf}z|jj dt |t dWYd}~dSd}~ww) NcSg|]}|qSr)r.0xr)r)r* rz9OpenRCStrategy.set_permanent_hostname..r hostname="%s"w rru) rrz enumeraterrrjoinr{r%rPr r|r}r(r4rirrr~r)r)r*r<ps"  & z%OpenRCStrategy.set_permanent_hostnameN)rDrErFrrzr;r<r)r)r)r*rZs  rcs8eZdZdZdZdZfddZddZdd ZZ S) OpenBSDStrategyz This is a OpenBSD family Hostname manipulation strategy class - it edits the /etc/myname file for the permanent hostname and executes hostname command for the current hostname. z /etc/mynamerTcrUrV)rWrr+r%rXrYrZr'r[r)r*r+r]zOpenBSDStrategy.__init__cCr^r_rardr)r)r*r6riz$OpenBSDStrategy.get_current_hostnamecCrjr_rkrlr)r)r*r8rmz$OpenBSDStrategy.set_current_hostname) rDrErFrrzrYr+r6r8ror)r)r[r*rs rcs<eZdZdZdZfddZddZddZd d ZZ S) SolarisStrategyzn This is a Solaris11 or later Hostname manipulation strategy class - it execute hostname command. rTcrUrV)rWrr+r%rXrYrZr'r[r)r*r+r]zSolarisStrategy.__init__cCsJd}|j||g}|j|\}}}|dkr#|jjd|||fddSdS)Nz-trr`rLrk)r(r4 cmd_optionrerfrgrhr)r)r*r8s  z$SolarisStrategy.set_current_hostnamecCsVd}d}d||f}|jj|dd\}}}|dkr%|jjd|||fdt|S) Nzsvc:/system/identity:nodezconfig/nodenamez+/usr/sbin/svccfg -s %s listprop -o value %sT)use_unsafe_shellrr`rL)r%rbrPr rc)r(fmripatternrerfrgrhr)r)r*r;s  z&SolarisStrategy.get_permanent_hostnamecCrjr_rkrlr)r)r*r<rmz&SolarisStrategy.set_permanent_hostname) rDrErFrrYr+r8r;r<ror)r)r[r*rs  rcsHeZdZdZdZdZfddZddZdd Zd d Z d d Z Z S)FreeBSDStrategyzq This is a FreeBSD hostname manipulation strategy class - it edits the /etc/rc.conf.d/hostname file. z/etc/rc.conf.d/hostnamerTcrUrV)rWrr+r%rXrYrZr'r[r)r*r+r]zFreeBSDStrategy.__init__cCr^r_rardr)r)r*r6riz$FreeBSDStrategy.get_current_hostnamecCrjr_rkrlr)r)r*r8rmz$FreeBSDStrategy.set_current_hostnamec Crrrrr)r)r*r;rz&FreeBSDStrategy.get_permanent_hostnamec CszRtj|jr)ddt|jD}t|D]\}}|dr'd|||<nqnd|g}t|jd}|d |dWdWdS1sKwYWdSt ys}z|j j dt |tdWYd}~dSd}~ww) NcSrr)rrr)r)r*rrz:FreeBSDStrategy.set_permanent_hostname..rrrrrru)rwrxryrzrrrrrrr{r%rPr r|r}rr)r)r*r<s(   & z&FreeBSDStrategy.set_permanent_hostname) rDrErFrrzrYr+r6r8r;r<ror)r)r[r*rs rcsXeZdZdZfddZddZddZdd Zd d Zd d Z ddZ ddZ Z S)DarwinStrategya> This is a macOS hostname manipulation strategy class. It uses /usr/sbin/scutil to set ComputerName, HostName, and LocalHostName. HostName corresponds to what most platforms consider to be hostname. It controls the name used on the command line and SSH. However, macOS also has LocalHostName and ComputerName settings. LocalHostName controls the Bonjour/ZeroConf name, used by services like AirDrop. This class implements a method, _scrub_hostname(), that mimics the transformations macOS makes on hostnames when enterened in the Sharing preference pane. It replaces spaces with dashes and removes all special characters. ComputerName is the name used for user-facing GUI services, like the System Preferences/Sharing pane and when users connect to the Mac over the network. cs>tt|||jdd|_d|_||jjd|_ dS)NscutilT)HostName ComputerName LocalHostNamer4) rWrr+r%rXr name_types_scrub_hostnamer5 scrubbed_namer'r[r)r*r+szDarwinStrategy.__init__cCsZt|}d}d}t|dt||}||}d|vr&|dd}d|vs|d}|S)z LocalHostName only accepts valid DNS characters while HostName and ComputerName accept a much wider range of characters. This function aims to mimic how macOS translates a friendly name to the LocalHostName. z'"~`!@#$%^&*(){}[]/=?+\|-_ z.'-z--rr)r str maketransr translatereplacerstrip)r(r4 replace_chars delete_charstabler)r)r*rs   zDarwinStrategy._scrub_hostnamecCsR|jddg}|j|\}}}|dkr#d|vr#|jjd|||fdt|S)N--getrrzHostName: not setz4Failed to get current hostname rc=%d, out=%s, err=%srLrr%rbrPr rcrdr)r)r*r6+s  z#DarwinStrategy.get_current_hostnamecCr)Nrrrz6Failed to get permanent hostname rc=%d, out=%s, err=%srLrrdr)r)r*r;3s  z%DarwinStrategy.get_permanent_hostnamecCs|jD]<}|jd|g}|dkr|t|jn|t||j|\}}}|dkr?|jjdt|t|t||dqdS)Nz--setrrz#Failed to set {3} to '{2}': {0} {1}rL) rrrr rr%rbrPformat)r(r4 hostname_typererfrgrhr)r)r*r<;s  &z%DarwinStrategy.set_permanent_hostnamecCr>r-r)r?r)r)r*r8Fr@z#DarwinStrategy.set_current_hostnamecCr>r-r)r0r)r)r*r.Ir@z&DarwinStrategy.update_current_hostnamecsfjjdtfddjD}tfddjD}||kr1jjs,d_dSdS)Nr4c3s,|]}jjd|gdVqdS)rN)r%rbrrc)r name_typer0r)r* Ps*z;DarwinStrategy.update_permanent_hostname..c3s"|] }|dkr jnVqdS)rN)r)rnr4r(r)r*rSs T)r%r5tuplerr7r<r&)r( all_namesexpected_namesr)rr*r/Ls   z(DarwinStrategy.update_permanent_hostname) rDrErFrr+rr6r;r<r8r.r/ror)r)r[r*rs  rcs\eZdZdZdZdZeZfddZddZ dd Z d d Z d d Z ddZ ddZZS)Hostnamez This is a generic Hostname manipulation class that is subclassed based on platform. A subclass may wish to set different strategy instance to self.strategy. All subclasses MUST define platform and distribution (which may be None). GenericNcstt}t|||Sr-)rrrW__new__)clsargskwargsnew_clsr[r)r*rjszHostname.__new__cCs~||_|jd|_|jd|_|jdur%tdt|j}|||_dStdkr7t |r7t ||_dS| ||_dS)Nr4usez %sStrategyLinux) r%r5r4rglobalsSTRATSstrategyrNrOris_systemd_managedrstrategy_class)r(r%stratr)r)r*r+ns   zHostname.__init__cC |jSr-)rr1r0r)r)r*r1| z.Hostname.update_current_and_permanent_hostnamecCrr-)rr6r0r)r)r*r6rzHostname.get_current_hostnamecC|j|dSr-)rr8r?r)r)r*r8zHostname.set_current_hostnamecCrr-)rr;r0r)r)r*r;rzHostname.get_permanent_hostnamecCrr-)rr<r?r)r)r*r<rzHostname.set_permanent_hostname)rDrErFrrNrQrGrrr+r1r6r8r;r<ror)r)r[r*r\s  rc@sdeZdZdZdZzeZer#deekrdkr#ne eZ WdSe e y1e Z YdSw) SLESHostnamerSlesr N) rDrErFrNrQrdistribution_versionfloatrr ValueErrorrGr)r)r)r*rs   rc@eZdZdZdZeZdS) RHELHostnamerRedhatNrDrErFrNrQrrr)r)r)r*rrc@r)CentOSHostnamerCentosNrr)r)r)r*rrrc@r)AnolisOSHostnamerAnolisNrr)r)r)r*rrrc@r)CloudlinuxserverHostnamerCloudlinuxserverNrr)r)r)r*rrrc@r)CloudlinuxHostnamer CloudlinuxNrr)r)r)r*rrrc@r)AlinuxHostnamerAlinuxNrr)r)r)r*rrrc@r)ScientificHostnamer ScientificNrr)r)r)r*rrrc@r)OracleLinuxHostnamerOracleNrr)r)r)r*r rr c@r)VirtuozzoLinuxHostnamer VirtuozzoNrr)r)r)r*r rr c@r)AmazonLinuxHostnamerAmazonNrr)r)r)r*rrrc@r)DebianHostnamerDebianNrDrErFrNrQrprr)r)r)r*rrrc@r) KylinHostnamerKylinNrr)r)r)r*rrrc@r)CumulusHostnamerz Cumulus-linuxNrr)r)r)r*rrrc@r) KaliHostnamerKaliNrr)r)r)r*rrrc@r)ParrotHostnamerParrotNrr)r)r)r*rrrc@r)UbuntuHostnamerUbuntuNrr)r)r)r*rrrc@r)LinuxmintHostnamer LinuxmintNrr)r)r)r*rrrc@r)LinaroHostnamerLinaroNrr)r)r)r*rrrc@r)DevuanHostnamerDevuanNrr)r)r)r*r rr c@r)RaspbianHostnamerRaspbianNrr)r)r)r*r" rr"c@r) UosHostnamerUosNrr)r)r)r*r$rr$c@r)DeepinHostnamerDeepinNrr)r)r)r*r&rr&c@r)GentooHostnamerGentooN)rDrErFrNrQrrr)r)r)r*r(rr(c@r)ALTLinuxHostnamerAltlinuxNrr)r)r)r*r*$rr*c@r)AlpineLinuxHostnamerr N)rDrErFrNrQrrr)r)r)r*r,*rr,c@eZdZdZdZeZdS)OpenBSDHostnamerN)rDrErFrNrQrrr)r)r)r*r.0rr.c@r-)SolarisHostnameSunOSN)rDrErFrNrQrrr)r)r)r*r/6rr/c@r-)FreeBSDHostnamerNrDrErFrNrQrrr)r)r)r*r1<rr1c@r-)NetBSDHostnameNetBSDNr2r)r)r)r*r3Brr3c@r) NeonHostnamerNeonNrr)r)r)r*r5Hrr5c@r-)DarwinHostnamerN)rDrErFrNrQrrr)r)r)r*r7Nrr7c@r)VoidLinuxHostnamerVoidNrr)r)r)r*r8Trr8c@r) PopHostnamerPopNrr)r)r)r*r:Zrr:c@r)EurolinuxHostnamer EurolinuxNrr)r)r)r*r<`rr<c Cstttdddtdttdddd}t|}|jd}|}|}| }||kr2|}n ||kr9|}n|}t||t| dd |t d t dd dd d }|rld |dd |dd|d<|jdi|dS)NrT)typerequired)r>choices)r4r) argument_specsupports_check_moder4.rr)ansible_hostnameansible_nodename ansible_fqdnansible_domain)r&r4 ansible_factsz hostname = r)afterbeforediffr))rdictlistrkeysrr5r6r;r1rsocketgetfqdnr exit_json)r%rTr4current_hostnamepermanent_hostnamer& name_beforekwr)r)r*mainfs:     rV__main__)P __future__r DOCUMENTATIONEXAMPLESrwrNrOr|"ansible.module_utils.compat.typing module_utilscompattypingtansible.module_utils.basicrrr$ansible.module_utils.common.sys_infor-ansible.module_utils.facts.system.service_mgrr ansible.module_utils.facts.utilsrr +ansible.module_utils.common.text.convertersr r robjectr$rGrSrprrrrrrrrrrrrrrrrrrr r rrrrrrrrrr r"r$r&r(r*r,r.r/r1r3r5r7r8r:r<rVrDr)r)r)r*s /   '!,2'#<`0 '