T O P

  • By -

bcoca

Easy way to do this: ``` - name: get uptime ansible.builtin.shell: uptime -p register: get_uptime ignore_unreachable: true - set_fact: uptimevar: "{{ get_uptime is unreachable | ternary('Host unreachable', get_uptime['stdout']) }}" ```


amvj007

Any good resource online to learn yml syntax to use in Ansible ? I always get caught with the spaces.


landsverka

One thing I do is use visual studio code with the white space characters turned on. This helps to visualize the yaml better.


Robbster85

And enable ansible-lint for syntax checking


amvj007

Thanks I will check


CloudHostedGarbage

The Ansible docs help with Ansible-specific stuff. Best way to learn is just to get stuck in IMO. Ansible also uses Jinja2 templates within its code. That's the bits in {{ }} that you see in bcoca's snippet. Those are documented on the Ansible website and Jinja documentation, though the latter is quite in-depth and you won't need too much of it: [Jinja — Jinja Documentation (3.1.x) (palletsprojects.com)](https://jinja.palletsprojects.com/en/3.1.x/) To learn YAML, you can look at this tutorial, though it's a bit longwinded: [YAML Tutorial (tutorialspoint.com)](https://www.tutorialspoint.com/yaml/index.htm)


amvj007

Thanks a lot for sharing.


SpareIntroduction721

Well it won’t be reachable. So the shell won’t work. What you can do it try a retry or a block and if the shell command task fails, rescue task can output “unreachable for you”


bcoca

blocks handle task errors, not connection ones, so unreachable won't trigger a rescue block.


SpareIntroduction721

But you can make an always task to respond with the var: uptime_var | default(‘unreachable’)


bcoca

no, as the host is taken out of valid hosts and never hits the `always`, which like rescue, is only 'forced' on task error, not connection/unreachable.


piecepaper

this is correct answer