0% found this document useful (0 votes)
50 views11 pages

Ansible Master

The document provides various links related to Ansible documentation, modules, tutorials and best practices. It also includes examples of running ad-hoc commands on network devices to collect facts and show specific command output using Ansible. The examples demonstrate using Ansible with network devices by specifying the inventory, connection parameters, modules and commands.

Uploaded by

Jerin Varghese
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views11 pages

Ansible Master

The document provides various links related to Ansible documentation, modules, tutorials and best practices. It also includes examples of running ad-hoc commands on network devices to collect facts and show specific command output using Ansible. The examples demonstrate using Ansible with network devices by specifying the inventory, connection parameters, modules and commands.

Uploaded by

Jerin Varghese
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Ansible Links

http://docs.ansible.com/ansible/latest/index.html — Ansible Documentation

http://docs.ansible.com/ansible/latest/modules_by_category.html — Module categories

http://docs.ansible.com/ansible/latest/list_of_all_modules.html — All Ansible Modules

http://docs.ansible.com/ansible/latest/YAMLSyntax.html — YAML Syntax … read this a few times, it’ll come


in handy
http://docs.ansible.com/ansible/latest/playbooks.html — Ansible Playbooks

https://zaiste.net/posts/ansible_101/ — A nice 5 minute hands-on intro to Ansible

https://gist.github.com/andreicristianpetcu/b892338de279af9dac067891579cad7d — Ansible cheatsheet, a great


reference point for just about any bit of Ansible code you’d need to write
https://www.ansible.com/blog/ansible-best-practices-essentials — Best practices

https://serversforhackers.com/c/an-ansible2-tutorial — Detailed Ansible tutorial with explaination and code.

https://gist.github.com/phred/2897937 — pedantically commented playbook

Run Your First Command


ansible all -i vyos.example.net, -c ansible.netcommon.network_cli -u my_vyos_user -k -m vyos.vyos.vyos_facts -e
ansible_network_os=vyos.vyos.vyos

The flags in this command set seven values:


 the host group(s) to which the command should apply (in this case, all)
 the inventory (-i, the device or devices to target - without the trailing comma -i points to an inventory file)
 the connection method (-c, the method for connecting and executing ansible)

Modules
 cli_backup module – Back up device configuration from network devices over
network_cli
 cli_command module – Run a cli command on cli-based network devices
 cli_config module – Push text based configuration to network devices over network_cli
 grpc_config module – Fetch configuration/state data from gRPC enabled target hosts.
 grpc_get module – Fetch configuration/state data from gRPC enabled target hosts.
 net_get module – Copy a file from a network device to Ansible Controller
 net_ping module – Tests reachability using ping from a network device
 net_put module – Copy a file from Ansible Controller to a network device
 netconf_config module – netconf device configuration
 netconf_get module – Fetch configuration/state data from NETCONF enabled network
devices.
 netconf_rpc module – Execute operations on NETCONF enabled network devices.
 network_resource module – Manage resource modules
 restconf_config module – Handles create, update, read and delete of configuration data
on RESTCONF enabled devices.
 restconf_get module – Fetch configuration/state data from RESTCONF enabled devices.
 telnet module – Executes a low-down and dirty telnet command

Become Plugins
 enable become – Switch to elevated permissions on a network device
Cache Plugins
 memory cache – RAM backed, non persistent cache.
Cliconf Plugins
 default cliconf – General purpose cliconf plugin for new platforms
Connection Plugins
 grpc connection – Provides a persistent connection using the gRPC protocol
 httpapi connection – Use httpapi to run command on network appliances
 libssh connection – Run tasks using libssh for ssh connection
 netconf connection – Provides a persistent connection using the netconf protocol
 network_cli connection – Use network_cli to run command on network appliances
 persistent connection – Use a persistent unix socket for connection
Filter Plugins
 comp_type5 filter – The comp_type5 filter plugin.
 hash_salt filter – The hash_salt filter plugin.
 parse_cli filter – parse_cli filter plugin.
 parse_cli_textfsm filter – parse_cli_textfsm filter plugin.
 parse_xml filter – The parse_xml filter plugin.
 pop_ace filter – Remove ace entries from a acl source of truth.
 type5_pw filter – The type5_pw filter plugin.
 vlan_expander filter – The vlan_expander filter plugin.
 vlan_parser filter – The vlan_parser filter plugin.
Httpapi Plugins
 restconf httpapi – HttpApi Plugin for devices supporting Restconf API
Netconf Plugins
 default netconf – Use default netconf plugin to run standard netconf commands as per
RFC

 the user (-u, the username for the SSH connection)


 the SSH connection method (-k, please prompt for the password)
 the module (-m, the Ansible module to run, using the fully qualified collection name (FQCN))
All Modules
https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html
 ios_facts – Collect facts from remote devices running Cisco IOS
https://docs.ansible.com/ansible/2.9/modules/ios_facts_module.html#ios-facts-module

Ansible Galaxy
https://galaxy.ansible.com/ui/repo/published/ansible/netcommon/?
extIdCarryOver=true&sc_cid=701f2000001OH7YAAW

 an extra variable ( -e, in this case, setting the network OS value)

ADHOC COMMAND
IOS_FACTS
(venv-ansible) jerin@jerin:/$ ansible all -i devnetsandboxiosxe.cisco.com,192.168.234.135, -c
ansible.netcommon.network_cli -u jerin -k -m cisco.ios.ios_facts -e ansible_network_os=cisco.ios.ios
SSH password:
[WARNING]: ansible-pylibssh not installed, falling back to paramiko
192.168.234.135 | SUCCESS => {
"ansible_facts": {
"ansible_net_api": "cliconf",
"ansible_net_gather_network_resources": [],
"ansible_net_gather_subset": [
"default"
],
"ansible_net_hostname": "R1",
"ansible_net_image": "tftp://255.255.255.255/unknown",
"ansible_net_iostype": "IOS",
"ansible_net_model": "7206VXR",
"ansible_net_operatingmode": "autonomous",
"ansible_net_python_version": "3.10.12",
"ansible_net_serialnum": "4279256517",
"ansible_net_system": "ios",
"ansible_net_version": "15.2(4)M7",
"ansible_network_resources": {}
},
"changed": false
}
devnetsandboxiosxe.cisco.com | SUCCESS => {
"ansible_facts": {
"ansible_net_api": "cliconf",
"ansible_net_gather_network_resources": [],
"ansible_net_gather_subset": [
"default"
],
"ansible_net_hostname": "iosxe",
"ansible_net_image": "bootflash:packages.conf",
"ansible_net_iostype": "IOS-XE",
"ansible_net_model": "C8000V",
"ansible_net_operatingmode": "autonomous",
"ansible_net_python_version": "3.10.12",
"ansible_net_serialnum": "9OBXJHNNU5V",
"ansible_net_system": "ios",
"ansible_net_version": "17.12.02",
"ansible_network_resources": {}
},
"changed": false

Command line

(venv-ansible) jerin@jerin:~$ ansible all -i devnetsandboxiosxe.cisco.com,192.168.234.135, -c


ansible.netcommon.network_cli -u jerin -k -m cisco.ios.ios_command -e ansible_network_os=cisco.ios.ios -a
"commands='show version | i uptime'"
SSH password:
[WARNING]: ansible-pylibssh not installed, falling back to paramiko
192.168.234.135 | SUCCESS => {
"changed": false,
"stdout": [
"R1 uptime is 2 hours, 54 minutes"
],
"stdout_lines": [
[
"R1 uptime is 2 hours, 54 minutes"
]
]
}
devnetsandboxiosxe.cisco.com | SUCCESS => {
"changed": false,
"stdout": [
"iosxe uptime is 2 days, 10 minutes"
],
"stdout_lines": [
[
"iosxe uptime is 2 days, 10 minutes"
]

Creating host’s file


(venv-ansible) jerin@jerin:~$ touch hosts
(venv-ansible) jerin@jerin:~$ cat hosts
192.168.234.135
devnetsandboxiosxe.cisco.com

venv-ansible) jerin@jerin:~$ ansible all -i hosts -c ansible.netcommon.network_cli -u jerin -k -m


cisco.ios.ios_facts -e ansible_network_os=cisco.ios.ios
SSH password:
[WARNING]: ansible-pylibssh not installed, falling back to paramiko
192.168.234.135 | SUCCESS => {
"ansible_facts": {
"ansible_net_api": "cliconf",
"ansible_net_gather_network_resources": [],
"ansible_net_gather_subset": [
"default"
],
"ansible_net_hostname": "R1",
"ansible_net_image": "tftp://255.255.255.255/unknown",
"ansible_net_iostype": "IOS",
"ansible_net_model": "7206VXR",
"ansible_net_operatingmode": "autonomous",
"ansible_net_python_version": "3.10.12",
"ansible_net_serialnum": "4279256517",
"ansible_net_system": "ios",
"ansible_net_version": "15.2(4)M7",
"ansible_network_resources": {}
},
"changed": false

Ansible Configuration Settings


https://docs.ansible.com/ansible/latest/reference_appendices/config.html

The configuration file

Changes can be made and used in a configuration file which will be searched for in the following order: PRIORITY
ORDER
 ANSIBLE_CONFIG (environment variable if set)
 ansible.cfg (in the current directory)
 ~/.ansible.cfg (in the home directory)
 /etc/ansible/ansible.cfg

Export ANSIBLE_CONFIG
(venv-ansible) jerin@jerin:~$ export ANSIBLE_CONFIG=/home/jerin/ansible.cfg
(venv-ansible) jerin@jerin:~$ echo $ANSIBLE_CONFIG
/home/jerin/ansible.cfg

Inventory folder creation.


Inside config file change path of inventory

Magic variables
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html

ansible_config_file
The full path of used Ansible configuration file
Lab_hosts is configured with the local under ansible_connection module

Calling the same test_host for collecting the facts


Starting with Playbook

Inventory file creation. In INI

Using CLI in Ansible


ansible_connection: ansible.netcommon.network_cli
ansible_network_os: cisco.ios.ios
ansible_user: myuser
ansible_password: !vault...
ansible_become: true
ansible_become_method: enable
ansible_become_password: !vault...
ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q bastion01"'
Converting INI to YML inventory file conversion
(venv-ansible) jerin@jerin:~$ ansible-inventory -i inventory/lab_hosts -y --list
all:
children:
routers:
children:
vios_group:
hosts:
R1:
ansible_become: 'true'
ansible_become_method: enable
ansible_become_password: jerin123
ansible_connection: ansible.netcommon.network_cli
ansible_host: 192.168.234.135
ansible_network_os: cisco.ios.ios
ansible_password: jerin123
ansible_user: jerin
R2:
ansible_become: 'true'
ansible_become_method: enable
ansible_become_password: jerin123
ansible_connection: ansible.netcommon.network_cli
ansible_host: 192.168.234.136
ansible_network_os: cisco.ios.ios
ansible_password: jerin123
ansible_user: jerin
R3:
ansible_become: 'true'
ansible_become_method: enable
ansible_become_password: jerin123
ansible_connection: ansible.netcommon.network_cli
ansible_host: 192.168.234.137
ansible_network_os: cisco.ios.ios
ansible_password: jerin123
ansible_user: jerin

Playbook Variables

Not all strings are valid Ansible variable names. A variable name can only include letters, numbers, and
underscores. Python keywords or playbook keywords are not valid variable names. A variable name
cannot begin with a number.

You might also like