How to manage the client

See also: Client

See also: terraform (CLI client)

Install the client

To install the terraform client for Juju on a Linux that supports snaps, run:

sudo snap install terraform

The client can also be installed on Windows or macOS, or on Linux in other ways.

See more: Hashicorp | Install Terraform

Make the client use the juju provider

In your terraform.tf file, require the juju provider:

terraform {
  required_providers {
    juju = {
      version = "~> 0.10.0"
      source  = "juju/juju"
    }
  }
}

Configure the client’s juju provider

There are 3 ways to do configure the client’s juju provider with your controller’s details.

For all methods: To view your controller's details, run:

juju show-controller --show-password

Using static credentials

In your Terraform plan, in your provider specification, use the various keywords to provide your controller information statically:

provider "juju" {
   controller_addresses = "10.225.205.241:17070,10.225.205.242:17070"
   username = "jujuuser"
   password = "password1"
   ca_certificate = file("~/ca-cert.pem")
}

See Terraform | juju provider

Using environment variables

In your Terraform plan, leave the provider specification empty:

provider "juju" {}

In your terminal, export the controller environment variables with your controller’s values. For example:

export JUJU_CONTROLLER_ADDRESSES = "10.225.205.241:17070,10.225.205.242:17070" 
export JUJU_USERNAME="jujuuser"
export JUJU_PASSWORD="password1"
export JUJU_CA_CERT= file("~/ca-cert.pem")

Using the juju CLI

In your Terraform plan, leave the provider specification empty:

provider "juju" {}

Then, your terminal, switch to the desired controller: juju switch <controller>. Your Terraform plan will be interpreted relative to that controller.

See also: python-libjuju

Install the client

pip3 install juju

Quick Start with Python-libjuju

Here’s a simple example that shows basic usage of the library. The example connects to the currently active Juju model, deploys a single unit of the ubuntu charm, then exits:

Note : Pylibjuju requires an already bootstrapped Juju controller to connect to.

  #!/usr/bin/python3

  import logging
  import sys

  from juju import jasyncio
  from juju.model import Model


  async def deploy():
      # Create a Model instance. We need to connect our Model to a Juju api
      # server before we can use it.
      model = Model()

      # Connect to the currently active Juju model
      await model.connect()

      try:
          # Deploy a single unit of the ubuntu charm, using the latest revision
          # from the stable channel of the Charm Store.
          ubuntu_app = await model.deploy(
            'ubuntu',
            application_name='my-ubuntu',
          )

          if '--wait' in sys.argv:
              # optionally block until the application is ready
              await model.wait_for_idle(status = 'active')

      finally:
          # Disconnect from the api server and cleanup.
          await model.disconnect()


  def main():
      logging.basicConfig(level=logging.INFO)

      # If you want to see everything sent over the wire, set this to DEBUG.
      ws_logger = logging.getLogger('websockets.protocol')
      ws_logger.setLevel(logging.INFO)

      # Run the deploy coroutine in an asyncio event loop, using a helper
      # that abstracts loop creation and teardown.
      jasyncio.run(deploy())


  if __name__ == '__main__':
      main()

More examples can be found in the docs, as well as in the examples directory in the repository which can be run using tox. For example, to run examples/connect_current_model.py, use:

  tox -e example -- examples/connect_current_model.py

Or you can directly run it via python as well:

 $ python3 examples/connect_current_model.py

Using Python-libjuju with Python REPL

To experiment with the library in a REPL, launch python in asyncio mode

  $ python3 -m asyncio

and then, to connect to the current model and fetch status:

  >>> from juju.model import Model
  >>> model = Model()
  >>> await model.connect_current()
  >>> status = await model.get_status()

Contributors: @cderici, @hmlanigan, @tmihoc

Last updated 6 days ago. Help improve this document in the forum.