ansible / ansible-runner

A tool and python library that helps when interfacing with Ansible directly or as part of another system whether that be through a container image interface, as a standalone tool, or as a Python module that can be imported. The goal is to provide a stable and consistent interface abstraction to Ansible.
Other
968 stars 356 forks source link

How to get environment variable in callback plugin #1352

Closed visukcse closed 7 months ago

visukcse commented 7 months ago

Description

  1. I am executing command _ANSIBLE_STDOUTCALLBACK=json ansible-playbook playbook.yml -i inventory.ini. But ansible.cfg has _stdoutcallback = null. In some cases, I need to get ansible output as JSON, so I set _ANSIBLE_STDOUTCALLBACK=json in CLI to override global value. And I have callback plugin and I want to know what is the stdout callback value.
  2. Is there any way to avoid callback method's print message adding to the ansible JSON output. My requirement is, though I have callback plugin and I have some print statement in the _v2_runner_onok method, it should not be added to JSON output.

ANSIBLE_STDOUT_CALLBACK=json ansible-playbook playbook.yml -i inventory.ini The custom message _v2_runner_onok(this is print message added in v2_runner_on_ok in callback plugin) is getting added to the ansible output and it impacts the JSON format

v2_runner_on_ok
v2_runner_on_ok
{
  "custom_stats": {

  },
  "global_custom_stats": {

  },
  "plays": [
    {
      "play": {
        "duration": {
          "start": "2024-04-02T16:39:49.226389Z"
        },
        "id": "e4434bbf-1410-d260-261f-000000000007",
        "name": "localhost"
      },
      "tasks": [

      ]
    },
    {
      "play": {
        "duration": {
          "end": "2024-04-02T16:39:51.115266Z",
          "start": "2024-04-02T16:39:49.242533Z"
        },
        "id": "e4434bbf-1410-d260-261f-000000000019",
        "name": "hosts*"
      },
      "tasks": [
        {
          "hosts": {
            "HOST_1": {
              "_ansible_no_log": false,
              "action": "command",
              "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python3.6"
              },
              "changed": true,
              "cmd": [
                "ls"
              ],
              "delta": "0:00:01.003975",
              "end": "2024-04-02 16:39:51.054327",
              "invocation": {
                "module_args": {
                  "_raw_params": "ls",
                  "_uses_shell": false,
                  "argv": null,
                  "chdir": null,
                  "creates": null,
                  "executable": null,
                  "removes": null,
                  "stdin": null,
                  "stdin_add_newline": true,
                  "strip_empty_ends": true,
                  "warn": true
                }
              },
              "rc": 0,
              "start": "2024-04-02 16:39:50.050352",
              "stderr": "",
              "stderr_lines": [

              ],
              "stdout": "bin\nssl.p12\nssl.pem",
              "stdout_lines": [
                "bin",
                "ssl.p12",
                "ssl.pem"
              ],

            },
            "HOST_2": {
              "_ansible_no_log": false,
              "action": "command",
              "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python3.6"
              },
              "changed": true,
              "cmd": [
                "ls"
              ],
              "delta": "0:00:00.002774",
              "end": "2024-04-02 16:39:50.053728",
              "invocation": {
                "module_args": {
                  "_raw_params": "ls",
                  "_uses_shell": false,
                  "argv": null,
                  "chdir": null,
                  "creates": null,
                  "executable": null,
                  "removes": null,
                  "stdin": null,
                  "stdin_add_newline": true,
                  "strip_empty_ends": true,
                  "warn": true
                }
              },
              "rc": 0,
              "start": "2024-04-02 16:39:50.050954",
              "stderr": "",
              "stderr_lines": [

              ],
              "stdout": "bin\nssl.p12\nssl.pem",
              "stdout_lines": [
                "bin",
                "ssl.p12",
                "ssl.pem"
              ],

            }
          },
          "task": {
            "duration": {
              "end": "2024-04-02T16:39:51.115266Z",
              "start": "2024-04-02T16:39:49.402803Z"
            },
            "id": "e4434bbf-1410-d260-261f-00000000006f",
            "name": "File : List the files and directories"
          }
        }
      ]
    }
  ],
  "stats": {
    "HOST_1": {
      "changed": 1,
      "failures": 0,
      "ignored": 0,
      "ok": 1,
      "rescued": 0,
      "skipped": 0,
      "unreachable": 0
    },
    "HOST_2": {
      "changed": 1,
      "failures": 0,
      "ignored": 0,
      "ok": 1,
      "rescued": 0,
      "skipped": 0,
      "unreachable": 0
    }
  }
}

Version

$ ansible --version
  ansible 2.9.25
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/service/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC]

$ ansible-playbook --version
  ansible-playbook 2.9.25
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/service/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC]
Shrews commented 7 months ago

You need to open this issue in the ansible repo. This is the repo for ansible-runner.