rajuk
rajuk

Reputation: 1

Ansible task variable output not displaying

Below is my simple playbook

  name: "test"
  hosts: webservers

 tasks:
- name: Echo my_env_var
  shell: "echo $MY_ENV_VARIABLE"
  environment:
    MY_ENV_VARIABLE: whatever_value

- name: Echo my_env_var again
  shell: "echo $MY_ENV_VARIABLE"
  register: stdd
- debug: msg={{stdd.stdout_lines}}

My output is always msg:"" or msg: []. Why am i not able to see the value of variable

Upvotes: 0

Views: 1827

Answers (1)

300D7309EF17
300D7309EF17

Reputation: 24573

I took your example and changed it from debug msg to debug var. I also simplified it by only running the task once, and found the error in the process. The environment argument is specific to a task. You aren't including it in your second shell task.

Here's the example I used.

echo.yml

- hosts: localhost
  tasks:
  - name: Echo my_env_var
    shell: "echo $MY_ENV_VARIABLE"
    environment:
      MY_ENV_VARIABLE: whatever_value
    register: stdd
  - debug: var=stdd

execution

$ ansible-playbook -c local -i "localhost," echo.yml 

PLAY [localhost] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [localhost]

TASK: [Echo my_env_var] ******************************************************* 
changed: [localhost]

TASK: [debug var=stdd] ******************************************************** 
ok: [localhost] => {
    "var": {
        "stdd": {
            "changed": true,
            "cmd": "echo $MY_ENV_VARIABLE",
            "delta": "0:00:00.005332",
            "end": "2016-07-25 19:42:54.320667",
            "invocation": {
                "module_args": "echo $MY_ENV_VARIABLE",
                "module_complex_args": {},
                "module_name": "shell"
            },
            "rc": 0,
            "start": "2016-07-25 19:42:54.315335",
            "stderr": "",
            "stdout": "whatever_value",
            "stdout_lines": [
                "whatever_value"
            ],
            "warnings": []
        }
    }
}

PLAY RECAP ******************************************************************** 
localhost                  : ok=3    changed=1    unreachable=0    failed=0   

Upvotes: 1

Related Questions