[chef] Re: Re: Re: Appending to an existing file, i.e nginx site


Chronological Thread 
  • From: Joseph Bowman < >
  • To:
  • Subject: [chef] Re: Re: Re: Appending to an existing file, i.e nginx site
  • Date: Tue, 19 Mar 2013 10:39:03 -0400

I do this a lot in my recipes using bash, albeit for single line items such as in hosts files and such. Moving it up to something that supports multi-line using ruby I'm assuming wouldn't be too difficult. I'm not a ruby developer though, so maybe I'm wrong?

The logic is simply

 - grep the file to see if what I want to add is already there or not
 - if not, put it in

As an example, I need loopback on the localhost line for our backup application

ash "add_loopback" do
        code <<-EOH
                LOOPBACKCHECK=$(grep loopback /etc/hosts | wc -l)
                if [ $LOOPBACKCHECK -lt 1 ];
                then
                        sed -i  's/::1[ \t]*localhost/::1\tlocalhost\tloopback/g' /etc/hosts
                fi
        EOH
end


Or, I also need the hostname assigned to the default outbound ip for the host which is always eth0 for us

bash "ensure_hostname" do
        code <<-EOH
                HOSTNAMECHECK=$(grep $HOSTNAME /etc/hosts | wc -l)
                if [ $HOSTNAMECHECK -lt 1 ];
                then
                        HOSTIP=$(grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 | sed s/'IPADDR='//)
                        echo "$HOSTIP   $HOSTNAME" >> /etc/hosts
                fi
        EOH
end






On Tue, Mar 19, 2013 at 10:29 AM, Michael Della Bitta < " target="_blank"> > wrote:
On Mon, Mar 18, 2013 at 4:24 PM, AJ Christensen < "> > wrote:
> Please don't edit files that already exist. Please never do this.

Is that really a practical edict? What about files like
/etc/sysctl.conf needing to be written to by multiple recipes?

FileEdit works, but it gets a bad rap on this list. Chef's going to
need a canonical and supported way to do this...


Michael Della Bitta

------------------------------------------------
Appinions
18 East 41st Street, 2nd Floor
New York, NY 10017-6271

www.appinions.com

Where Influence Isn’t a Game




Archive powered by MHonArc 2.6.16.

§