chef-infraknifedatabags

Pass CLI arguments to chef-client while knife ssh


I am trying to automate application deployment using chef.

I had prepared cookbook which is generic(works based on input attribute values) to deploy APIs. I've kept all the attributes in my attribute folder.

Here the problem am facing is I am able to successfully deploy the package if I keep package version number in attribute file. where as in reality package number gets changed every time that CD pipeline gets triggered. And am thinking to use chef-client CLI feature to pass parameters as JSON file with -j option.

Problem is I don't want to touch my cookbook to update package version every time when CD pipeline is triggered, because if i update anything in my cookbook I've to again run CI for cookbook validation and at the same time I can't keep a file in every node(ranges from 50 - 500 servers) and update version remotely before chef-client gets triggered.

Please help me in finding a way to pass parameter remotely to knife ssh. Or any other solutions that solves this problem. Thank you very much in advance.


Solution

  • If the package version is specified as an attribute, why not set it in a chef environment?

    https://docs.chef.io/environments.html

    Using this approach you then have a choice of creating an environment for each version of your application:

    Or alternatively just update an existing environment for each stage in your pipeline process:

    The point is when you bootstrap new nodes you can specify the environment. The environment will ensure that each node will has the same node attribute override settings (You can also update the environment of an existing node),

    Lastly, the non-obvious benefit of using an environment is the ability to "pin" the versions of the application cookbooks you're using. At some stage you will experience a need to upgrade your cookbook and that change will need to be versioned too!

    In conclusion here's an example environment, which illustrates the principles:

    {
      "name": "mytest",
      "cookbook_versions": {
        "myappcookbook1": "= 1.0",
        "myappcookbook2": "= 2.0"
      },
      "override_attributes": {
        "myapp1": {
          "version": "1.0"
        },
        "myapp2": {
          "version": "2.0"
        }
      }
    }