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

1
  • 4
    Currently, variable substitution can not be done in name. See here
    – cuonglm
    May 8, 2015 at 7:39

2 Answers 2

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}}
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

You must log in to answer this question.

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