25

I am writing a rolling upgrade playbook and would like to print out the hostname of current host been upgraded.

I put inventory_hostname and ansible_hostname in task names but that did not work

- name: upgrade softare on {{inventory_hostname}}
- name: current host is {{ansible_hostname}}

debug works fine

- name: Test a variable
  debug: var=inventory_hostname

TASK: [Test a variable] ******************************************************* 
ok: [SERV14] => {
    "var": {
        "inventory_hostname": "SERV14"
    }
}

So what should I do to be able to use those variables in task name descriptions.

Thanks

2 Answers2

36

Starting from v2.0 Ansible supports variable substitution in task/handler names: https://github.com/ansible/ansible/issues/10347, so these examples will work as expected:

- name: upgrade software on {{inventory_hostname}}
- name: current host is {{ansible_hostname}}
anlar
  • 4,165
2

I think you should write the {{ ansible_hostname }} after the tasks:, because before that it hasn't yet gathered the facts so it can't give the result. I may be wrong on the concept but in practice it was successful.

tasks:
  - name: Install the httpd on {{ ansible_hostname }}
    yum: 
      name: httpd 
      state: latest

playbook

play output

Stephen Kitt
  • 434,908