# Change agent_disabled.lock location

John Simpson `<jms1@jms1.net>` 2019-05-23

Last updated 2019-07-28

# Background

Used [`laura/puppet_agent_settings`](https://forge.puppet.com/laura/puppet_agent_settings) (specifically the [custom fact](https://github.com/LMacchi/puppet_agent_settings/blob/master/lib/facter/puppet_agent_settings.rb) within the module) as a guide for this.

# Procedure

## `util/lib/facter/agent_settings.rb`

This creates an `agent_settings` custom fact (within the `util` module) whose value is a hash containing selected values from the agent's `Puppet.settings` hash. In this case, it allows server-side code to find out the agent's `confdir` and `config` settings, so that it can declare the `agent_disabled_lockfile` setting in the correct file, and with the correct value.

```ruby
Facter.add( 'agent_settings' ) do
  setcode do
    ########################################
    # Which settings need to be gathered

    keys = ['config','confdir','ssldir']

    ########################################
    # Gather the info

    rv = {}
    keys.each do |k|
      rv[k] = Puppet.settings[k]
    end

    ########################################
    # Return what we found

    rv
  end
end
```

## `util/manifests.init.pp`

Adding the following code to the `util` class will make it declare the `agent_disabled_lockfile` setting in the correct file, with the correct value.

```puppet
class util {

  if ( has_key( $facts , 'agent_settings' ) ) {
    if ( has_key( $facts['agent_settings'] , 'confdir' ) ) {
      $etc_puppet = $facts['agent_settings']['confdir']
    } else {
      fail( 'agent_settings does not contain confdir' )
    }
    if ( has_key( $facts['agent_settings'] , 'config' ) ) {
      $puppet_conf = $facts['agent_settings']['config']
    } else {
      fail( 'agent_settings does not contain config' )
    }
  } else {
    fail( 'agent_settings does not exist' )
  }

  ########################################
  # For new stuff (PE 2019.1.0)
  #
  #  ini_setting { 'agent_disabled_lockfile':
  #    ensure  => present ,
  #    path    => $puppet_conf ,
  #    section => 'agent' ,
  #    setting => 'agent_disabled_lockfile' ,
  #    value   => "${etc_puppet}/agent_disabled.lock" ,
  #  }

  ########################################
  # For old stuff (PE 2016.2.0)
  # the "match" function doesn't seem to be working?

  file_line { 'agent_disabled.lock' :
    ensure => present ,
    path   => $puppet_conf ,
    after  => '^\[agent\]' ,
    match  => '^[#\s]+agent_diabled_lockfile\s*=' ,
    line   => "agent_disabled_lockfile = ${etc_puppet}/agent_disabled.lock" ,
  }

}
```
