
    Vh;                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlZd dlmZ d dlmZ d Zd	 Zd
 Zd Zd Zd Zd Zd ZddZd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionaV  
module: interfaces_file
short_description: Tweak settings in C(/etc/network/interfaces) files
extends_documentation_fragment:
  - ansible.builtin.files
  - community.general.attributes
description:
  - Manage (add, remove, change) individual interface options in an interfaces-style file without having to manage the file
    as a whole with, say, M(ansible.builtin.template) or M(ansible.builtin.assemble). Interface has to be presented in a file.
  - Read information about interfaces from interfaces-styled files.
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  dest:
    type: path
    description:
      - Path to the interfaces file.
    default: /etc/network/interfaces
  iface:
    type: str
    description:
      - Name of the interface, required for value changes or option remove.
  address_family:
    type: str
    description:
      - Address family of the interface, useful if same interface name is used for both V(inet) and V(inet6).
  option:
    type: str
    description:
      - Name of the option, required for value changes or option remove.
  value:
    type: str
    description:
      - If O(option) is not presented for the O(iface) and O(state) is V(present) option will be added. If O(option) already
        exists and is not V(pre-up), V(up), V(post-up) or V(down), its value will be updated. V(pre-up), V(up), V(post-up)
        and V(down) options cannot be updated, only adding new options, removing existing ones or cleaning the whole option
        set are supported.
  backup:
    description:
      - Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered
        it incorrectly.
    type: bool
    default: false
  state:
    type: str
    description:
      - If set to V(absent) the option or section will be removed if present instead of created.
    default: "present"
    choices: ["present", "absent"]

notes:
  - If option is defined multiple times last one will be updated but all will be deleted in case of an absent state.
requirements: []
author: "Roman Belyakovsky (@hryamzik)"
a  
dest:
  description: Destination file/path.
  returned: success
  type: str
  sample: "/etc/network/interfaces"
ifaces:
  description: Interfaces dictionary.
  returned: success
  type: dict
  contains:
    ifaces:
      description: Interface dictionary.
      returned: success
      type: dict
      contains:
        eth0:
          description: Name of the interface.
          returned: success
          type: dict
          contains:
            address_family:
              description: Interface address family.
              returned: success
              type: str
              sample: "inet"
            method:
              description: Interface method.
              returned: success
              type: str
              sample: "manual"
            mtu:
              description: Other options, all values returned as strings.
              returned: success
              type: str
              sample: "1500"
            pre-up:
              description: List of C(pre-up) scripts.
              returned: success
              type: list
              elements: str
              sample:
                - "route add -net 10.10.10.0/24 gw 10.10.10.1 dev eth1"
                - "route add -net 10.10.11.0/24 gw 10.10.11.1 dev eth2"
            up:
              description: List of C(up) scripts.
              returned: success
              type: list
              elements: str
              sample:
                - "route add -net 10.10.10.0/24 gw 10.10.10.1 dev eth1"
                - "route add -net 10.10.11.0/24 gw 10.10.11.1 dev eth2"
            post-up:
              description: List of C(post-up) scripts.
              returned: success
              type: list
              elements: str
              sample:
                - "route add -net 10.10.10.0/24 gw 10.10.10.1 dev eth1"
                - "route add -net 10.10.11.0/24 gw 10.10.11.1 dev eth2"
            down:
              description: List of C(down) scripts.
              returned: success
              type: list
              elements: str
              sample:
                - "route del -net 10.10.10.0/24 gw 10.10.10.1 dev eth1"
                - "route del -net 10.10.11.0/24 gw 10.10.11.1 dev eth2"
z
- name: Set eth1 mtu configuration value to 8000
  community.general.interfaces_file:
    dest: /etc/network/interfaces.d/eth1.cfg
    iface: eth1
    option: mtu
    value: 8000
    backup: true
    state: present
  register: eth1_cfg
N)AnsibleModule)to_bytesc                     | ddS )Nunknown)line	line_type )r
   s    u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/interfaces_file.pylineDictr      s    y11    c                     | |||d|dS )Noption)r
   ifacer   valuer   address_familyr   )r
   r   r   r   r   s        r   
optionDictr      s!    5FUYa  vD  E  Er   c                    t        j                  d      }t        |j                  |             d   }|j	                         }| j                         d   }| j                  |      }t        |      }| ||z   d  j                         S )N\s+r   )	recompilelistfinditerstartsplitfindlenstrip)sspaceRemvalueEndr   optionStart	optionLens          r   getValueFromLiner(      sv    jj GWa !"%AwwyHWWYq\F&&.KFIY$%&,,..r   c                 ^    t        |d      5 }t        | |      cd d d        S # 1 sw Y   y xY w)Nr)openread_interfaces_lines)modulefilenamefs      r   read_interfaces_filer0      s-    	h	 0$VQ/0 0 0s   #,c           
         g }i }d }d}|D ]  }|dz  }|j                         }t        |      dk  r|j                  t        |             B|d   d   dk(  r|j                  t        |             h|d   dk(  r|j                  t        |             d}|d   dk(  r|j                  t        |             d}|d   dk(  r|j                  t        |             d}|d   d	k(  r|j                  t        |             d}|d   d
k(  rCg g g g d}|d   }		 |d   |d<   |d   }
	 |d   |d<   |||	<   |j                  ||	d
||
d       d}G|d   dk(  r|j                  t        |             d}m|d   j                  d      r|j                  t        |             d}|d   dk(  r|j                  t        |             d}|d   dk(  r|j                  t        |             d}|dk(  rO|d   }t        |      }|j                  t        |	||
             |dv r|   j                  |       8||<   ?|dk(  r|j                  t        |             `|dk(  r|j                  t        |             | j                  d||fz          y ||fS # t        $ r	 d |d<   Y w xY w# t        $ r	 d |d<   Y w xY w)Nr      #mappingMAPPINGsourceNONEz
source-dirzsource-directoryr   zpre-upupdownzpost-up   r      method)r
   r   r   paramsr   IFACEautozallow-zno-auto-downz
no-scriptszmisplaced option %s in line %dmsg)NN)	r   r    appendr   
IndexError
startswithr(   r   	fail_json)r-   line_stringslinesifacescurrently_processingir
   wordscurrif
iface_namer   option_namer   s                r   r,   r,      s   EF	A F"	Q

u:>LL$(8A;#LL$(8y LL$(#, 1X!LL$(#) 1X%LL$(#) 1X++LL$(#) 1X 	F qJ0+08'( $$45N(#(8x  "(F:LL$']c  xF  G  H#* 1XLL$(#) 1X  *LL$(#) 1X'LL$(#) 1X%LL$(#) #w.#Ah(.Zj+un]^"EE;'..u5*/F;'%2Xd^,%/Xd^,  %Eq	%Q R!MF"N &=O  0+/'(0
  (#'x (s$   J**J?*J<;J<?KKc                 >    | D cg c]  }|d   dk(  s| c}S c c}w )Nr   r   r   )iface_linesrK   s     r   get_interface_optionsrR     s!    "A!an&@AAAA   c                 >    | D cg c]  }|d   |k(  s| c}S c c}w )Nr   r   )iface_optionsr   rK   s      r   get_target_optionsrV     s!    $>!(v(=A>>>rS   c           	      t   | d   }| d   }|j                  | d         }t        | d         }t        j                  dj	                  t        t        j                  |j                                     |||z   d        }|j                         |z   |z   }|j                         |z   |z   }|d | |z   ||d  z   }	|	S )Nr
   r   r   r   )
r   r    r   searchjoinmapescaper   r   end)
target_optionr   old_line	old_valueprefix_startr'   old_value_positionr   r\   r
   s
             r   update_existing_option_linerb     s    V$Hg&I==x!89LM(+,I6;;s299ioo>O/P#QS[\hkt\t\uSvw$$&5	AE

 
 
"\
1I
=CFUe#hstn4DKr   c           	         t        |      d}|D cg c]  }d|v s|d   |k(  s| }	}||	D cg c]  }d|v r
|d   |k(  r| }	}t        |	      dk  r| j                  d|z         |d fS t        |	      }
t	        |
|      }|dk(  rt        |      dk  rd}|	d	   }t        |||||
|      \  }}||fS |d
v r?t        t        t        fd|                  dk  rvt        ||||d	   |
|      \  }}||fS |d	   d   k7  rPd}|d	   }t        |      }|d   }t        |      |d d d	   j                  |      z
  dz
  }t        ||||      ||<   ||fS |dk(  rtt        |      dk\  rb|d
v r@>dk7  r9|D cg c]  }|d   k(  s| c}D ]  }d}|D cg c]
  }||k7  s	| }} ||fS d}|D ]  }|D cg c]
  }||k7  s	| }} ||fS | j                  d|z         ||fS c c}w c c}w c c}w c c}w c c}w )NFr   r   r2   zError: interface %s not foundrA   presentTr   r8   c                     | d   k(  S )Nr   r   )rK   r   s    r   <lambda>z&set_interface_option.<locals>.<lambda>4  s    QwZ5-@ r   r   absentNonez?Error: unsupported state %s, has to be either present or absent)strr    rF   rR   rV   addOptionAfterLiner   filterrb   indexr   )r-   rH   r   r   	raw_valuestater   changeditemrQ   rU   target_optionslast_line_dictr]   r
   rl   itolnr   s                     @r   set_interface_optionru     s   	NEG$)XDW_gRWAW4XKX!(3 a*d2t<L7MQ_7_  a a ;!<uDE})+6M'v>N	~"G(_N/ue^]jlz{NGU6 E>3 <<tF#@.QRSVWW%7ueUcdfUgiv  yG  &HNGU. E>) ""%g.%7"G$22$6M6}eLD%23C%DNJtt):):=)IIAME#-dE65.#YE%L E> 
(	~!#<<ARW\`fWf5C%]cs7|W\G\c%] HM"G*/GB23FRGEGH E> %3 HM*/GB23FRGEGH
 E> 	^affgE>[ YaB &^G Hs8   	G-G-G-G2=G7G7
G<&G<=
HHc                 P   | dk(  rd}|D ]  }|j                  dd      dk(  s|j                  dd      |k(  s/||j                  di       j                  dd      k7  sU||j                  d      |k7  rld}t        j                  |j                  di       j                  dd      d	z   ||j                  d
            |d
<   ||d   d<    ||fS |d
   }	|	j                  |	j	                         d         }
|	j                  |	j	                         d         t        |	j	                         d         z   }|	d |
 }t        |      dk  r|dz  }|| d|z   |	|d  z   }t        ||| ||      }t        |      |d d d   j                  |      z
  }|j                  ||       d|fS )Nr=   Fr    r   r>   r   T$r
   r   r   r2   z     )
getr   subr   r   rfindr    r   rl   insert)r   r   r   rH   rr   rU   r   ro   rt   	last_liner`   suffix_startprefixr
   option_dictrl   s                   r   rj   rj   O  s    	/Bvvk2&'1bffWb6IU6RW\`b`f`fgoqs`t`x`x  zB  DF  aG  XG!-"&&9I2Jn2\VVBFF8R$8$<$<Xr$JS$PRWY[Y_Y_`fYgh6
).8X&	/ ~v&I>>)//"3A"67L??9??#4R#89C	@QRT@U<VVL}%F
=A&vu--	,-0HHDT5&%HKJtt**>::E	LL$;r   c                 <   t        j                         \  }}t        j                  |d      5 }|j	                  t        dj                  |      d             d d d        | j                  |t        j                  j                  |             y # 1 sw Y   9xY w)Nwbrw   surrogate_or_strict)errors)
tempfilemkstemposfdopenwriter   rY   atomic_movepathrealpath)r-   rH   desttmpfdtmpfiler/   s         r   write_changesr   l  sx    %%'NE7	5$	 H1	0EFGH
w 0 0 67H Hs   ,BBc                  V   t        t        t        dd      t        d      t        d      t        d      t        d      t        dd      t        ddd	dg
            ddt        d            } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }||dk(  r|| j                  d       t	        | |      \  }}	d}
|t        | ||||||      \  }
}|
r%t        | |D cg c]  }d|v s|d    c}      \  }}	|
rB| j                  s6|r| j                  |       t        | |D cg c]  }d|v s|d    c}|       | j                  ||
|	       y c c}w c c}w )Nr   z/etc/network/interfaces)typedefaultri   )r   boolFrd   rg   )r   r   choices)r   r   r   r   r   backuprn   T)r   )r   )argument_specadd_file_common_argssupports_check_moderequired_byr   r   r   r   r   r   rn   z=Value must be set if option is defined and state is 'present'rA   r
   )r   ro   rI   )r   dictr>   rF   r0   ru   r,   
check_modebackup_localr   	exit_json)r-   r   r   r   r   r   r   rn   rH   rI   ro   ddummys                r   mainr   t  s   6+DEE"U+U#E"VU3E9x>ST
 " 
F" == DMM'"E]]#34N]]8$FMM'"E]]8$FMM'"Eey0U]\](6ME6G-feUFESXZhi-f%6_QSY]^S^qy6_`vv((%f%GQ6Q;qyGN
$? 7`
 Hs   -	F!
7F!
3	F&
=F&
__main__)N)
__future__r   r   r   r   __metaclass__DOCUMENTATIONRETURNEXAMPLESr   r   r   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   r   r   r(   r0   r,   rR   rV   rb   ru   rj   r   r   __name__r   r   r   <module>r      s    A @9vD
L
 
 	  4 @2E/0
L^B?	1h:8,@^ zF r   