
    Vh                     r    d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlZ G d de      Zd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functiona  
author:
  - David Lundgren (@dlundgren)
module: sysrc
short_description: Manage FreeBSD using sysrc
version_added: '2.0.0'
description:
  - Manages C(/etc/rc.conf) for FreeBSD.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    description:
      - Name of variable in C(/etc/rc.conf) to manage.
    type: str
    required: true
  value:
    description:
      - The value to set when O(state=present).
      - The value to add when O(state=value_present).
      - The value to remove when O(state=value_absent).
    type: str
  state:
    description:
      - Use V(present) to add the variable.
      - Use V(absent) to remove the variable.
      - Use V(value_present) to add the value to the existing variable.
      - Use V(value_absent) to remove the value from the existing variable.
    type: str
    default: "present"
    choices: [absent, present, value_present, value_absent]
  path:
    description:
      - Path to file to use instead of V(/etc/rc.conf).
    type: str
    default: "/etc/rc.conf"
  delim:
    description:
      - Delimiter to be used instead of V(" ") (space).
      - Only used when O(state=value_present) or O(state=value_absent).
    default: " "
    type: str
  jail:
    description:
      - Name or ID of the jail to operate on.
    type: str
notes:
  - The O(name) cannot contain periods as sysrc does not support OID style names.
a  
# enable mysql in the /etc/rc.conf
- name: Configure mysql pid file
  community.general.sysrc:
    name: mysql_pidfile
    value: "/var/run/mysqld/mysqld.pid"

# enable accf_http kld in the boot loader
- name: Enable accf_http kld
  community.general.sysrc:
    name: accf_http_load
    state: present
    value: "YES"
    path: /boot/loader.conf

# add gif0 to cloned_interfaces
- name: Add gif0 interface
  community.general.sysrc:
    name: cloned_interfaces
    state: value_present
    value: "gif0"

# enable nginx on a jail
- name: Enable nginx in test jail
  community.general.sysrc:
    name: nginx_enable
    value: "YES"
    jail: testjail
zj
changed:
  description: Return changed for sysrc actions.
  returned: always
  type: bool
  sample: true
)AnsibleModuleNc                   B    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)Sysrcc                     || _         || _        d| _        || _        || _        || _        || _        |j                  dd      | _        y )NFsysrcT)	modulenamechangedvaluepathdelimjailget_bin_pathr
   )selfr   r   r   r   r   r   s          k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/sysrc.py__init__zSysrc.__init__n   sG    	
	
	(($7
    c                 V    |j                  d      dkD  xs |j                  d      dkD  S )Nzunknown variabler   )find)r   outerrs      r   has_unknown_variablezSysrc.has_unknown_variablex   s,    xx*+a/S388<N3ORS3SSr   c           	      8   | j                  dd      \  }}}t        |j                         D cg c]  }|j                  dd       c}      }| j                  | j
                  |v S | j
                  |v xr || j
                     d| j                  z  k(  S c c}w )as  
        Tests whether the name is in the file.  If parameter value is defined,
        then tests whether name=value is in the file.  These tests are necessary
        because sysrc doesn't use exit codes.  Instead, let sysrc read the
        file's content and create a dictionary comprising the configuration.
        Use this dictionary to preform the tests.
        z-ez-a=   z"%s")	run_sysrcdict
splitlinessplitr   r   )r   rcr   r   iconfs         r   existszSysrc.exists|   s     d3S#cnn.>?QWWS!_?@::99$$99$OdiiFTZZ<O)OO	 @s   Bc                     | j                  d| j                        \  }}}| j                  ||      ry| j                  |j	                         j                  | j                        v S )Nz-nF)r   r   r   r   stripr"   r   r   r#   r   r   s       r   containszSysrc.contains   sS    dii8S#$$S#.zzSYY[..tzz:::r   c                     | j                         ry | j                  j                  s.| j                  | j                  d| j
                        \  }}}d| _        y )Nr   T)r&   r   
check_moder   r   r   r   r)   s       r   presentzSysrc.present   sC    ;;={{%%!^^tyy$**,MNNRcr   c                     | j                         sy | j                  j                  s3| j                  d| j                        \  }}}| j                  ||      ry d| _        y )Nz-xT)r&   r   r,   r   r   r   r   r)   s       r   absentzSysrc.absent   sQ    {{} {{%%!^^D$))<NRc((c2r   c                    | j                         ry | j                  j                  rd| _        y | j                  d| j
                  | j                  }| j                  |      \  }}}|j                  d| j                  z        dk(  rR|j                  d      d   j                         j                  | j
                        }| j                  |v rd| _        y y y )NTz+=%s:r    -> r   r*   r   r,   r   r   r   r   r   r   r"   r(   r   	setstringr#   r   r   valuess         r   value_presentzSysrc.value_present   s    ==?;;!!DL"&))TZZD		2S#88EDII%&!+YYv&q)//177

CFzzV## $ ,r   c                    | j                         sy | j                  j                  rd| _        y | j                  d| j
                  | j                  }| j                  |      \  }}}|j                  d| j                  z        dk(  rR|j                  d      d   j                         j                  | j
                        }| j                  |vrd| _        y y y )NTz-=r1   r   r2   r   r3   r4   s         r   value_absentzSysrc.value_absent   s    }};;!!DL"&))TZZD		2S#88EDII%&!+YYv&q)//177

CFzz'# ( ,r   c                     | j                   d| j                  g}| j                  r|d| j                  gz  }|j                  |       | j                  j                  |      \  }}}|||fS )Nz-fz-j)r
   r   r   extendr   run_command)r   argscmdr#   r   r   s         r   r   zSysrc.run_sysrc   sb    zz4+99D$))$$C

4005S#C~r   N)__name__
__module____qualname__r   r   r&   r*   r-   r/   r7   r9   r    r   r   r   r   m   s1    8TP;
$$r   r   c                     t        t        t        dd      t        dd       t        ddg d      t        dd      t        dd	      t        dd       
      d      } | j                  j                  d      }t	        j
                  d|      s| j                  d       | j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }t        ||||||      }t        | |||||      }|dk(  r|j                          nA|dk(  r|j                          n+|dk(  r|j                          n|dk(  r|j                          |j                  |d<    | j                  di | y )NstrT)typerequired)rE   defaultr-   )r/   r-   r7   r9   )rE   rG   choicesz/etc/rc.conf )r   r   stater   r   r   )argument_specsupports_check_moder   z^[a-zA-Z0-9_]+$z<Name may only contain alphanumeric and underscore characters)msgr   rJ   r   r   r   )r   rJ   r   r   r   r   r/   r7   r9   r   rB   )r   r    paramspoprematch	fail_jsonr   r-   r/   r7   r9   r   	exit_json)	r   r   r   rJ   r   r   r   resultrc_values	            r   mainrV      s   540E40E9>tu5.9E3/5$/
 !
F ==V$D88%t,N 	 	
 MMg&EMMg&E==V$DMMg&E==V$DF VT5$t<H		(		/	! 	.	  ((F9Fvr   __main__)
__future__r   r   r   rE   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   rP   objectr   rV   r?   rB   r   r   <module>r_      sZ    A @5n<
 5 	`F `F/d zF r   