Christian Meißner
Christian Meißner

Reputation: 243

ERB: iterate over an array and output each value as part of a one line string

I am trying to write a sudoers file by iterating over an array which comes from a puppet array.

Here is my puppet class. It defines an array oracle_homes

1 class oracle_home {
2   $oracle_homes = split($::oracle_homes, ',')
3   file { '/tmp/oracle_homes':
4     ensure  => file,
5     content => template('oracle/oracle.sudoer.erb'),
6   } 
7 }       
8 
9 require oracle_home

and here is the erb template.

 1 Defaults    env_keep += "ORACLE_BASE ORACLE_HOME TNS_ADMIN"
 2 
 3 <% @oracle_homes.each do |oracle_home| -%>
 4 Cmnd_Alias SQLPLUS = <%= oracle_home -%>/bin/sqlplus
 5 Cmnd_Alias SRVCTL  = <%= oracle_home -%>/bin/srvctl
 6 Cmnd_Alias VOTEDSK = <%= oracle_home -%>/bin/crsctl query css votedisk
 7 <% end -%>  
 8 
 9 Runas_Alias DB    = oracle, regdb
10 Runas_Alias GRID  = oracle, grid
11 
12 zabbix ALL = (GRID) NOPASSWD: VOTEDSK
13 zabbix ALL = (DB) NOPASSWD: SQLPLUS
14 zabbix ALL = (GRID) NOPASSWD: SRVCTL
15 
16 Defaults:zabbix !authenticate
17 Defaults:zabbix !syslog

From lines 3 to 6 I am trying to generate the Cmnd_Alias but the output is not what I need.

I get

 1  Defaults    env_keep += "ORACLE_BASE ORACLE_HOME TNS_ADMIN"
 2  
 3  Cmnd_Alias SQLPLUS = '/opt/oracle/regdb/product/12.1.0.2/dbhome_1'/bin/sqlplus
 4  Cmnd_Alias SRVCTL  = '/opt/oracle/regdb/product/12.1.0.2/dbhome_1'/bin/srvctl
 5  Cmnd_Alias VOTEDSK = '/opt/oracle/regdb/product/12.1.0.2/dbhome_1'/bin/crsctl query css votedisk
 6  Cmnd_Alias SQLPLUS = '/opt/oracle/regdb/product/11.2.0.4/dbhome_1'/bin/sqlplus
 7  Cmnd_Alias SRVCTL  = '/opt/oracle/regdb/product/11.2.0.4/dbhome_1'/bin/srvctl
 8  Cmnd_Alias VOTEDSK = '/opt/oracle/regdb/product/11.2.0.4/dbhome_1'/bin/crsctl query css votedisk
 9  
10  Runas_Alias DB    = oracle, regdb
11  Runas_Alias GRID  = oracle, grid
12  
13  zabbix ALL = (GRID) NOPASSWD: VOTEDSK
14  zabbix ALL = (DB) NOPASSWD: SQLPLUS
15  zabbix ALL = (GRID) NOPASSWD: SRVCTL
16  
17  Defaults:zabbix !authenticate
18  Defaults:zabbix !syslog

How can I change the iteration syntax to generate Cmnd_Alias lines like

Cmnd_Alias SQLPLUS = /opt/oracle/regdb/product/12.1.0.2/dbhome_1/bin/sqlplus, /opt/oracle/regdb/product/11.2.0.4/dbhome_1/bin/sqlplus
Cmnd_Alias VOTEDSK = /opt/oracle/regdb/product/12.1.0.2/dbhome_1/bin/crsctl query css votedisk, /opt/oracle/regdb/product/11.2.0.4/dbhome_1/bin/crsctl query css votedisk

Upvotes: 2

Views: 1724

Answers (2)

Alkesh
Alkesh

Reputation: 39

sql_plus = []

srv_ctl = []

vote_desk = []

<% @oracle_homes.each do |oracle_home| -%>

    sql_plus << "#{oracle_home}/bin/sqlplus"

    srv_ctl << "#{oracle_home}/bin/srvctl"

    vote_desk << "#{oracle_home}/bin/crsctl query css votedisk"  
<% end -%>

Cmnd_Alias SQLPLUS = sql_plus.join(', ')

Cmnd_Alias SRVCTL  = srv_ctl.join(', ')

Cmnd_Alias VOTEDSK = vote_desk.join(', ')

Upvotes: 1

spickermann
spickermann

Reputation: 106792

I would try to replace

3 <% @oracle_homes.each do |oracle_home| -%>
4 Cmnd_Alias SQLPLUS = <%= oracle_home -%>/bin/sqlplus
5 Cmnd_Alias SRVCTL  = <%= oracle_home -%>/bin/srvctl
6 Cmnd_Alias VOTEDSK = <%= oracle_home -%>/bin/crsctl query css votedisk
7 <% end -%>  

with

Cmnd_Alias SQLPLUS = <%= @oracle_homes.map { |path| "#{path}/bin/sqlplus" }.join(', ') %>
Cmnd_Alias SRVCTL  = <%= @oracle_homes.map { |path| "#{path}/bin/srvctl" }.join(', ') %>
Cmnd_Alias VOTEDSK = <%= @oracle_homes.map { |path| "#{path}/bin/crsctl query css votedisk" }.join(', ') %>

Upvotes: 4

Related Questions