SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
Ansible Callback Plugins
Jiri Tyr
About me
● Using Ansible since 2014
● Ansible contributor
○ Modules: yum_repository , jenkins_plugin , ldap_attr, ldap_entry
○ Jinja2 filter: comment
○ Bug fixing (mount module)
○ Code reviews
● Author of more than 100 publicly available Ansible roles
○ https://github.com/jtyr
○ https://galaxy.ansible.com/jtyr
What are callback plugins?
What are callback plugins?
● Control most of the Ansible's output by responding to events
● Over 30 built-in plugins
○ Format output (json, yaml, debug, dense, minimal, unixy, stderr, null, ...)
○ Send output to other system (slack, jabber, mail, grafana_annotations, logstash, splunk, ...)
How to use it?
How to use it?
# Command line
export ANSIBLE_STDOUT_CALLBACK=json
ansible-playbook -i hosts site.yaml
export ANSIBLE_LOAD_CALLBACK_PLUGINS=yes
export ANSIBLE_STDOUT_CALLBACK=json
ansible all -i hosts -m ping
# ansible.cfg
[defaults]
callback_plugins = path/to/my/plugins
Stdout_callback = json
bin_ansible_callbacks = True
How to use it?
# Query variable from Ansible
export ANSIBLE_LOAD_CALLBACK_PLUGINS=yes
export ANSIBLE_STDOUT_CALLBACK=json
export MYHOST=host01
ansible $MYHOST -i hosts -m debug -a 'var=ansible_host' | 
jq ".plays[0].tasks[0].hosts["$MYHOST"].ansible_host" | 
sed -e 's/^"//' -e 's/"$//' -e 's/^null$//'
How does it work?
● Two versions of callback functions
○ Functions for Ansible 2.x prefixed with v2_
● Three categories of callback functions
○ Playbook (playbook, play, task)
○ Runner (module)
○ Other (any, diff)
How does it work?
How does it work?
# Playbook
v2_playbook_on_cleanup_task_start
v2_playbook_on_handler_task_start
v2_playbook_on_import_for_host
v2_playbook_on_include
v2_playbook_on_no_hosts_matched
v2_playbook_on_no_hosts_remaining
v2_playbook_on_notify
v2_playbook_on_not_import_for_host
v2_playbook_on_play_start
v2_playbook_on_start
v2_playbook_on_stats
v2_playbook_on_task_start
v2_playbook_on_vars_prompt
# Runner
v2_runner_item_on_failed
v2_runner_item_on_ok
v2_runner_item_on_skipped
v2_runner_on_async_failed
v2_runner_on_async_ok
v2_runner_on_async_poll
v2_runner_on_failed
v2_runner_on_ok
v2_runner_on_skipped
v2_runner_on_start
v2_runner_on_unreachable
v2_runner_retry
# Other
v2_on_any
v2_on_file_diff
How does it work?
# callback_plugins/mycallback.py
from ansible.plugins.callback import CallbackBase
class CallbackModule(CallbackBase):
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'stdout'
CALLBACK_NAME = ' mycallback'
def v2_playbook_on_play_start(self, play):
self._display.display("Starting play")
def v2_playbook_on_stats(self, stats):
self._display.display("Showing stats")
$ ANSIBLE_STDOUT_CALLBACK= mycallback ansible-playbook -i localhost, site.yaml
Starting play
Showing stats
CSV callback plugin
● Allows to use Ansible as a reporting tool
● Turns Ansible's output into CSV (Comma-Separated Values)
● Meant to be used by plays producing output to STDOUT (raw or shell module)
● The output must be either valid CSV format or it has to fail (no empty string)
● Unsuccessful PR #65801
CSV callback plugin
CSV callback plugin
# Default usage
$ export ANSIBLE_CALLBACK_PLUGINS=callback_plugins
$ export ANSIBLE_LOAD_CALLBACK_PLUGINS=1
$ export ANSIBLE_STDOUT_CALLBACK=csv
$ ansible all -i localhost, -c local -m raw -a "echo ','"
host,status
localhost,ok
TOTAL: 1
OK: 1
FAILED: 0
UNREACHABLE: 0
CSV callback plugin
# Customized usage
[callback_csv]
fields = [
{'placeholder': '%n', 'name': 'name'},
{'name': 'host', 'in_message': True},
{'placeholder': '%h', 'name': 'ip', 'in_message': True},
{'name': 'distro', 'in_message': True},
{'name': 'version', 'in_message': True},
{'name': 'cpu', 'in_message': True},
{'name': 'mem', 'in_message': True},
{'placeholder': '%s', 'name': 'status'},
{'placeholder': '%g', 'name': 'group'},
{'placeholder': '%v', 'variable': 'inventory_hostname_short', 'name': 'short'}]
format_ok = %n,%m,%s,%g,%v
CSV callback plugin
# Customized usage
$ export ANSIBLE_CALLBACK_PLUGINS=callback_plugins
$ export ANSIBLE_LOAD_CALLBACK_PLUGINS=1
$ export ANSIBLE_STDOUT_CALLBACK=csv
$ ansible all -i localhost, -c local -m raw 
-a "echo $(cat ./facts.sh | base64 -w0) |
base64 -di > ./facts.sh &&
chmod +x ./facts.sh &&
./facts.sh"
name,hostname,ip,distro,version,cpu,mem,status,group,short
localhost,my.hostname.com,192.168.1.123,centos,7,4,3881048,ok,ungrouped,localhost
TOTAL: 1
OK: 1
FAILED: 0
UNREACHABLE: 0
CSV callback plugin
# Customized usage - Windows
$ export ANSIBLE_CALLBACK_PLUGINS=callback_plugins
$ export ANSIBLE_LOAD_CALLBACK_PLUGINS=1
$ export ANSIBLE_STDOUT_CALLBACK=csv
$ ansible host01 -i hosts -m win_shell -a '$myhost =
[System.Net.Dns]::GetHostByName((hostname)).HostName; $ip = Test-Connection -ComputerName
(hostname) -Count 1 | Select IPV4Address | %{$_.IPV4Address} | %{$_.IPAddressToString};
$version = Get-CimInstance Win32_OperatingSystem | select Version | %{$_.Version}; $cpu =
Get-WmiObject –class Win32_ComputerSystem | select NumberOfProcessors |
%{$_.NumberOfProcessors}; $mem = Get-WmiObject –class Win32_ComputerSystem | select
TotalPhysicalMemory | %{$_.TotalPhysicalMemory}; "{0},{1},windows,{2},{3},{4}" -f
$myhost.ToLower(), $ip, $version, $cpu, [int]($mem/1000)'
name,hostname,ip,distro,version,cpu,mem,status,group,short
host01,my.hostname.com,192.168.1.456,windows,10.0.18363,4,3881048,ok,ungrouped,host01
TOTAL: 1
OK: 1
FAILED: 0
UNREACHABLE: 0
Demo
Thank you for your attention!
Questions?

Más contenido relacionado

La actualidad más candente

Ninja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for BeginnersNinja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for BeginnersChang W. Doh
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansibleOmid Vahdaty
 
IT Automation with Ansible
IT Automation with AnsibleIT Automation with Ansible
IT Automation with AnsibleRayed Alrashed
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in RustChih-Hsuan Kuo
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentationKumar Y
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentationSuresh Kumar
 
Ansible Automation to Rule Them All
Ansible Automation to Rule Them AllAnsible Automation to Rule Them All
Ansible Automation to Rule Them AllTim Fairweather
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basicsManav Prasad
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentationJohn Lynch
 
NGINX Ingress Controller for Kubernetes
NGINX Ingress Controller for KubernetesNGINX Ingress Controller for Kubernetes
NGINX Ingress Controller for KubernetesNGINX, Inc.
 
Android media framework overview
Android media framework overviewAndroid media framework overview
Android media framework overviewJerrin George
 
Ansible Introduction
Ansible Introduction Ansible Introduction
Ansible Introduction Robert Reiz
 
U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0GlobalLogic Ukraine
 

La actualidad más candente (20)

Ninja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for BeginnersNinja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for Beginners
 
Network automation (NetDevOps) with Ansible
Network automation (NetDevOps) with AnsibleNetwork automation (NetDevOps) with Ansible
Network automation (NetDevOps) with Ansible
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansible
 
IT Automation with Ansible
IT Automation with AnsibleIT Automation with Ansible
IT Automation with Ansible
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
 
Automating with Ansible
Automating with AnsibleAutomating with Ansible
Automating with Ansible
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 
Ansible
AnsibleAnsible
Ansible
 
Ansible Automation to Rule Them All
Ansible Automation to Rule Them AllAnsible Automation to Rule Them All
Ansible Automation to Rule Them All
 
Git undo
Git undoGit undo
Git undo
 
Ansible get started
Ansible get startedAnsible get started
Ansible get started
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 
Ansible
AnsibleAnsible
Ansible
 
NGINX Ingress Controller for Kubernetes
NGINX Ingress Controller for KubernetesNGINX Ingress Controller for Kubernetes
NGINX Ingress Controller for Kubernetes
 
Android media framework overview
Android media framework overviewAndroid media framework overview
Android media framework overview
 
Nginx
NginxNginx
Nginx
 
Ansible Introduction
Ansible Introduction Ansible Introduction
Ansible Introduction
 
U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0
 

Similar a Ansible Callback Plugins Guide

Ansible tips & tricks
Ansible tips & tricksAnsible tips & tricks
Ansible tips & tricksbcoca
 
Automation with Ansible and Containers
Automation with Ansible and ContainersAutomation with Ansible and Containers
Automation with Ansible and ContainersRodolfo Carvalho
 
Ansible Inventory Plugins
Ansible Inventory PluginsAnsible Inventory Plugins
Ansible Inventory Pluginsjtyr
 
Development of Ansible modules
Development of Ansible modulesDevelopment of Ansible modules
Development of Ansible modulesjtyr
 
How to Vim - for beginners
How to Vim - for beginnersHow to Vim - for beginners
How to Vim - for beginnersMarcin Rogacki
 
Ansible - Swiss Army Knife Orchestration
Ansible - Swiss Army Knife OrchestrationAnsible - Swiss Army Knife Orchestration
Ansible - Swiss Army Knife Orchestrationbcoca
 
Getting by with just psql
Getting by with just psqlGetting by with just psql
Getting by with just psqlCorey Huinker
 
Cassandra and materialized views
Cassandra and materialized viewsCassandra and materialized views
Cassandra and materialized viewsGrzegorz Duda
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?Nikita Popov
 
Ansible for Beginners
Ansible for BeginnersAnsible for Beginners
Ansible for BeginnersArie Bregman
 
The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6Wim Godden
 
CLI, the other SAPI
CLI, the other SAPICLI, the other SAPI
CLI, the other SAPICombell NV
 
Functional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartFunctional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartChen Fisher
 
Openstack taskflow 簡介
Openstack taskflow 簡介Openstack taskflow 簡介
Openstack taskflow 簡介kao kuo-tung
 
Douglas Crockford: Serversideness
Douglas Crockford: ServersidenessDouglas Crockford: Serversideness
Douglas Crockford: ServersidenessWebExpo
 
JSUG - Tech Tips1 by Christoph Pickl
JSUG - Tech Tips1 by Christoph PicklJSUG - Tech Tips1 by Christoph Pickl
JSUG - Tech Tips1 by Christoph PicklChristoph Pickl
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptKamil Toman
 

Similar a Ansible Callback Plugins Guide (20)

Ansible tips & tricks
Ansible tips & tricksAnsible tips & tricks
Ansible tips & tricks
 
Automation with Ansible and Containers
Automation with Ansible and ContainersAutomation with Ansible and Containers
Automation with Ansible and Containers
 
Ansible Inventory Plugins
Ansible Inventory PluginsAnsible Inventory Plugins
Ansible Inventory Plugins
 
Sdl Basic
Sdl BasicSdl Basic
Sdl Basic
 
Development of Ansible modules
Development of Ansible modulesDevelopment of Ansible modules
Development of Ansible modules
 
How to Vim - for beginners
How to Vim - for beginnersHow to Vim - for beginners
How to Vim - for beginners
 
Ansible - Swiss Army Knife Orchestration
Ansible - Swiss Army Knife OrchestrationAnsible - Swiss Army Knife Orchestration
Ansible - Swiss Army Knife Orchestration
 
Kotlin
KotlinKotlin
Kotlin
 
Getting by with just psql
Getting by with just psqlGetting by with just psql
Getting by with just psql
 
Cassandra and materialized views
Cassandra and materialized viewsCassandra and materialized views
Cassandra and materialized views
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
Ansible for Beginners
Ansible for BeginnersAnsible for Beginners
Ansible for Beginners
 
The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6
 
CLI, the other SAPI
CLI, the other SAPICLI, the other SAPI
CLI, the other SAPI
 
Functional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartFunctional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smart
 
Openstack taskflow 簡介
Openstack taskflow 簡介Openstack taskflow 簡介
Openstack taskflow 簡介
 
Douglas Crockford: Serversideness
Douglas Crockford: ServersidenessDouglas Crockford: Serversideness
Douglas Crockford: Serversideness
 
JSUG - Tech Tips1 by Christoph Pickl
JSUG - Tech Tips1 by Christoph PicklJSUG - Tech Tips1 by Christoph Pickl
JSUG - Tech Tips1 by Christoph Pickl
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
 

Más de jtyr

Managing VMware VMs with Ansible
Managing VMware VMs with AnsibleManaging VMware VMs with Ansible
Managing VMware VMs with Ansiblejtyr
 
How does Ansible's agentless architecture work?
How does Ansible's agentless architecture work?How does Ansible's agentless architecture work?
How does Ansible's agentless architecture work?jtyr
 
Variable precedence: Where should I put a variable?
Variable precedence: Where should I put a variable?Variable precedence: Where should I put a variable?
Variable precedence: Where should I put a variable?jtyr
 
Managing multiple environments with Ansible
Managing multiple environments with AnsibleManaging multiple environments with Ansible
Managing multiple environments with Ansiblejtyr
 
Jinja2 filters
Jinja2 filtersJinja2 filters
Jinja2 filtersjtyr
 
Templating in ansible
Templating in ansibleTemplating in ansible
Templating in ansiblejtyr
 
Make the prompt great again
Make the prompt great againMake the prompt great again
Make the prompt great againjtyr
 
Best practices for ansible roles development
Best practices for ansible roles developmentBest practices for ansible roles development
Best practices for ansible roles developmentjtyr
 
Overcoming problems of the standard Ansible inventory file
Overcoming problems of the standard Ansible inventory fileOvercoming problems of the standard Ansible inventory file
Overcoming problems of the standard Ansible inventory filejtyr
 
Automation and Ansible
Automation and AnsibleAutomation and Ansible
Automation and Ansiblejtyr
 
LEGO IR Controller
LEGO IR ControllerLEGO IR Controller
LEGO IR Controllerjtyr
 

Más de jtyr (11)

Managing VMware VMs with Ansible
Managing VMware VMs with AnsibleManaging VMware VMs with Ansible
Managing VMware VMs with Ansible
 
How does Ansible's agentless architecture work?
How does Ansible's agentless architecture work?How does Ansible's agentless architecture work?
How does Ansible's agentless architecture work?
 
Variable precedence: Where should I put a variable?
Variable precedence: Where should I put a variable?Variable precedence: Where should I put a variable?
Variable precedence: Where should I put a variable?
 
Managing multiple environments with Ansible
Managing multiple environments with AnsibleManaging multiple environments with Ansible
Managing multiple environments with Ansible
 
Jinja2 filters
Jinja2 filtersJinja2 filters
Jinja2 filters
 
Templating in ansible
Templating in ansibleTemplating in ansible
Templating in ansible
 
Make the prompt great again
Make the prompt great againMake the prompt great again
Make the prompt great again
 
Best practices for ansible roles development
Best practices for ansible roles developmentBest practices for ansible roles development
Best practices for ansible roles development
 
Overcoming problems of the standard Ansible inventory file
Overcoming problems of the standard Ansible inventory fileOvercoming problems of the standard Ansible inventory file
Overcoming problems of the standard Ansible inventory file
 
Automation and Ansible
Automation and AnsibleAutomation and Ansible
Automation and Ansible
 
LEGO IR Controller
LEGO IR ControllerLEGO IR Controller
LEGO IR Controller
 

Último

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 

Último (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 

Ansible Callback Plugins Guide

  • 2. About me ● Using Ansible since 2014 ● Ansible contributor ○ Modules: yum_repository , jenkins_plugin , ldap_attr, ldap_entry ○ Jinja2 filter: comment ○ Bug fixing (mount module) ○ Code reviews ● Author of more than 100 publicly available Ansible roles ○ https://github.com/jtyr ○ https://galaxy.ansible.com/jtyr
  • 3. What are callback plugins?
  • 4. What are callback plugins? ● Control most of the Ansible's output by responding to events ● Over 30 built-in plugins ○ Format output (json, yaml, debug, dense, minimal, unixy, stderr, null, ...) ○ Send output to other system (slack, jabber, mail, grafana_annotations, logstash, splunk, ...)
  • 6. How to use it? # Command line export ANSIBLE_STDOUT_CALLBACK=json ansible-playbook -i hosts site.yaml export ANSIBLE_LOAD_CALLBACK_PLUGINS=yes export ANSIBLE_STDOUT_CALLBACK=json ansible all -i hosts -m ping # ansible.cfg [defaults] callback_plugins = path/to/my/plugins Stdout_callback = json bin_ansible_callbacks = True
  • 7. How to use it? # Query variable from Ansible export ANSIBLE_LOAD_CALLBACK_PLUGINS=yes export ANSIBLE_STDOUT_CALLBACK=json export MYHOST=host01 ansible $MYHOST -i hosts -m debug -a 'var=ansible_host' | jq ".plays[0].tasks[0].hosts["$MYHOST"].ansible_host" | sed -e 's/^"//' -e 's/"$//' -e 's/^null$//'
  • 8. How does it work?
  • 9. ● Two versions of callback functions ○ Functions for Ansible 2.x prefixed with v2_ ● Three categories of callback functions ○ Playbook (playbook, play, task) ○ Runner (module) ○ Other (any, diff) How does it work?
  • 10. How does it work? # Playbook v2_playbook_on_cleanup_task_start v2_playbook_on_handler_task_start v2_playbook_on_import_for_host v2_playbook_on_include v2_playbook_on_no_hosts_matched v2_playbook_on_no_hosts_remaining v2_playbook_on_notify v2_playbook_on_not_import_for_host v2_playbook_on_play_start v2_playbook_on_start v2_playbook_on_stats v2_playbook_on_task_start v2_playbook_on_vars_prompt # Runner v2_runner_item_on_failed v2_runner_item_on_ok v2_runner_item_on_skipped v2_runner_on_async_failed v2_runner_on_async_ok v2_runner_on_async_poll v2_runner_on_failed v2_runner_on_ok v2_runner_on_skipped v2_runner_on_start v2_runner_on_unreachable v2_runner_retry # Other v2_on_any v2_on_file_diff
  • 11. How does it work? # callback_plugins/mycallback.py from ansible.plugins.callback import CallbackBase class CallbackModule(CallbackBase): CALLBACK_VERSION = 2.0 CALLBACK_TYPE = 'stdout' CALLBACK_NAME = ' mycallback' def v2_playbook_on_play_start(self, play): self._display.display("Starting play") def v2_playbook_on_stats(self, stats): self._display.display("Showing stats") $ ANSIBLE_STDOUT_CALLBACK= mycallback ansible-playbook -i localhost, site.yaml Starting play Showing stats
  • 13. ● Allows to use Ansible as a reporting tool ● Turns Ansible's output into CSV (Comma-Separated Values) ● Meant to be used by plays producing output to STDOUT (raw or shell module) ● The output must be either valid CSV format or it has to fail (no empty string) ● Unsuccessful PR #65801 CSV callback plugin
  • 14. CSV callback plugin # Default usage $ export ANSIBLE_CALLBACK_PLUGINS=callback_plugins $ export ANSIBLE_LOAD_CALLBACK_PLUGINS=1 $ export ANSIBLE_STDOUT_CALLBACK=csv $ ansible all -i localhost, -c local -m raw -a "echo ','" host,status localhost,ok TOTAL: 1 OK: 1 FAILED: 0 UNREACHABLE: 0
  • 15. CSV callback plugin # Customized usage [callback_csv] fields = [ {'placeholder': '%n', 'name': 'name'}, {'name': 'host', 'in_message': True}, {'placeholder': '%h', 'name': 'ip', 'in_message': True}, {'name': 'distro', 'in_message': True}, {'name': 'version', 'in_message': True}, {'name': 'cpu', 'in_message': True}, {'name': 'mem', 'in_message': True}, {'placeholder': '%s', 'name': 'status'}, {'placeholder': '%g', 'name': 'group'}, {'placeholder': '%v', 'variable': 'inventory_hostname_short', 'name': 'short'}] format_ok = %n,%m,%s,%g,%v
  • 16. CSV callback plugin # Customized usage $ export ANSIBLE_CALLBACK_PLUGINS=callback_plugins $ export ANSIBLE_LOAD_CALLBACK_PLUGINS=1 $ export ANSIBLE_STDOUT_CALLBACK=csv $ ansible all -i localhost, -c local -m raw -a "echo $(cat ./facts.sh | base64 -w0) | base64 -di > ./facts.sh && chmod +x ./facts.sh && ./facts.sh" name,hostname,ip,distro,version,cpu,mem,status,group,short localhost,my.hostname.com,192.168.1.123,centos,7,4,3881048,ok,ungrouped,localhost TOTAL: 1 OK: 1 FAILED: 0 UNREACHABLE: 0
  • 17. CSV callback plugin # Customized usage - Windows $ export ANSIBLE_CALLBACK_PLUGINS=callback_plugins $ export ANSIBLE_LOAD_CALLBACK_PLUGINS=1 $ export ANSIBLE_STDOUT_CALLBACK=csv $ ansible host01 -i hosts -m win_shell -a '$myhost = [System.Net.Dns]::GetHostByName((hostname)).HostName; $ip = Test-Connection -ComputerName (hostname) -Count 1 | Select IPV4Address | %{$_.IPV4Address} | %{$_.IPAddressToString}; $version = Get-CimInstance Win32_OperatingSystem | select Version | %{$_.Version}; $cpu = Get-WmiObject –class Win32_ComputerSystem | select NumberOfProcessors | %{$_.NumberOfProcessors}; $mem = Get-WmiObject –class Win32_ComputerSystem | select TotalPhysicalMemory | %{$_.TotalPhysicalMemory}; "{0},{1},windows,{2},{3},{4}" -f $myhost.ToLower(), $ip, $version, $cpu, [int]($mem/1000)' name,hostname,ip,distro,version,cpu,mem,status,group,short host01,my.hostname.com,192.168.1.456,windows,10.0.18363,4,3881048,ok,ungrouped,host01 TOTAL: 1 OK: 1 FAILED: 0 UNREACHABLE: 0
  • 18. Demo
  • 19. Thank you for your attention! Questions?