I have a space separated string of IPs that I am exporting to a Vagrantfile which I want to iterate over.
export IP="10.10.10.10 10.10.10.11"
I want to perform an operation on this so it becomes a list in the vagrantfile to iterate over.
["10.10.10.10", "10.10.10.11"]
What is the way to do this?
Try figure it out yourself in bash:
$ export IP="10.10.10.10 10.10.10.11"
$ irb # interactive ruby
> puts ENV['IP'] # make sure IP is not nil
10.10.10.10 10.10.10.11 # output
> IPs = ENV['IP'].split
> puts IPs
Vagrantfile is a Ruby script, so you can use ENV['IP'].split in it
The following should be robust. You would not need to worry about padding by space characters at the beginning or the end of the string, or about irregular sequences of space characters.
"10.10.10.10 10.10.10.11".scan(/\S+/)
# => ["10.10.10.10", "10.10.10.11"]
You can just use Split directly as from the examples,
" now's the time".split
=> ["now's", "the", "time"]
>> "10.10.10.10 10.10.10.11".split
=> ["10.10.10.10", "10.10.10.11"]
>> "10.10.10.10 10.10.10.11".split
=> ["10.10.10.10", "10.10.10.11"]
>> " 10.10.10.10 10.10.10.11".split
=> ["10.10.10.10", "10.10.10.11"]
>> "".split
=> []
Read the Docs here
To include variables in vagrant file, Refer this
Use split in a simple way
"10.10.10.10 10.10.10.11".split(' ')
=> ["10.10.10.10", "10.10.10.11"]
Related
I want an output like this: ["# foo\n\n1234\n\n", "# bar\n\nk23j4k2j4\nk23j4kj4\n\n", "# baz\n\nk2k2k2\n\n"] with scan method from following text content.
content = '
# foo
1234
# bar
k23j4k2j4
k23j4kj4
# baz
k2k2k2
'
I tried the following code. But it didn't work as I think.
p content.scan(/^#.*/m)
# => ["# foo\n\n1234\n\n# bar\n\nk23j4k2j4\nk23j4kj4\n\n# baz\n\nk2k2k2\n\n"]
How do you solve this?
note: Alternatively You can use split or other methods instead of scan.
. matchs all strings including #.
You have to exclude it.
content.scan(/^#[^#]*/m)
# => ["# foo\n\n1234\n\n", "# bar\n\nk23j4k2j4\nk23j4kj4\n\n", "# baz\n\nk2k2k2\n\n"]
try
content.scan(/#[^#]*/)
it matches # and anything else until you find # again
=> ["# foo\n\n1234\n\n", "# bar\n\nk23j4k2j4\nk23j4kj4\n\n", "# baz\n\nk2k2k2\n\n"]
I have two Ruby files:
db/migrate/20120920180336_create_admin_pages.rb
db/migrate/20120920150716_devise_create_users.rb
There's a constant variable in this file: _create_admin_pages.rb
How can I find it in a file and make the following command work right?
gsub_file "db/migrate/*_create_admin_pages.rb", "t.boolean :published", "t.boolean :published, :default => false"
ruby -pe 'gsub("t.boolean :published", "t.boolean :published, :default => false")' < db/migrate/*_create_admin_pages.rb
Ruby can run with several options. Here the -pe option (actually two options) let Ruby execute this loop:
Until no more input:
read a line of input,
run the string after -pe as code,
print the result.
All using the implicit variable $_, the last read line.
You can try this:
gsub_file Dir['db/migrate/*_create_admin_pages.rb'].first, "t.boolean :published", "t.boolean :published, :default => false"
In puppet, if define command is > 80 characters, how can I wrap into two line to do it?
exec { 'create_domain':
command => "some command exceed 80 character...........................................................how to do how to do?.......",
}
It's sort of ugly, but if the last character in a string is a \ followed by a newline, then the string is continued on the next line. My sample.pp manifest is below:
exec { 'wrapped_string_example':
command => "/bin/echo 12345678901234567890123456789012345678901234567890\
wrapped > /var/tmp/test.txt";
}
Running this with puppet apply sample.pp gives the following output
$ puppet apply sample.pp
notice: /Stage[main]/Exec[wrapped_string_example]/returns: executed successfully
notice: Finished catalog run in 0.10 seconds
And catting the created file shows the lines have wrapped:
$ cat /var/tmp/test.txt
12345678901234567890123456789012345678901234567890wrapped
See https://github.com/puppetlabs/puppet/blob/9fbb36de/lib/puppet/parser/lexer.rb#L537 (as of Puppet v2.7.0)
Also this is sort of a known issue: http://projects.puppetlabs.com/issues/5022
For big chunks of data, heredocs are the best way of dealing with long lines in Puppet manifests. The /L interpolation option is particularly useful. /L causes \ at the end of a line to remove newlines. For example, the following does what you'd expect, stripping indentation and newlines, including the trailing newline.
sshkey { 'example.com':
ensure => present,
type => 'ssh-rsa',
key => #(KEY/L),
RfrXBrU1T6qMNllnhXsJdaud9yBgWWm6OprdEQ3rpkTvCc9kJKH0k8MNfKxeBiGZVsUn435q\
e83opnamtGBz17gUOrzjfmpRuBaDDGmGGTPcO8Dohwz1zYuir93bJmxkNldjogbjAWPfrX10\
8aoDw26K12sK61lOt6GTdR9yjDPdG4zL5G3ZjXCuDyQ6mzcNHdAPPFRQdlRRyCtG2sQWpWan\
3AlYe6h6bG48thlo6vyNvOD8s9K0YBnwl596DJiNCY6EsxnSAhA3Uf9jeKqlVqqrxhEzHufx\
07iP1nXIXCMUV
|-KEY
target => '/home/user/.ssh/authorized_keys',
}
Or to keep the final newline, leave out the - before the end text:
exec { 'create_domain':
command => #(CMD/L),
/bin/echo 123456789012345678901234567890123456789012345678901234567890123456\
wrapped > /var/tmp/test.txt
| CMD
}
As of Puppet 3.5 you have a couple of options that i have used. Ruby allows you to concat strings over a couple of lines.
string = "line #1"\
"line #2"\
"line #3"
p string # => "line #1line #2line #3"
Another option, as of Puppet 3.5 they have added HereDoc functionality. This will allow you to put the string in a section of a source code file that is treated as if it were a separate file.
$mytext = #(EOT)
This block of text is
visibly separated from
everything around it.
| EOT
The puppet documentation is here: https://docs.puppet.com/puppet/4.9/lang_data_string.html#heredocs
If you really care about the 80cols limit you can always abuse a template to achieve that goal
exec {'VeryLongExec':
command => template("${module}/verylongexec")
}
Then put the actual command in that template file
Credits should go to Jan Vansteenkiste to figure
I try to modify "/foo/bar/dir" to "\/foo\/bar\/dir" by ruby gsub command.
I test it in irb the result is
x = "/foo/bar/dir"
x.gsub("/","\/")
=> "/foo/bar/dir"
x.gsub("/","\\/")
=> "\\/foo\\/bar\\/dir"
Is it possible to replace "/" with "/" by gsub ?
Source of problems:
I try to execute "string in command line" and "real_path" is my variable
real_path = "/home/me/www/idata"
path = real_path.gsub("/","\\/")
=> \\/home\\/me\\/www\\/idata
# But what I expect is \/home\/me\/www\/idata
run "sed 's/SHARE_PATH/#{path}/g' #{path}/config/sphinx.yml > #{path}/config/sphinx.tmp.yml"
result from "run" command is
"sh -c 'sed '\''s/SHARE_PATH/\\/home\\/me\\/www\\/idata\\/shared/g .... "
I need is only one back slash like
"sh -c 'sed '\''s/SHARE_PATH/\/home\/me\/www\/idata\/shared/g .... "
"run" is command from Capistrano
my solution is
use single quote instead of double quote like this
path = real_path.gsub("/",'\/')
You have written:
x = "/foo/bar/dir"
x.gsub("/","\\/")
=> "\\/foo\\/bar\\/dir"
so You did what You had asked before. x.gsub("/","\\/") in fact evaluates to "\/foo\/bar\/dir" but irb prints return value of inspect method instead of to_s.
Edit: Did You mean
real_path.gsub("/","\/")
istead of
real_path.gsub("\/","\/")
Anyway the output is correct - You changed / with \/ so You have
"sh -c 'sed '\''s/SHARE_PATH/\/home\/me\/www\/idata\/shared/g'\'' .... "`
instead of
`"sh -c 'sed '\''s/SHARE_PATH//home/me/www/idata/shared/g'\'' .... "`
and result is different from irb's result (notice the lack of doubled backslash).
For path manipulation I recommend using File.join (documentation)
By the way: why are You modifying the path this way? (1)
Edit2: Why are You asking about changing "/" to "/" but write the following line?
path = real_path.gsub("\/","\\/")
What are You trying to achieve? And what is Your answer to question (1) ?
Edit3:
Here We go:
>> real_path = "/foo/bar/dir"
=> "/foo/bar/dir"
>> path = real_path.gsub("/", "\\/")
=> "\\/foo\\/bar\\/dir"
>> puts "sed 's/SHARE_PATH/#{path}/g' #{path}/config/sphinx.yml > #{path}/config/sphinx.tmp.yml"
sed 's/SHARE_PATH/\/foo\/bar\/dir/g' \/foo\/bar\/dir/config/sphinx.yml > \/foo\/bar\/dir/config/sphinx.tmp.yml
=> nil
>>
but I do not understand why You need backslash in a path?
Yes
irb(main):028:0> (t = x.gsub("/", "\\/")) && nil
=> nil
irb(main):029:0> t
=> "\\/foo\\/bar\\/dir"
irb(main):030:0> puts t
\/foo\/bar\/dir
=> nil
Your first example actually did what you wanted, but the .inspect method that irb is using is escaping backslashes, so it looked like there were extras. If you had used puts you would have seen the real result.
Ruby is preinstalled on my Mac and so I wanted to have a look at it. First thing I noticed, is that irb prompts >> instead of irb(main):001:0>. I can't find anything on how to change this with Google because everyone is using irb(main):001:0> in their code ;-)
Can you help me out?
PS: It's not that I think Ruby is broken, but I want to look more nerdy while programming ;-)
$ irb --help
Usage: irb.rb [options] [programfile] [arguments]
--prompt prompt-mode
--prompt-mode prompt-mode
Switch prompt mode. Pre-defined prompt modes are
`default', `simple', `xmp' and `inf-ruby'
$ irb --prompt inf-ruby
irb(main):001:0>
What I do is make that into an alias in my .bashrc so I don't have to type it every time.
echo alias irb=\'irb --prompt inf-ruby\' >> ~/.bashrc
Hope this helps!
goto the location /home/leapfrog/.rvm/scripts
cd ~/.rvm/scripts
Open the file ‘irbrc.rb’, use superuser power to over-write the
$ sudo gedit irbrc.rb
Change the content of the hash '#prompt' to the following
#prompt = {
:PROMPT_I => "#{rvm_ruby_string} :%03n > ", # default prompt
:PROMPT_S => "#{rvm_ruby_string} :%03n%l> ", # known continuation
:PROMPT_C => "#{rvm_ruby_string} :%03n > ",
:PROMPT_N => "#{rvm_ruby_string} :%03n?> ", # unknown continuation
:RETURN => " => %s \n",
:AUTO_INDENT => true
}
Hope this help you :)