0

Here is a simple Ansible playbook:

- name: this command prints FAILED when it fails
  command: /usr/bin/example-command -x -y -z
  register: command_result
  failed_when: "'FAILED' in command_result.stderr"

Now, I know variable command_result has a method called stderr but how can I get a list of all methods?

4

You can use debug to dump the variable:

- name: this command prints FAILED when it fails
  command: /usr/bin/example-command -x -y -z
  register: command_result
  failed_when: "'FAILED' in command_result.stderr"
- name: dump command_result
  debug: var=command_result

This will output something like:

TASK: [dump command_result] **************************************************************
ok: [hostname] => {
    "command_result": {
        "changed": false,
        "cmd": "/usr/bin/example-command -x -y -z",
        "delta": "0:00:00.018233",
        "end": "2015-05-07 09:33:08.444674",
        "invocation": {
            "module_args": "/usr/bin/example-command -x -y -z",
            "module_name": "command"
        },
        "rc": 0,
        "start": "2015-05-07 09:33:08.426441",
        "stderr": "",
        "stdout": "whatever",
        "stdout_lines": [
            "whatever"
        ],
        "warnings": []
    }
}
  • thanks that certainly helped but when I tried a different variable ansible_hostname it does not have any methods, so that mean it is possible this variable only has a subset of all the possible methods, do you agree ? I hope there is a document list all the methods, like an API document. – Ask and Learn May 8 '15 at 7:26
  • Things like ansible_hostname are facts which are discovered in the first step of running a playbook. You can see what facts exist about a host (and the structure of the information) by running ansible hostname -m setup – wurtel May 8 '15 at 12:22
  • Yes I understand, I am trying to say that it is possible some variables does not have a cmd method and some variables have others methods. How do I find out them? I would thought there is a document for this but did not find it. – Ask and Learn May 13 '15 at 3:01
1

Variables in Ansible are basically strings intepreted by Jinja2 template engine. Read the language reference for template variable details. Jinja2's built-in types (string, sequence, mapping, etc.) resembles their counterpart in Python (str, list, dict, etc.) and have many common attributes and methods (callable attributes), but they are not all the same.

0

Actually, this "variable.stderr" is not a method, but a variable... In Ansible you don't deal with "methods" directly, as it is not a true programming language. The best source to learn, as always is the official docs for ansible, there the modules are pretty well covered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.