0

I`m trying to pass a variable to the playbook with current date and time. The goal is to create a DIR at all affected hosts for deployment, consisting current date and time with seconds. The number of hosts is 12, and I use "serial: 2" option in the playbook to run job simaltenously only on 2 hosts, so I need 6 runs to complete the whole job. I use Extra Variables field on Template's properties page in AWX to pass the variable - datetime: '{{ansible_date_time}}', but this variable's value changes with every serial play, and thus created DIRs have different names on different hosts.

Now I use the following code in main.yml in Roles to debug:

- name: Debug date and time
  debug:
    msg: "{{datetime.iso8601_basic_short}}"

And what I get:

TASK [Gathering Facts] *********************************************************
ok: [server1]
ok: [server2]
TASK [dqtestrole : Debug date and time] ****************************************
ok: [server1] => {
    "msg": "20190717T094151"
}
ok: [server2] => {
    "msg": "20190717T094151"
}
PLAY [Deploy WAR-file for Tomcat] **********************************************
TASK [Gathering Facts] *********************************************************
ok: [server3]
ok: [server4]
TASK [dqtestrole : Debug date and time] ****************************************
ok: [server3] => {
    "msg": "20190717T094155"
}
ok: [server4] => {
    "msg": "20190717T094155"
}

...

What I also tried:

  • assigning value in playbook YML file

  • assigning value in "AWX-Settings-Jobs-Extra environment variables" and then accessing through lookup('env','datetime')

So, I am sure the right way exists, but after googling and trying for few days I still cannot find it. Does anybody have any suggestions? Thanks in advance.

ADDITION

Tried Pouyan`s idea with Pre-tasks, works the same way.

- name:  
  hosts: dc1  
  serial: 2  
  pre_tasks:  
  - name: Get time and date  
    set_fact:  
       date_time: "{{datetime.iso8601_basic_short}}"  
  roles:  

main.yml:

msg: "{{datetime.iso8601_basic_short}}, {{date_time}}"

Result:

ok: [server1] => {"msg": "20190717T114204, 20190717T114204"}  
ok: [server2] => {"msg": "20190717T114204, 20190717T114204"}  
......  
ok: [server3] => {"msg": "20190717T114208, 20190717T114208"}  
ok: [server4] => {"msg": "20190717T114207, 20190717T114207"}  

1 Answers1

0

Have you tried to set it as pre_task variable in playbook. It is something like this:

hosts: hostname
become: yes
pre_tasks:
  - name: Get the secrets from Vault
    set_fact:
       date_time: "{{datetime.iso8601_basic_short}}"
Pouyan
  • 499
  • 1
  • 5
  • 14