ansible date and time variable

45 views
Skip to first unread message

Om Prasad Surapu

unread,
May 18, 2020, 11:29:44 AM5/18/20
to Ansible Project
Hi Everyone,

I have an ansible playbook which will be used to take AMI snapshot of EC2 instance every month in a year.

Now i got a new requirement that snapshot should not be taken for every month and it should for 3 months once. I checked ansible_date_time but did not see the logic to take every 3 months. It has just day, month and yearly like ansible_date_time.month. 

here is my playbook (variable calls from template). Is there any way to take snapshot for every 3 months. It would be grateful if anyone could answer.

------------------------
- hosts: localhost
  connection: local

  vars:
    - ami_instance_id: null
    - ami_date: "{{ ansible_date_time.month }}{{ ansible_date_time.year }}"
    - ami_name: null
    - ami_service: null
  tasks:
    - name: create AMI
      ec2_ami:
        instance_id: "{{ ami_instance_id }}"
        wait: yes
        name: "{{ ami_name }}_{{ ami_date }}"
        region: us-east-1
        tags:
          Name: "{{ ami_name }}_{{ ami_date }}"
          Service: "{{ ami_service }}"
      register: instance

    - debug: var=instance

---------------------------------

Dick Visser

unread,
May 18, 2020, 12:06:39 PM5/18/20
to ansible...@googlegroups.com
On Mon, 18 May 2020 at 17:29, Om Prasad Surapu <prasa...@gmail.com> wrote:
>
> Hi Everyone,
>
> I have an ansible playbook which will be used to take AMI snapshot of EC2 instance every month in a year.

"Will be used" implies some existing mechanism to take snapshots.
The playbook you mentioned below doesn't seem to have any scheduling.
So what is the mechanism? Cloudwatch? Cron? Manual action?

> Now i got a new requirement that snapshot should not be taken for every month and it should for 3 months once. I checked ansible_date_time but did not see the logic to take every 3 months. It has just day, month and yearly like ansible_date_time.month.

Can you please explain a bit better what you mean by "it should for 3
months once"?

> here is my playbook (variable calls from template). Is there any way to take snapshot for every 3 months. It would be grateful if anyone could answer.
>
> ------------------------
>
> - hosts: localhost
> connection: local
>
> vars:
> - ami_instance_id: null
> - ami_date: "{{ ansible_date_time.month }}{{ ansible_date_time.year }}"
> - ami_name: null
> - ami_service: null
> tasks:
> - name: create AMI
> ec2_ami:
> instance_id: "{{ ami_instance_id }}"
> wait: yes
> name: "{{ ami_name }}_{{ ami_date }}"
> region: us-east-1
> tags:
> Name: "{{ ami_name }}_{{ ami_date }}"
> Service: "{{ ami_service }}"
> register: instance
>
> - debug: var=instance

Did you mean to post some results here?

--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Om Prasad Surapu

unread,
May 18, 2020, 12:10:55 PM5/18/20
to Ansible Project
Thanks for your reply.

it was actually scheduled with crontab on Ansible Tower. I found that there is an option to change the interval every 3 months.

Thanks,
Om
Reply all
Reply to author
Forward
0 new messages