Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. Ansible is agent less (push configuration), Python based, connect via SSH to run commands, Open Source.
- Getting started with Ansible – Ansible is an amazing utility for automating your server builds, and in this series we’ll go through everything you need to know in order to become productive with it.
- What is Ansible?: Advantages, Features, and Architecture by SimpliLearn.com
- you need to learn Ansible RIGHT NOW!! (Linux Automation)
- Ansible 101 – Episode 1 – Introduction to Ansible by Jeff Geerling
- Ansible vs Kubernetes by SimpliLearn.com
- Ansible Documentation
- Ansible Documentation: Ansible Network Examples
- How to Use Ansible: A Reference Guide
- Ansible 101 – Episode 1 – Introduction to Ansible
- Ansible Username Password Prompt with vars_prompt
- Search: Ansible playbook prompt for password
- Ansible Username Password Prompt with vars_prompt
- Search: ansible playbook list ip addresses and interfaces
- DevSec Hardening Framework: ansible-os-hardening
- Simple automation for all your Linux servers with Ansible
- Blog: Simple automation for all your Linux servers with Ansible
Authentication
- SSH Key Linux secure remote authentication to your Server by The Digital Life
- Getting started with Ansible 02 – SSH Overview & Setup
Ansible Examples
Ansible: Create a file with Date & Time Stamp
This Ansible playbook demonstrates how to:
- Retrieve the current date and time via ansible-facts
- Store the date and time in variables
- Concatenate the date & time with a string to build the filename
- Create the file with the timestamped filename
- Run a command for hosts in the inventory
- Retrieve attributes from the command results
- Write the results of the command for each host to the timestamped file
---
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Ansible Playbook
# FileName: demo-timestamp-filename.yml
#
# Description: Demonstrate how to create a file
# with a timestamped filename
# and write command results to the file
#
# Revised: 2021-01-18 by Chin for ConsciousVibes.com
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- name: Output to Filename with Timestamp
hosts: healthchecks
connection: local
gather_facts: yes
strategy: linear
order: inventory
vars:
report_filename_prefix: "HealthCheckResults-"
report_date_time: "{{ ansible_date_time.date }}_{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}"
report_filename_date: "{{ report_filename_prefix }}{{ report_date_time }}.txt"
report_path: "/reports/healthchecks/{{ report_filename_date }}"
tasks:
- name: Create file with timestamped filename
delegate_to: localhost
lineinfile:
path: "{{ report_path }}"
create: yes
line: "Start: Health Check Report\n{{ report_path }}"
run_once: true
- name: Run nslookup command
delegate_to: localhost
throttle: 1
command: nslookup {{ inventory_hostname }}
register: nslookup_result
- name: Append nslookup results to a file
delegate_to: localhost
throttle: 1
blockinfile:
state: present
insertafter: EOF
dest: "{{ report_path }}"
marker: "- - - - - - - - - - - - - - - - - - - - -"
block: |
Server: {{ inventory_hostname }}
Environment: {{ environmentz }}
{{ nslookup_result.stdout_lines.3 }}
{{ nslookup_result.stdout_lines.4 }}
### End of Playbook
Note: If the time changes (e.g. hour or minutes) while the playbook is still iterating through the hosts in the inventory, the variable changes, throwing a “path does not exist” error for each of the remaining hosts.
See: Ansible: How to Define Variables in Playbook Which Do Not Change per Host
Reference
- Using Date and Timestamp Variable in Ansible Playbook (See: Re-using timestamp variable to refer the same object during the play)
- file – Manage files and file properties (syntax for using ansible_date_time.date)
- Using Date and Timestamp Variable in Ansible Playbook