Creating login items from Ansible

Options
babaika
babaika
Community Member
edited April 2023 in CLI

Im trying to figure out how to use 1Password CLI to create items from Ansible.
Everything I tried is giving me an error: "Failed to create item: invalid JSON"

Here is my test task:

- name: Run command
  ansible.builtin.command: "op item create --category login --title 'Test'"
  delegate_to: localhost

Even running an empty command "op item create" gives the same error about invalid JSON even though I would expect it to fail with something related to a missing category.

I also tried to create a json template and use it, but in that case task finishes fine, but no actual login is created:

- name: Run command
  ansible.builtin.command: "echo {{ lookup('file', 'login.tpl') }} | op item create"
  delegate_to: localhost

Executing command locally works just fine

CLI version: 2.16.1

What am I doing wrong?


1Password Version: 8.10.4
Extension Version: Not Provided
OS Version: MacOS 13.3.1
Browser:_ Not Provided

Comments

  • babaika
    babaika
    Community Member
    Options

    Here is a raw error msg (from the first command) in case its helpful:

    fatal: [ans-test3 -> localhost]: FAILED! => {
        "changed": true,
        "cmd": [
            "op",
            "item",
            "create",
            "--category",
            "login",
            "--title",
            "Test"
        ],
        "delta": "0:00:01.144492",
        "end": "2023-04-28 12:21:14.001860",
        "invocation": {
            "module_args": {
                "_raw_params": "op item create --category login --title 'Test'",
                "_uses_shell": false,
                "argv": null,
                "chdir": null,
                "creates": null,
                "executable": null,
                "removes": null,
                "stdin": null,
                "stdin_add_newline": true,
                "strip_empty_ends": true
            }
        },
        "msg": "non-zero return code",
        "rc": 1,
        "start": "2023-04-28 12:21:12.857368",
        "stderr": "[ERROR] 2023/04/28 12:21:13 Failed to create item: invalid JSON",
        "stderr_lines": [
            "[ERROR] 2023/04/28 12:21:13 Failed to create item: invalid JSON"
        ],
        "stdout": "",
        "stdout_lines": []
    }
    
  • mkhinda
    mkhinda
    Community Member
    Options

    I am having this exact same issue. Using 1password-cli 2.17.0.
    Something to add is that the op get command works fine:
    "cmd": ["op", "item", "get", "FOO", "--vault", "Private", "--fields", "notesPlain"]
    but the simple create does not:
    "cmd": ["op", "item", "create"]
    "stderr": "[ERROR] 2023/05/03 14:08:17 Failed to create item: invalid JSON"

  • mkhinda
    mkhinda
    Community Member
    Options

    @babaika one of my colleagues at work found a solution to this that worked for us. This is what I did in my playbook that worked:

    ansible.builtin.shell:
    cmd: |
      echo '{ "title": "YOUR TITLE", "fields": [ { "id": "notesPlain", "value": "Hey these are some notes" } ] }' |  op item create --vault Private --category "Password" "password={{ super_secret_password }}" --tags=foo --format=JSON
    

    You can get all the fields for the echo json by downloading the template:

    op item template get --out-file=password.json "Password"
    
  • Hey everyone,

    My apologies for the late response. This seems to be a bug of op item create present in piped environments, the workaround is indeed to use --template or to pipe a part of the item (or a full template) into the command:

    op item template get login | op item create
    

    I hope this helps, as a workaround. This is something we're currently looking into fixing, so thank you for raising this!

    Best,
    Horia

This discussion has been closed.