I try this

- name: Install required packages
 shell: "apt-get instal linux-headers-{{ ansible_kernel }}"

However getting this instead

{"msg": "The task includes an option with an undefined variable. The error was: 'ansible_kernel' is undefined\

I can can see this variable via

ansible -i myinventoryfile myhost -m setup | grep kernel

and also

ansible -i myinventoryfile myhost -m shell -a "uname -r"

how can I get this working ?

1 Answer 1


Works for me.

% cat facts.yml
#!/usr/bin/env ansible-playbook
- name: test
  hosts: somehost.example.edu
  - debug: var=ansible_kernel

  - shell: "echo {{ ansible_kernel }} > /tmp/thisisverybaddonotuse"
% ./facts.yml

PLAY [test] ***

TASK [Gathering Facts] ***
ok: [somehost.example.edu]

TASK [debug] ***
ok: [somehost.example.edu] => {
    "ansible_kernel": "3.10.0-693.21.1.el7.x86_64"

TASK [command] ***
changed: [somehost.example.edu]

somehost.example.edu  : ok=3    changed=1    unreachable=0    failed=0

% ssh somehost.example.edu "cat /tmp/thisisverybaddonotuse"

Possibly you are not gathering facts, or are otherwise doing something that is unclear from the limited information in your posting.

  • "vagrant+ansible" on linux is the environment in which I do not have "ansible_local" provisioning.I am trying to make remote deployment.
    – hakkican
    Mar 29, 2018 at 5:26
  • In my custom inventory file "gather_facts: false".This is not the case.It appears to be I forget to define "var=ansible_kernel" part in my ansible role.I add this to "vars/main.yml" it is ok now. Do I need to put this in original message at the top or leave it here.kind of new to this place
    – hakkican
    Mar 29, 2018 at 7:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .