How to Create Ansible Custom Modules using Python |Passing Arguments to AnsibleModule

Published: 05 May 2024
on channel: NetworkEvolution
744
9

#ansibletutorial #ansibleautomation #netdevops #networkautomation #networkengineers #devops
𝗧𝗼 𝗔𝗰𝗰𝗲𝘀𝘀 𝘁𝗵𝗲 𝗙𝘂𝗹𝗹 𝗖𝗼𝗻𝘁𝗲𝗻𝘁 𝗘𝗻𝗿𝗼𝗹𝗹 𝗶𝗻 𝗼𝘂𝗿 𝗶𝗻-𝗱𝗲𝗽𝘁𝗵 𝗨𝗱𝗲𝗺𝘆 𝗖𝗼𝘂𝗿𝘀𝗲 (Mastering Ansible Automation):
https://www.udemy.com/course/ansible-...

Playlist: Ansible Leraning For Network Engineers
   • Network Automation using Ansible Part...  


𝗣𝗹𝗮𝘆𝗹𝗶𝘀 𝘁: 𝗣𝘆𝘁𝗵𝗼𝗻 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗡𝗲𝘁𝘄𝗼𝗿𝗸 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝘀(𝟭𝟮𝟬+ 𝗩𝗶𝗱𝗲𝗼𝘀)
   • Welcome to NetworkEvolution !!  

☸𝗣𝗹𝗲𝗮𝘀𝗲 𝗳𝗼𝗹𝗹𝗼𝘄 𝗯𝗲𝗹𝗼𝘄 𝗚𝗶𝘁𝗛𝘂𝗯 𝗣𝗮𝗴𝗲 𝗳𝗼𝗿 𝘁𝗵𝗲 𝗹𝗮𝘁𝗲𝘀𝘁 𝗰𝗼𝗱𝗲𝘀:
https://github.com/network-evolution

script
from ansible.module_utils.basic import AnsibleModule
import csv

def main():
module = AnsibleModule(
argument_spec = dict(
device_facts = dict(type='dict', required=True),
dest_csv_path = dict(type='str', required=False, default = '/home/dev/ansible_masterclass/21_custom_module' ),
dest_csv_file = dict(type='str', required=False, default='facts_file.csv')
)
)

try:
device_facts = module.params['device_facts']
sorted_device_facts = dict(sorted(device_facts.items()))
csv_filename = f"{module.params['dest_csv_path']}/{module.params['dest_csv_file']}"

csv_header = ["inventory_name","device_ip", "hostname", "model", "version", "serial_number"]

with open(csv_filename, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_header)
writer.writeheader()
for device, details in sorted_device_facts.items():
writer.writerow({
"inventory_name": device,
"device_ip": details['ansible_host'],
"hostname": details['ansible_facts']['net_hostname'],
"model": details['ansible_facts']['net_model'],
"version": details['ansible_facts']['net_version'],
"serial_number": details['ansible_facts']['net_serialnum'],
})

module.exit_json(changed=True, output="Saved content to CSV")
except Exception as e:
module.fail_json(msg="Module Failed", exception= str(e))
if _name_ == '__main__':
main()


this video demonstrates how to create ansible custom modules using python
create csv in ansible using ansible custom module
gather device facts and create a dictionary
create csv using ansible
ansible library folder for custom module
ansible custom module name example
sample custom module
ansible custom module dioumentation and return values
import modules for generating custom modules
how to create csv file using ansible custom module
add facts to csv file using ansible
create module parameter using the instance AnsibleModule
how to pass module arguments to task using ansible vars
module = AnsibleModule()
set required mandatory option for module args
argument_spec in ansible custom module
custom module args example ansible
ansible module.params
ansible custom module required and default value
ansible custom module exit json
return the value from ansible custom module python
create a task using custom module example
ansible tutorial
ansible python script for advanced usecases
write your own module
ansible custom modules
ansible custom modules path
ansible automation platform custom modules
ansible where to put custom modules
ansible custom module examples
ansible expect module examples
ansible custom module location
ansible custom module python
ansible custom module access variables
ansible custom module access facts
ansible custom module arguments
ansible custom module path
ansible install custom module
ansible custom module bash
custom ansible modules
ansible custom facts example
creating custom ansible modules
ansible custom module debug print
ansible custom module documentation
ansible custom module debug
ansible custom modules examples
ansible custom module fail_json
ansible custom facts module
ansible custom module set fact
ansible custom module not found
ansible custom module multiple files
ansible custom module example
custom ansible module hello world
ansible custom module error handling
how to create custom modules in ansible
ansible add custom module
custom modules in ansible
ansible custom lookup module
ansible custom module print message
ansible custom module namespace
ansible custom module no module named
ansible custom module output
ansible custom module print output
ansible custom module powershell
ansible use custom module
ansible import custom module
ansible custom module stdout
ansible custom module tutorial
ansible custom module template
ansible custom module unable to import due to invalid syntax
ansible custom module windows
ansible writing custom modules
ansible tutorial
learn custom module


Watch video How to Create Ansible Custom Modules using Python |Passing Arguments to AnsibleModule online, duration hours minute second in high quality that is uploaded to the channel NetworkEvolution 05 May 2024. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 744 times and liked it 9 visitors.