# Bash output to variable

**URL:** <https://discourse.chef.io/t/bash-output-to-variable/11835>\
**Category:** Chef Infra (archive)\
**Created:** [October 29, 2017, 11:09am UTC](https://discourse.chef.io/t/bash-output-to-variable/11835 "2017-10-29T11:09:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![genache](https://avatars.discourse-cdn.com/v4/letter/g/0ea827/32.png) [@genache](https://discourse.chef.io/u/genache)\
**Post date:** [October 29, 2017, 11:09am UTC](https://discourse.chef.io/t/bash-output-to-variable/11835/1 "2017-10-29T11:09:57Z")

</div>

I need to store the IP which is the result of a bash resource into a variable that I can use later.

My code looks like this:

```
sentinel_ip=" "
sentinel_hostname=" "
sentinel_machine = search(:node, "role:redis AND chef_environment:#{chef_env} AND team:#{team} AND node_type:sentinel")
sentinel_machine.each do |nodeobj_sentinel|
  sentinel_ip << nodeobj_sentinel['ipaddress']
  sentinel_hostname << nodeobj_sentinel.name
end

execute 'get master' do
  command = "redis-cli -h #{sentinel_hostname} -p 26379 info | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' "
  action :run
# I'll store the result of the above command in master_ip variable
  master_ip << node['ipaddress']
end

#here I need to use the master_ip to connect to it
bash 'update config in runtime' do
  code <<-EOH
redis-cli -h #{master_ip} -p #{defaultport} -a #{auth} config set #{runtime_config}
redis-cli -h #{master_ip} -p #{defaultport} -a #{auth} config rewrite
  EOH
  not_if { runtime_config.empty?}
end

```

Basically, if I get any master\_ip from “get\_master” execute resource, I pass it to the bash block below to use it, in order to update redis at runtime.

When I get something in master\_ip, I’ll continue the rest of my recipe.

Anyway, the result of running ‘get master’ execute block above ends with

```
Compiling Cookbooks...
[2017-10-29T10:26:58+00:00] WARN: nodeobj_sentinel: node[clj-lc-tstrds03]
[2017-10-29T10:26:58+00:00] WARN: nodeobj_sentinel ip: 10.111.200.25
[2017-10-29T10:26:58+00:00] WARN: nodeobj_sentinel name: clj-lc-tstrds03
[2017-10-29T10:26:58+00:00] WARN: Sentinel is clj-lc-tstrds03
[2017-10-29T10:26:59+00:00] WARN: master ip is {}

```

So the master\_ip is empty.  
Any ideea how to do this better?  
Thank you!  
Gabi

P.S. running everything by hand in the machine, i get the IP.

---

<div class="post-metadata">

**Author:** ![cheeseplus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/cheeseplus/32/960_2.png) [@cheeseplus](https://discourse.chef.io/u/cheeseplus)\
**Post date:** [November 2, 2017, 11:56am UTC](https://discourse.chef.io/t/bash-output-to-variable/11835/2 "2017-11-02T11:56:43Z")

</div>

There isn’t really such a thing as a “return value” from a resource. From the looks of things you probably want to restructure this to be a Custom Resource[1](https://docs.chef.io/custom_resources.html) or write a library helper to do the shelling out for you. In short, don’t use execute for this as that’s getting a value that really has no place being a resource in your run.

---

<div class="post-metadata">

**Author:** ![genache](https://avatars.discourse-cdn.com/v4/letter/g/0ea827/32.png) [@genache](https://discourse.chef.io/u/genache)\
**Post date:** [November 3, 2017, 2:26pm UTC](https://discourse.chef.io/t/bash-output-to-variable/11835/3 "2017-11-03T14:26:21Z")

</div>

I’ve done it like this:

```
ruby_block "get master" do
    block do
      Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
      command1 = "redis-cli -h #{sentinel_ip} -p 26379 info | grep -o '[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}' && redis-cli -h #{sentinel_ip} -p #{defaultport} -a #{auth} config set #{runtime_config} && redis-cli -h #{sentinel_ip} -p #{defaultport} -a #{auth} config rewrite"
      command_out = shell_out(command1)
      node.run_state['master_ip'] = command_out1.stdout
    end
    action :create
  end

  template "redis.conf" do
    path "/etc/redis.conf"
    source "redis.conf.erb"
    owner node['redis']['user']
    group node['redis']['group']
    mode "0600"
    variables(
        :node_type => node_type,
        :masterport => node['redis']['defaultport'],
        :masterip => lazy { node.run_state['master_ip'].chomp },
        :maxmemory => node['redis']['maxmemory'],
        :maxmemory_policy => node['redis']['maxmemory_policy'],
        :secret => auth,
        :runtime_config => runtime_config
    )
  end

```

Gabi

---

<div class="post-metadata">

**Author:** ![cheeseplus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/cheeseplus/32/960_2.png) [@cheeseplus](https://discourse.chef.io/u/cheeseplus)\
**Post date:** [November 14, 2017, 1:17pm UTC](https://discourse.chef.io/t/bash-output-to-variable/11835/4 "2017-11-14T13:17:36Z")

</div>

This solution is workable but that mess of code is exactly why a Custom Resource or Helper method is recommended. Any sufficiently overly complicated `ruby_block` is a code smell.
