punith bp
punith bp

Reputation: 174

How to add variable inside a time variable in ansible?

I have a URI module calling an api with start time and end time

- name: silence alert of a specific hostname
          uri: 
            url: localhost:8080/alert-manager/api/v2/silences
            method: POST
            HEADER_Content-Type: "application/json"
            return_content: yes
            body: 
              matchers:
                - name : "hostname" 
                  value : "{{ remotehost_output.stdout}}"
                  isRegex: false
              startsAt: "{{ '%Y-%m-%dT%H:%M:%S' | strftime( ( ansible_date_time.epoch | int ) ) }}"
              endsAt : "{{ '%Y-%m-%dT%H:%M:%S' | strftime( ( ansible_date_time.epoch | int ) + ( 3600 * 2 )  ) }}"
              createdBy : "punith bp"
              comment: "via ansible"
            body_format: json
            status_code: 200 

I need send number for hours from a variable , i.e in this line

              endsAt : "{{ '%Y-%m-%dT%H:%M:%S' | strftime( ( ansible_date_time.epoch | int ) + ( 3600 * 2 )  ) }}"

instead of " 2 " I need to send a variable which contains that. Thanks in advance, I'm new to ansible.

Upvotes: 0

Views: 164

Answers (1)

Mirco
Mirco

Reputation: 232

try this one

- hosts: localhost
  tasks:
    - debug:
        msg: "{{ '%Y-%m-%dT%H:%M:%S' | strftime( ( ansible_date_time.epoch | int ) + ( 3600 * hours | int)  ) }}"
      vars:
        hours: 2

i get this log

PLAY [localhost] *******************************************************************************

TASK [Gathering Facts] *************************************************************************
ok: [localhost]

TASK [debug] ***********************************************************************************
ok: [localhost] => {
    "msg": "2022-04-27T16:59:02"
}

PLAY RECAP *************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Upvotes: 2

Related Questions