How can i use Host Variables in Task name

39 views
Skip to first unread message

Ankit

unread,
Oct 9, 2020, 11:33:51 AM10/9/20
to Ansible Project
Hello Everyone,

I am trying to use the host variables set for each host of inventory in the task name but for some reason, it doesn't work.

I have got a dynamic inventory (hosts imported in AWX using it) and all the hosts have got their own variables updated based on the information available from source. There are 2 variables in the task name which i want to use in such a way that they are updated every time a task runs for each host of inventory. Ex variable for one of the host:

AID: 1234
Environment: Staging

What i have been trying:

- hosts: all
  vars:
     AIR: "{{ hostvars.AIRID }}"
     ENVI: "{{ hostvars.Environment }}"
     
- name: "Event Logs for AIRID={{ AIR }} ENVIRONMENT={{ ENVI }}"
  command: some command here

What i get in the result for task name is:
TASK [Event Logs for AIRID={{ AIR }} ENVIRONMENT={{ ENVI }}]

I have even tried set_fact task before this task but that too doesn't work. Can anyone suggest how can i get the host variables in task name?


PS: Please ignore the indentation, this maybe because of formatting change in the post. 

Dick Visser

unread,
Oct 9, 2020, 11:55:13 AM10/9/20
to ansible...@googlegroups.com
On Fri, 9 Oct 2020 at 17:34, Ankit <ankitva...@gmail.com> wrote:
>
> Hello Everyone,
>
> I am trying to use the host variables set for each host of inventory in the task name but for some reason, it doesn't work.
>
> I have got a dynamic inventory (hosts imported in AWX using it) and all the hosts have got their own variables updated based on the information available from source. There are 2 variables in the task name which i want to use in such a way that they are updated every time a task runs for each host of inventory. Ex variable for one of the host:
>
> AID: 1234
> Environment: Staging
>
> What i have been trying:
>
> - hosts: all
> vars:
> AIR: "{{ hostvars.AIRID }}"
> ENVI: "{{ hostvars.Environment }}"

Try this:

vars:
AIR: "{{ AIRID }}"
ENVI: "{{ Environment }}"


> PS: Please ignore the indentation, this maybe because of formatting change in the post.

Use plain text instead - no formatting issues there.


DIck

Tej Singh Rana

unread,
Oct 9, 2020, 12:10:19 PM10/9/20
to ansible...@googlegroups.com
Should be in this format : -

vars:
     user: "{{ hostvars['host-name']['name'] }}"
  tasks:
    - name: 'Name is {{ user }} '



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6eb4aa96-1156-4a37-b2d8-c527572ffd1fn%40googlegroups.com.


--
---------------------
Tej Singh Rana

Ankit Vashistha

unread,
Oct 16, 2020, 11:29:19 AM10/16/20
to Ansible Project
Hello,

Thanks for your response. Sadly, it didn't work.

Regards,
Ankit


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages