@@ -180,6 +180,10 @@ value: :code:`Setting={{ varname1 }}`
180180 - {{{ config_dir }}}
181181 contains: {{{ line_regex }}}
182182 register: _config_dir_has_parameter
183+ - name: {{{ rule_title }}} - Check if {{{ config_file }}} exists
184+ ansible.builtin.stat:
185+ path: {{{ config_file }}}
186+ register: _config_file_exists
183187- name: {{{ rule_title }}} - Check if the parameter {{{ parameter }}} is configured correctly in {{{ config_file }}}
184188 ansible.builtin.lineinfile:
185189 path: {{{ config_file }}}
@@ -188,6 +192,7 @@ value: :code:`Setting={{ varname1 }}`
188192 check_mode: true
189193 changed_when: false
190194 register: _config_file_correctly
195+ when: _config_file_exists.stat.exists
191196- name: {{{ rule_title }}} - Check if the parameter {{{ parameter }}} is configured correctly in {{{ config_dir }}}
192197 ansible.builtin.find:
193198 paths:
@@ -201,7 +206,9 @@ value: :code:`Setting={{ varname1 }}`
201206 {{{ ansible_find("Check if the parameter " + parameter + " is present in " + config_dir, paths=config_dir, contains=line_regex, register=dir_parameter, when=find_when)|indent }}}
202207 {{{ ansible_lineinfile("Remove parameter from files in " + config_dir, path="{{ item.path }}", regex=line_regex, insensitive=insensitive, state="absent", with_items=lineinfile_items, when=lineinfile_when)|indent }}}
203208 {{{ ansible_lineinfile("Insert correct line to " + set_file, set_file, regex=line_regex, insensitive=insensitive, new_line=new_line, create=create, state='present', validate=validate, insert_after=insert_after, insert_before=insert_before)|indent }}}
204- when: (_config_file_correctly.found == 0 and _config_dir_correctly.matched == 0) or ((_config_file_has_parameter.found | int) + (_config_dir_has_parameter.matched | int)) != 1
209+ when:
210+ - _config_file_correctly is not skipped
211+ - (_config_file_correctly.found == 0 and _config_dir_correctly.matched == 0) or ((_config_file_has_parameter.found | int) + (_config_dir_has_parameter.matched | int)) != 1
205212{{%- endmacro %}}
206213
207214
@@ -2424,124 +2431,6 @@ lines will be inserted at the beginning of the profile.
24242431 when: dconf_user_profile_blockinfile is changed
24252432{{%- endmacro -%}}
24262433
2427-
2428- {{#
2429-
2430- Set a sshd configuration parameter to a value for system with /usr - located default config
2431-
2432- :parameter msg: Message to be set as Task Title, if not set the rule's title will be used instead
2433- :type msg: str
2434- :parameter parameter: Parameter to set
2435- :type parameter: str
2436- :parameter value: The value to set
2437- :type value: str
2438- :param copy_defaults: If true default sshd configuration in /usr/etc/ssh/sshd_config will be
2439- copied onto /etc/ssh/sshd_config, if /etc/ssh/sshd_config does not exist
2440- :type copy_defaults: bool
2441- :parameter config_basename: drop-in filename of sshd configuration file
2442- :type config_basename: str
2443-
2444- #}}
2445- {{%- macro ansible_sshd_set_usr(msg='', parameter='', value='', copy_defaults=true, config_basename="00-complianceascode-hardening.conf", rule_title=None) %}}
2446- {{%- set sshd_config_path = "/etc/ssh/sshd_config" %}}
2447- {{%- set sshd_usr_config_path = "/usr/etc/ssh/sshd_config" %}}
2448- {{%- set sshd_config_dir = "/etc/ssh/sshd_config.d" -%}}
2449- {{%- set sshd_usr_config_dir = "/usr/etc/ssh/sshd_config.d" -%}}
2450- {{%- set ssh_paths = ['/etc/ssh/sshd_config.d', '/usr/etc/ssh/sshd_config.d'] -%}}
2451- {{%- set config_file = "/etc/ssh/sshd_config.d/" ~ config_basename -%}}
2452- {{%- set new_line = parameter + ' ' + value -%}}
2453- {{%- set line_regex = "(?i)^\s*" + "{{ \"" + parameter + "\"| regex_escape }}" + "\s+" -%}}
2454- {{%- set dir_parameter = "sshd_config_d_has_parameter" -%}}
2455- {{%- set lineinfile_items = "{{ " + dir_parameter + ".files }}" -%}}
2456-
2457- - name: {{{ rule_title }}} - Copy default {{{ sshd_usr_config_path }}} to {{{ sshd_config_path }}}
2458- ansible.builtin.copy:
2459- src: {{{ sshd_usr_config_path }}}
2460- dest: {{{ sshd_config_path }}}
2461- force: no
2462- mode: '0600'
2463- - name: {{{ rule_title }}} - Check if the parameter {{{ parameter }}} is configured in sshd configuration(s)
2464- ansible.builtin.find:
2465- paths:
2466- - '/etc/ssh'
2467- - '/usr/etc/ssh'
2468- - {{{ sshd_config_dir }}}
2469- - {{{ sshd_usr_config_dir }}}
2470- contains: {{{ line_regex }}}
2471- patterns:
2472- - '*.conf'
2473- - 'sshd_config'
2474- register: _sshd_config_has_parameter
2475- - name: {{{ rule_title }}} - Check if the parameter {{{ parameter }}} is configured correctly in sshd configuration(s)
2476- ansible.builtin.find:
2477- paths:
2478- - '/etc/ssh'
2479- - '/usr/etc/ssh'
2480- - {{{ sshd_config_dir }}}
2481- - {{{ sshd_usr_config_dir }}}
2482- contains: {{{ line_regex ~ value ~ "$" }}}
2483- patterns:
2484- - '*.conf'
2485- - 'sshd_config'
2486- register: _sshd_config_correctly
2487- - name: '{{{ msg or rule_title }}}'
2488- block:
2489- {{{ ansible_lineinfile(
2490- "Deduplicate values from " + sshd_config_path,
2491- sshd_config_path,
2492- regex=line_regex,
2493- insensitive='false',
2494- create='no',
2495- state='absent')|indent }}}
2496- {{{ ansible_lineinfile(
2497- "Deduplicate values from " + sshd_usr_config_path,
2498- sshd_usr_config_path,
2499- regex=line_regex,
2500- insensitive='false',
2501- create='no',
2502- state='absent')|indent }}}
2503- - name: "{{{ rule_title }}} - Check if the parameter {{{ parameter }}} is present in {{{ sshd_config_dir }}} and in {{{ sshd_usr_config_dir }}}"
2504- ansible.builtin.find:
2505- paths: {{{ ssh_paths }}}
2506- recurse: 'yes'
2507- follow: 'no'
2508- contains: '(?i)^\s*{{ "{{{ parameter }}}"| regex_escape }}\s+'
2509- register: {{{ dir_parameter }}}
2510- {{{ ansible_lineinfile(
2511- "Remove parameter from files in " + sshd_config_dir,
2512- path="{{ item.path }}",
2513- regex=line_regex,
2514- state="absent",
2515- with_items=lineinfile_items)|indent}}}
2516- {{{ ansible_lineinfile(
2517- "Remove parameter from files in " + sshd_usr_config_dir,
2518- path="{{ item.path }}",
2519- regex=line_regex,
2520- state="absent",
2521- with_items=lineinfile_items)|indent }}}
2522- {{{ ansible_lineinfile(
2523- "Insert correct line to " + config_file,
2524- config_file,
2525- regex=line_regex,
2526- insensitive='false',
2527- new_line=new_line,
2528- create='yes',
2529- state='present',
2530- validate='/usr/sbin/sshd -t -f %s',
2531- insert_after='',
2532- insert_before="BOF" )|indent }}}
2533- when: _sshd_config_correctly.matched == 0 or _sshd_config_has_parameter.matched != 1
2534-
2535- - name: {{{ rule_title }}} - set file mode for {{{ config_file }}}
2536- ansible.builtin.file:
2537- path: {{{ config_file }}}
2538- mode: '0600'
2539- state: touch
2540- modification_time: preserve
2541- access_time: preserve
2542- {{%- endmacro %}}
2543-
2544-
25452434{{#
25462435 copy source file to destination file if destination
25472436 does not exist
0 commit comments