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.
"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!!!
Hello Rafael,
Could you provide the actual information for that step.
I know about %s, but not %c.
Thanks
- 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
shashi01 -
This is a beautiful example, and it makes
perfect sense!!
Many thanks for adding to my knowledgebase!
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
Take a look at this python doc : https://docs.python.org/3/library/time.html#time.strftime
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 ).
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!
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,
@Rafael Can you please share which exercise or section you are referring?
@Rafael Please ignore my last repl I see its already been answered.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.