Creating environmental variable in ansible playbook

I have created a playbook. I want to define environmental variables when I execute a task.

  • name: Executing scripts
    shell: sh /root/something.sh
    register: shell
    environment:
    SOMETHING: something

This is not working. any idea?

I did not get that working either.

I ended up putting my environment variables in the actual environment in Linux. Like this:

export ANSIBLE_NET_USERNAME=vyos
export ANSIBLE_NET_PASSWORD=password

Some bugs in you script maybe, this works for me.

$ more something.sh
#!/bin/sh
echo $SOMETHING

playbook.yml
- hosts: localhost
   tasks:
     - name: Executing scripts
       shell: sh something.sh
       register: shell
       environment:
         SOMETHING: Yabba Dabba Doo!

     - debug: var=shell.stdout

Output from ansible-playbook

PLAY [localhost]

my bad. Yes it is working. Anyway to define it globally as long as all task runs? I tried to set it in top of tasks it shows error:

It should work. See example from @Kai Stian Olstad

You can only use environment on play, role, block or task.