Login 木偶文件按特定顺序排列

Login 木偶文件按特定顺序排列,login,environment-variables,sh,puppet,Login,Environment Variables,Sh,Puppet,我正在尝试向/etc/profile添加三条系统强化线: TMOUT=43200 readonly TMOUT export TMOUT 当然,这些行需要按照这个特定的顺序排列,我还必须预料到文件的顺序是错误的。 我无法使用模板,因为有些主机具有无法更改的自定义配置文件。因此,我必须能够附加这三行 所以我在我的舱单上写了这个: file_line { 'TMOUT': path => '/etc/profile', ensure => present, li

我正在尝试向/etc/profile添加三条系统强化线:

TMOUT=43200
readonly TMOUT
export TMOUT
当然,这些行需要按照这个特定的顺序排列,我还必须预料到文件的顺序是错误的。 我无法使用模板,因为有些主机具有无法更改的自定义配置文件。因此,我必须能够附加这三行

所以我在我的舱单上写了这个:

file_line { 'TMOUT':
  path     => '/etc/profile',
  ensure   => present,
  line     => 'TMOUT=43200',
  multiple => true,
  before   => 'readonly TMOUT',
  match    => '^TMOUT',
}
file_line { 'readonly TMOUT':
  path     => '/etc/profile',
  ensure   => present,
  line     => 'readonly TMOUT',
  multiple => true,
  before   => 'export TMOUT',
  after    => '^TMOUT=43200',
  match    => '^readonly TMOUT',
}
file_line { 'export TMOUT':
  path     => '/etc/profile',
  ensure   => present,
  line     => 'export TMOUT',
  multiple => true,
  after    => '^readonly TMOUT',
  match    => '^export TMOUT',
}
Puppet以正确的顺序创建行,但当我编辑文件并更改顺序时,它在重新运行时不会得到纠正,例如

readonly TMOUT
export TMOUT
TMOUT=43200
我是不是犯了个错误,还是我必须换成其他的东西,比如Augeas

提前感谢

TL;博士 您基本上有三种选择:尝试创建有序依赖项(这不是真正的傀儡方式)、创建复合语句,或者如果您的发行版支持/etc/profile.d,则使用/etc/profile.d。虽然没有单一的“最佳”处理方法,但复合语句或声明可能是最简单的解决方案

有序依赖 除非明确声明依赖项,否则Puppet不能保证大多数操作的顺序。以下内容未经测试,但可能适用于您

file_line { 'set TMOUT':
  ensure => present,
  path   => '/etc/profile',
  line   => 'TMOUT=43200',
  match  => '^TMOUT',
} ->
file_line { 'export TMOUT':
  ensure => present,
  path   => '/etc/profile',
  line   => 'export TMOUT',
  after  => '^TMOUT=',
} ->
file_line { 'set TMOUT as readonly':
  ensure => present,
  path   => '/etc/profile',
  line   => 'readonly TMOUT',
  after  => '^export TMOUT',
}
这应该是你想要的,但它比需要的更脆弱。还有更稳健的选择

复合语句和变量声明 大多数(如果不是全部)与Bourne兼容的shell都应该支持复合语句,因此最好跳过对单个原子行操作的排序。例如:

file_line { 'profile TMOUT':
  ensure => present,
  path   => '/etc/profile',
  line   => 'TMOUT=43200; export TMOUT; readonly TMOUT',
  match  => '^TMOUT',
}
file_line { 'profile TMOUT':
  ensure => present,
  path   => '/etc/profile',
  line   => 'declare -r -x TMOUT=43200',
  match  => '^TMOUT',
}
更好的是,使用shell的declaresynax在一个操作中完成这一切!例如:

file_line { 'profile TMOUT':
  ensure => present,
  path   => '/etc/profile',
  line   => 'TMOUT=43200; export TMOUT; readonly TMOUT',
  match  => '^TMOUT',
}
file_line { 'profile TMOUT':
  ensure => present,
  path   => '/etc/profile',
  line   => 'declare -r -x TMOUT=43200',
  match  => '^TMOUT',
}
注意:readonly变量仍然可以在不直接设置readonly属性的子shell中分配给。这就是它的工作方式

将文件放入Profile.d中 在支持它的发行版上,使用/etc/profile.d几乎总是比在像/etc/profile这样的单一脚本中瞎搞更好的选择。它也不太可能被其他脚本、系统更新等破坏。然而,我在下面指出了一些注意事项

# Create your snippet in the /etc/profile.d directory.
file {'/etc/profile.d/tmout.sh':
  ensure => present,
  content => "TMOUT=43200\nexport TMOUT\nreadonly TMOUT",
} ->

# Remove the lines in /etc/profile if they exist.
file_line { 'profile TMOUT':
  ensure            => absent,
  path              => '/etc/profile',
  match             => 'TMOUT',
  match_for_absence => true,
  multiple          => true,
}
这里有一些需要注意的事项:

  • 存在一个微妙的竞争条件(即使使用依赖项链接),因为更改是连续的,而不是原子的。可能有一个小窗口,其中TMOUT在多个位置定义
  • 由于profile.d是特定于供应商的,因此除非配置它,否则它可能根本不受支持(甚至可能不存在)。检查您的分发文档
  • 同样,由于profile.d是特定于供应商的,profile.d中脚本的来源顺序可能会有所不同。它们通常来源于主/etc/profile脚本之后,存在多个设置相同值的脚本可能会使结果不确定
  • TL;博士 您基本上有三种选择:尝试创建有序依赖项(这不是真正的傀儡方式)、创建复合语句,或者如果您的发行版支持/etc/profile.d,则使用/etc/profile.d。虽然没有单一的“最佳”处理方法,但复合语句或声明可能是最简单的解决方案

    有序依赖 除非明确声明依赖项,否则Puppet不能保证大多数操作的顺序。以下内容未经测试,但可能适用于您

    file_line { 'set TMOUT':
      ensure => present,
      path   => '/etc/profile',
      line   => 'TMOUT=43200',
      match  => '^TMOUT',
    } ->
    file_line { 'export TMOUT':
      ensure => present,
      path   => '/etc/profile',
      line   => 'export TMOUT',
      after  => '^TMOUT=',
    } ->
    file_line { 'set TMOUT as readonly':
      ensure => present,
      path   => '/etc/profile',
      line   => 'readonly TMOUT',
      after  => '^export TMOUT',
    }
    
    这应该是你想要的,但它比需要的更脆弱。还有更稳健的选择

    复合语句和变量声明 大多数(如果不是全部)与Bourne兼容的shell都应该支持复合语句,因此最好跳过对单个原子行操作的排序。例如:

    file_line { 'profile TMOUT':
      ensure => present,
      path   => '/etc/profile',
      line   => 'TMOUT=43200; export TMOUT; readonly TMOUT',
      match  => '^TMOUT',
    }
    
    file_line { 'profile TMOUT':
      ensure => present,
      path   => '/etc/profile',
      line   => 'declare -r -x TMOUT=43200',
      match  => '^TMOUT',
    }
    
    更好的是,使用shell的declaresynax在一个操作中完成这一切!例如:

    file_line { 'profile TMOUT':
      ensure => present,
      path   => '/etc/profile',
      line   => 'TMOUT=43200; export TMOUT; readonly TMOUT',
      match  => '^TMOUT',
    }
    
    file_line { 'profile TMOUT':
      ensure => present,
      path   => '/etc/profile',
      line   => 'declare -r -x TMOUT=43200',
      match  => '^TMOUT',
    }
    
    注意:readonly变量仍然可以在不直接设置readonly属性的子shell中分配给。这就是它的工作方式

    将文件放入Profile.d中 在支持它的发行版上,使用/etc/profile.d几乎总是比在像/etc/profile这样的单一脚本中瞎搞更好的选择。它也不太可能被其他脚本、系统更新等破坏。然而,我在下面指出了一些注意事项

    # Create your snippet in the /etc/profile.d directory.
    file {'/etc/profile.d/tmout.sh':
      ensure => present,
      content => "TMOUT=43200\nexport TMOUT\nreadonly TMOUT",
    } ->
    
    # Remove the lines in /etc/profile if they exist.
    file_line { 'profile TMOUT':
      ensure            => absent,
      path              => '/etc/profile',
      match             => 'TMOUT',
      match_for_absence => true,
      multiple          => true,
    }
    
    这里有一些需要注意的事项:

  • 存在一个微妙的竞争条件(即使使用依赖项链接),因为更改是连续的,而不是原子的。可能有一个小窗口,其中TMOUT在多个位置定义
  • 因为profile.d是特定于供应商的,所以除非您进行配置,否则它可能根本不受支持(甚至可能不存在)。检查您的分发文档
  • 同样,由于profile.d是特定于供应商的,profile.d中脚本的来源顺序可能会有所不同。它们通常来源于主/etc/profile脚本之后,存在多个设置相同值的脚本可能会使结果不确定