Deanna
Community Manager
Community Manager
  • 3,475 Views

Welcome to the Developing Advanced Automation with Red Hat Ansible Automation (DO374) group!

We are excited to launch a space dedicated to the Red Hat Training course Developing Advanced Automation with Red Hat Ansible Automation Platform!

To gain the most value from this group - click the "Join Group" button in the upper right hand corner of the group home page.

We encourage group members to collaborate in this group to discuss topics, ask questions, share best practices and tips, provide course feedback, and share their accomplishments as it relates to DO374.

Read more about Developing Advanced Automation with Red Hat Ansible Automation Platform here.

--
Deanna
Labels (2)
24 Replies
Trevor
Starfighter Starfighter
Starfighter
  • 1,093 Views

"I failed my way to success." - Thomas Edison

Huh!  This fella didn't do too bad!!!

 

" I never tried quitting, and I never quit trying."  -  Dolly Parton

Never quitting - that's probably why the world knows that name!!!

 

 

Trevor "Red Hat Evangelist" Chandler
Rafael
Cadet
Cadet
  • 993 Views

Does anyone know what '%c' is in the lineinfile from step 3.1?
Trevor
Starfighter Starfighter
Starfighter
  • 765 Views

Hello Rafael,

Could you provide the actual information for that step.
I know about %s, but not %c.

Thanks

Trevor "Red Hat Evangelist" Chandler
shashi01
Moderator
Moderator
  • 742 Views

@Trevor 

- name: Save server times to workstation
      ansible.builtin.lineinfile:
        path: /tmp/times.txt
        state: present
        mode: '0644'
        create: true
        insertafter: EOF
        line: |
          {{ ansible_facts['fqdn'] }}
          {{ '%c' | strftime(ansible_facts['date_time']['epoch']) }}
      delegate_to: workstation
Trevor
Starfighter Starfighter
Starfighter
  • 717 Views

shashi01 -

This is a beautiful example, and it makes
perfect sense!!

Many thanks for adding to my knowledgebase!

Trevor "Red Hat Evangelist" Chandler
0 Kudos
Chetan_Tiwary_
Moderator
Moderator
  • 730 Views

@Trevor @Rafael  

The strftime() function transforms a time representation, such as a tuple or struct_time produced by gmtime() or localtime(), into a string according to the specified format pattern. If no explicit time is provided, it utilizes the current time obtained from localtime(). The format argument must be a string.

Refer this : https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html 

Chetan_Tiwary__0-1700224863107.png

Take a look at this python doc : https://docs.python.org/3/library/time.html#time.strftime 

Chetan_Tiwary__1-1700224948315.png

 

The %c specifier outputs a date and time string in a format that is appropriate for the user's locale settings. For example, if the user's locale is set to en_US, the output might look like:

"Fri Nov 17 17:06:40 2023 "   Which is very pleasing to eyes compared to " 2023-11-17 17:06:40" ( based on our use purpose ). 

Trevor
Starfighter Starfighter
Starfighter
  • 714 Views

Chetan -

Thanks for the extra content.  These items called "directives"
are what I worked with in the mid 90s, when teaching C programming.
In that world, they are known as "format specifiers".   Also, the
functionality of %c in C programming is different from %c
in Ansible.  No big deal!  Different behavior in terms of how it
formats the data referenced, but the overall purpose of its use
is still what I know from C and Python programming.

Not having to devote any time to comprehending the purpose
of those "diirectives" (still sounds funny to me), I could put my
attention on using them in Ansible, which is very straightforward
in and of itself.  

As always, thanks for your contribution to extending my 
knowledge!

Trevor "Red Hat Evangelist" Chandler
0 Kudos
shashi01
Moderator
Moderator
  • 750 Views

Hello @Rafael 

Thanks for reaching out.

the lineinfile module from the ansible.builtin collection. The lineinfile module is used to manage lines in text files. In this case, it's used to add a line to a file.


'%c': This is a format string used with the strftime function in many programming languages, including Python. The %c format specifier represents the locale’s appropriate date and time representation.
So, when used with strftime, %c will format the given time into a string that is formatted according to the local date and time standards of the system running the Ansible playbook.

strftime function and its format specifiers, you can look at the Python documentation, as Ansible uses Python's strftime behavior: Python 3 strftime documentation(https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior). Keep in mind that %c will output the date and time format based on the system's locale settings,

0 Kudos
Wasim_Raja
Moderator
Moderator
  • 528 Views

@Rafael Can you please share which exercise or section you are referring?

0 Kudos
Wasim_Raja
Moderator
Moderator
  • 525 Views

@Rafael Please ignore my last repl I see its already been answered.

0 Kudos
Join the discussion
You must log in to join this conversation.