a )g{p@sddlmZmZmZeZdZdZddlZddl Z ddl Z ddl Z ddl m mmZddlmZmZmZddlmZddlmZddlmZmZdd lmZmZdd lm Z m!Z!d d d dddddddddd d Z"Gddde#Z$Gddde$Z%Gddde$Z&Gddde$Z'Gddde'Z(Gd d!d!e$Z)Gd"d#d#e'Z*Gd$d%d%e$Z+Gd&d'd'e$Z,Gd(d)d)e'Z-Gd*d+d+e$Z.Gd,d-d-e$Z/Gd.d/d/e$Z0Gd0d1d1e#Z1Gd2d3d3e1Z2Gd4d5d5e1Z3Gd6d7d7e1Z4Gd8d9d9e1Z5Gd:d;d;e1Z6Gdd?d?e1Z8Gd@dAdAe1Z9GdBdCdCe1Z:GdDdEdEe1Z;GdFdGdGe1ZGdLdMdMe1Z?GdNdOdOe1Z@GdPdQdQe1ZAGdRdSdSe1ZBGdTdUdUe1ZCGdVdWdWe1ZDGdXdYdYe1ZEGdZd[d[e1ZFGd\d]d]e1ZGGd^d_d_e1ZHGd`dadae1ZIGdbdcdce1ZJGdddedee1ZKGdfdgdge1ZLGdhdidie1ZMGdjdkdke1ZNGdldmdme1ZOGdndodoe1ZPGdpdqdqe1ZQGdrdsdse1ZRGdtdudue1ZSGdvdwdwe1ZTdxdyZUeVdzkreUdS){)absolute_importdivisionprint_functiona --- 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_text)PY3 text_typeAlpineZSystemdFreeBSDZBaseDarwinOpenBSDZOpenRCZRedHatZSLESZSolaris) ZalpineZdebianZfreebsdZgenericZmacosZmacosxdarwinZopenbsdZopenrcZredhatZslesZsolarisZsystemdc@sLeZdZddZddZddZddZd d Zd d Zd dZ ddZ dS) BaseStrategycCs||_d|_dS)NF)modulechangedselfrrt}t}|dur$d||f}n|}|jjd|ddS)Nz%s (%s)z-hostname module cannot be used on platform %smsg)platformsystemrr fail_json)rr6 distributionZ msg_platformrrrr2sz)UnimplementedStrategy.unimplemented_errorN) r-r.r/r!rrr%r'r(r)r2rrrrr0sr0cs@eZdZdZfddZddZddZdd Zd d ZZ S) CommandStrategyhostnamecs&tt|||j|jd|_dSNT)superr9rr get_bin_pathCOMMAND hostname_cmdr __class__rrrszCommandStrategy.__init__cCsF|jg}|j|\}}}|dkr:|jjd|||fdt|SNr$Command failed rc=%d, out=%s, err=%sr3r?r run_commandr7r striprcmdrcouterrrrrr%s z$CommandStrategy.get_current_hostnamecCs@|j|g}|j|\}}}|dkr<|jjd|||fddSrBr?rrEr7rr#rHrIrJrKrrrr's z$CommandStrategy.set_current_hostnamecCsdS)NZUNKNOWNrr rrrr(sz&CommandStrategy.get_permanent_hostnamecCsdSrrr*rrrr)sz&CommandStrategy.set_permanent_hostname) r-r.r/r>rr%r'r(r) __classcell__rrr@rr9s  r9c@s eZdZdZddZddZdS) FileStrategy /etc/hostnamec Csjtj|jsdSzt|jdddWStyd}z(|jjdt|t dWYd}~n d}~00dS)NT)defaultrFfailed to read hostname: %sr4 exception) ospathisfileFILEr Exceptionrr7r traceback format_exc)rerrrr(s z#FileStrategy.get_permanent_hostnamec Csz>t|jd}|d|Wdn1s20YWn@ty~}z(|jjdt|tdWYd}~n d}~00dS)Nw+z%s failed to update hostname: %srT) openrYwriterZrr7r r[r\)rr#fr]rrrr)s0 z#FileStrategy.set_permanent_hostnameN)r-r.r/rYr(r)rrrrrOs rOc@seZdZdZdZdS) SLESStrategyzW This is a SLES Hostname strategy class - it edits the /etc/HOSTNAME file. z /etc/HOSTNAMENr-r.r/__doc__rYrrrrrcsrcc@s$eZdZdZdZddZddZdS)RedHatStrategyzb This is a Redhat Hostname strategy class - it edits the /etc/sysconfig/network file. z/etc/sysconfig/networkc CszXt|jD]6}t|}|dr |d\}}|WSq |jd|jWn@ty}z(|jjdt|t dWYd}~n d}~00dS)NHOSTNAME=z%Unable to locate HOSTNAME entry in %srSrT) r NETWORK_FILEr rF startswithsplitrr7rZr[r\)rlinekvr]rrrr(s   z%RedHatStrategy.get_permanent_hostnamec Cszg}d}t|jddpd}|dD]8}t|}|drT|d|d}q&||q&|sr|d|t|jd}||Wdn1s0YWn@t y}z(|j j dt|t d WYd}~n d}~00dS) NFrFrQTrgz HOSTNAME=%s r^r_rT)r ri splitlinesr rFrjappendr` writelinesrZrr7r[r\)rr#linesfoundZcontentrlrbr]rrrr)s& , z%RedHatStrategy.set_permanent_hostnameN)r-r.r/rerir(r)rrrrrfsrfcs(eZdZdZdZdZfddZZS)AlpineStrategyz This is a Alpine Linux Hostname manipulation strategy class - it edits the /etc/hostname file then run hostname -F /etc/hostname. rPr:csbtt|||j|jd}|d|jg}|j|\}}}|dkr^|jjd|||fddS)NTz-FrrCr3) r<rur'rr=r>rYrEr7)rr#r?rHrIrJrKr@rrr' s  z#AlpineStrategy.set_current_hostname)r-r.r/rerYr>r'rNrrr@rrusrucsLeZdZdZdZfddZddZddZd d Zd d Z d dZ Z S)SystemdStrategyzg This is a Systemd hostname manipulation strategy class - it uses the hostnamectl command. Z hostnamectlcs&tt|||j|jd|_dSr;)r<rvrrr=r>hostnamectl_cmdrr@rrr2szSystemdStrategy.__init__cCsJ|jddg}|j|\}}}|dkr>|jjd|||fdt|S)N --transientstatusrrCr3rwrrEr7r rFrGrrrr%6s  z$SystemdStrategy.get_current_hostnamecCs^t|dkr|jjdd|jdd|g}|j|\}}}|dkrZ|jjd|||fddS)N@Oname cannot be longer than 64 characters on systemd servers, try a shorter namer3rx set-hostnamerrClenrr7rwrErMrrrr'=s  z$SystemdStrategy.set_current_hostnamecCsJ|jddg}|j|\}}}|dkr>|jjd|||fdt|S)N--staticryrrCr3rzrGrrrr(Es  z&SystemdStrategy.get_permanent_hostnamecCs`t|dkr|jjdd|jddd|g}|j|\}}}|dkr\|jjd|||fddS) Nr{r|r3z--prettyrr}rrCr~rMrrrr)Ls  z&SystemdStrategy.set_permanent_hostnamecCs|||jSr)rrrr rrrr!Tsz5SystemdStrategy.update_current_and_permanent_hostname) r-r.r/rer>rr%r'r(r)r!rNrrr@rrv*s rvc@s$eZdZdZdZddZddZdS)OpenRCStrategyzv This is a Gentoo (OpenRC) Hostname manipulation strategy class - it edits the /etc/conf.d/hostname file. z/etc/conf.d/hostnamec Cstj|jsdSz>t|jD].}|}|dr|dddWSqWn@ty}z(|jj dt |t dWYd}~n d}~00dSNrQ hostname= "rSrT rVrWrXrYr rFrjrZrr7r r[r\rrlr]rrrr(ds  z%OpenRCStrategy.get_permanent_hostnamec Cszddt|jD}t|D]"\}}|drd|||<qBqt|jd$}|d|dWdn1sx0YWn@ty}z(|jj dt |t dWYd}~n d}~00dS) NcSsg|] }|qSrro.0xrrr tz9OpenRCStrategy.set_permanent_hostname..r hostname="%s"w r_rT) r rY enumeraterjr`rajoinrZrr7r r[r\rr#rsirlrbr]rrrr)rs  6 z%OpenRCStrategy.set_permanent_hostnameN)r-r.r/rerYr(r)rrrrr\src@seZdZdZdZdS)OpenBSDStrategyzl This is a OpenBSD family Hostname manipulation strategy class - it edits the /etc/myname file. z /etc/mynameNrdrrrrrsrcs<eZdZdZdZfddZddZddZd d ZZ S) SolarisStrategyzn This is a Solaris11 or later Hostname manipulation strategy class - it execute hostname command. r:cs&tt|||j|jd|_dSr;)r<rrrr=r>r?rr@rrrszSolarisStrategy.__init__cCsFd}|j||g}|j|\}}}|dkrB|jjd|||fddS)Nz-trrCr3rL)rr#Z cmd_optionrHrIrJrKrrrr's  z$SolarisStrategy.set_current_hostnamecCsVd}d}d||f}|jj|dd\}}}|dkrJ|jjd|||fdt|S) Nzsvc:/system/identity:nodezconfig/nodenamez+/usr/sbin/svccfg -s %s listprop -o value %sT)Zuse_unsafe_shellrrCr3)rrEr7r rF)rZfmripatternrHrIrJrKrrrr(s z&SolarisStrategy.get_permanent_hostnamecCs@|j|g}|j|\}}}|dkr<|jjd|||fddSrBrLrMrrrr)s z&SolarisStrategy.set_permanent_hostname) r-r.r/rer>rr'r(r)rNrrr@rrs   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/hostnamer:cs&tt|||j|jd|_dSr;)r<rrrr=r>r?rr@rrrszFreeBSDStrategy.__init__cCsF|jg}|j|\}}}|dkr:|jjd|||fdt|SrBrDrGrrrr%s z$FreeBSDStrategy.get_current_hostnamecCs@|j|g}|j|\}}}|dkr<|jjd|||fddSrBrLrMrrrr's z$FreeBSDStrategy.set_current_hostnamec Cstj|jsdSz>t|jD].}|}|dr|dddWSqWn@ty}z(|jj dt |t dWYd}~n d}~00dSrrrrrrr(s  z&FreeBSDStrategy.get_permanent_hostnamec Csztj|jrRddt|jD}t|D]"\}}|dr,d|||<q\q,n d|g}t|jd$}|d |dWdn1s0YWn@t y}z(|j j dt |tdWYd}~n d}~00dS) NcSsg|] }|qSrrorrrrrrz:FreeBSDStrategy.set_permanent_hostname..rrrrr_rT)rVrWrXrYr rrjr`rarrZrr7r r[r\rrrrr)s   6 z&FreeBSDStrategy.set_permanent_hostname) r-r.r/rerYr>rr%r'r(r)rNrrr@rrs rcs`eZdZdZfddZddZddZdd Zd d Zd d Z ddZ 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 LocalHostNamer#) r<rrrr=r name_types_scrub_hostnamer$ scrubbed_namerr@rrrszDarwinStrategy.__init__cCsxtrt|||St|tr&t|ts.tdt|t|krFtdttdd|D|}|D]}d|t |<qb|S)Nz8replace_chars and replacement_chars must both be stringsz:replacement_chars must be the same length as replace_charscss|]}t|VqdSr)ord)rcrrr rz3DarwinStrategy._make_translation..) rstr maketrans isinstancer ValueErrorrdictzipr)r replace_charsZreplacement_chars delete_charstablecharrrr_make_translationsz DarwinStrategy._make_translationcCsTt|}d}d}||dt||}||}d|vrF|dd}q0|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--rQ)r rr translatereplacerstrip)rr#rrrrrrrs  zDarwinStrategy._scrub_hostnamecCsR|jddg}|j|\}}}|dkrFd|vrF|jjd|||fdt|S)N--getrrzHostName: not setz4Failed to get current hostname rc=%d, out=%s, err=%sr3rrrEr7r rFrGrrrr%)s  z#DarwinStrategy.get_current_hostnamecCsJ|jddg}|j|\}}}|dkr>|jjd|||fdt|S)Nrrrz6Failed to get permanent hostname rc=%d, out=%s, err=%sr3rrGrrrr(1s  z%DarwinStrategy.get_permanent_hostnamecCs|jD]x}|jd|g}|dkr0|t|jn|t||j|\}}}|dkr|jjdt|t|t||dqdS)Nz--setrrz#Failed to set {3} to '{2}': {0} {1}r3) rrrqr rrrEr7format)rr#Z hostname_typerHrIrJrKrrrr)9s  z%DarwinStrategy.set_permanent_hostnamecCsdSrrr*rrrr'Dsz#DarwinStrategy.set_current_hostnamecCsdSrrr rrrrGsz&DarwinStrategy.update_current_hostnamecsbjjdtfddjD}tfddjD}||kr^jjsXd_dS)Nr#c3s*|]"}jjd|gdVqdS)rN)rrErrF)rZ name_typer rrrNrz;DarwinStrategy.update_permanent_hostname..c3s |]}|dkrjnVqdS)rN)r)rnr#rrrrQrT)rr$tuplerr&r)r)rZ all_namesZexpected_namesrrrrJs  z(DarwinStrategy.update_permanent_hostname)r-r.r/rerrrr%r(r)r'rrrNrrr@rrs  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)rrr<__new__)clsargskwargsZnew_clsr@rrrhszHostname.__new__cCsz||_|jd|_|jd|_|jdurHtdt|j}|||_n.tdkrjt |rjt ||_n | ||_dS)Nr#usez %sStrategyLinux) rr$r#rglobalsSTRATSstrategyr5r6r Zis_systemd_managedrvstrategy_class)rrZstratrrrrls     zHostname.__init__cCs |jSr)rr!r rrrr!zsz.Hostname.update_current_and_permanent_hostnamecCs |jSr)rr%r rrrr%}szHostname.get_current_hostnamecCs|j|dSr)rr'r*rrrr'szHostname.set_current_hostnamecCs |jSr)rr(r rrrr(szHostname.get_permanent_hostnamecCs|j|dSr)rr)r*rrrr)szHostname.set_permanent_hostname)r-r.r/rer5r8r0rrrr!r%r'r(r)rNrrr@rrZs  rc@sbeZdZdZdZz6eZer>deekr4dkr>nneZ ne Wne y\e Z Yn0dS) SLESHostnamerZSlesr N) r-r.r/r5r8rZdistribution_versionfloatrcrrr0rrrrrs   rc@seZdZdZdZeZdS) RHELHostnamerZRedhatNr-r.r/r5r8rfrrrrrrsrc@seZdZdZdZeZdS)CentOSHostnamerZCentosNrrrrrrsrc@seZdZdZdZeZdS)AnolisOSHostnamerZAnolisNrrrrrrsrc@seZdZdZdZeZdS)CloudlinuxserverHostnamerZCloudlinuxserverNrrrrrrsrc@seZdZdZdZeZdS)CloudlinuxHostnamerZ CloudlinuxNrrrrrrsrc@seZdZdZdZeZdS)AlinuxHostnamerZAlinuxNrrrrrrsrc@seZdZdZdZeZdS)ScientificHostnamerZ ScientificNrrrrrrsrc@seZdZdZdZeZdS)OracleLinuxHostnamerZOracleNrrrrrrsrc@seZdZdZdZeZdS)VirtuozzoLinuxHostnamerZ VirtuozzoNrrrrrrsrc@seZdZdZdZeZdS)AmazonLinuxHostnamerZAmazonNrrrrrrsrc@seZdZdZdZeZdS)DebianHostnamerZDebianNr-r.r/r5r8rOrrrrrrsrc@seZdZdZdZeZdS) KylinHostnamerZKylinNrrrrrrsrc@seZdZdZdZeZdS)CumulusHostnamerz Cumulus-linuxNrrrrrrsrc@seZdZdZdZeZdS) KaliHostnamerZKaliNrrrrrrsrc@seZdZdZdZeZdS)ParrotHostnamerZParrotNrrrrrrsrc@seZdZdZdZeZdS)UbuntuHostnamerZUbuntuNrrrrrrsrc@seZdZdZdZeZdS)LinuxmintHostnamerZ LinuxmintNrrrrrrsrc@seZdZdZdZeZdS)LinaroHostnamerZLinaroNrrrrrrsrc@seZdZdZdZeZdS)DevuanHostnamerZDevuanNrrrrrrsrc@seZdZdZdZeZdS)RaspbianHostnamerZRaspbianNrrrrrr src@seZdZdZdZeZdS) UosHostnamerZUosNrrrrrrsrc@seZdZdZdZeZdS)DeepinHostnamerZDeepinNrrrrrrsrc@seZdZdZdZeZdS)GentooHostnamerZGentooN)r-r.r/r5r8rrrrrrrsrc@seZdZdZdZeZdS)ALTLinuxHostnamerZAltlinuxNrrrrrr"src@seZdZdZdZeZdS)AlpineLinuxHostnamerrN)r-r.r/r5r8rurrrrrr(src@seZdZdZdZeZdS)OpenBSDHostnamerN)r-r.r/r5r8rrrrrrr.src@seZdZdZdZeZdS)SolarisHostnameZSunOSN)r-r.r/r5r8rrrrrrr4src@seZdZdZdZeZdS)FreeBSDHostnamerNr-r.r/r5r8rrrrrrr:src@seZdZdZdZeZdS)NetBSDHostnameZNetBSDNrrrrrr@src@seZdZdZdZeZdS) NeonHostnamerZNeonNrrrrrrFsrc@seZdZdZdZeZdS)DarwinHostnamerN)r-r.r/r5r8rrrrrrrLsrc@seZdZdZdZeZdS)VoidLinuxHostnamerZVoidNrrrrrrRsrc@seZdZdZdZeZdS) PopHostnamerZPopNrrrrrrXsrc@seZdZdZdZeZdS)EurolinuxHostnamerZ EurolinuxNrrrrrr^src Cstttdddtdttdddd}t|}|jd}|}|}| }||krd|}n||krr|}n|}t||t| dd |t d t dd dd d }|rd |dd |dd|d<|jfi|dS)NrT)typerequired)rchoices)r#r)Z argument_specZsupports_check_moder#.rr)Zansible_hostnameZansible_nodenameZ ansible_fqdnZansible_domain)rr#Z ansible_factsz hostname = r)Zafterbeforediff)rrlistrkeysrr$r%r(r!rksocketZgetfqdnrZ exit_json)rr:r#Zcurrent_hostnameZpermanent_hostnamerZ name_beforekwrrrmainds:    r__main__)WZ __future__rrrrZ __metaclass__Z DOCUMENTATIONZEXAMPLESrVr5rr[Z"ansible.module_utils.compat.typingZ module_utilscompattypingtZansible.module_utils.basicrrrZ$ansible.module_utils.common.sys_inforZ-ansible.module_utils.facts.system.service_mgrr Z ansible.module_utils.facts.utilsr r Zansible.module_utils._textr r Zansible.module_utils.sixrrrobjectrr0r9rOrcrfrurvrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr-rrrrs/   '!,2' #<o0'