Date created: 02/14/20 16:26:41. Last modified: 02/21/20 12:27:19

Ansible Notes

References:

Ansible network modules == https://docs.ansible.com/ansible/latest/modules/list_of_network_modules.html

ansible_network_os == https://github.com/ansible/ansible/blob/devel/docs/docsite/rst/network/user_guide/platform_index.rst

 

CLI Args:

ansible -vvvv -i hosts -u james.bensley -k -a "ls -l" linux_servers

# Ansible CLI args:
#
# -vvvv
# Verbosity level
#
# -i
# Inventory file name, or specify a comma seperated list of hostnames/IPs
#
# -u
# Username for SSH auth to remote device
#
# -k
# Prompt for password for SSH auth to remote device
#
# -c
# How to connect to the remote system (default=smart)
#
# -m
# Ansible module to use (default=command)
#
# -a
# Ansible module args
#
# -e
# Set additional key/value pairs like a network device os (-e "ansible_network_os=iosxr")
#
# Finish with hosts file group or match glob/pattern

 

Connection methods and jump hosts / bastion hosts:

# This will use the default connection method of "smart" which will in turn default to SSH,
# and requires sshpass to be installed, because SSH won't use a user supplied password (-k option)
# without sshpass:
ansible -i hosts -u james.bensley -k linux_servers -a "ls -l"

# To install sshpass on MacOS use:
# xcode-select --install
# brew install https://raw.githubusercontent.com/esolitos/homebrew-ipa/master/Formula/sshpass.rb

# One can explicitly chose ssh using "-c ssh":
ansible -i hosts -u james.bensley -k linux_servers -c ssh -a "ls -l"


# The network_cli connection type is for device with proprietary SSH CLIs like network devices.
# It doesn't require an SSH key for example:
ansible -i hosts lab --limit r1.lab -u james.bensley -k -m iosxr_facts -c network_cli -e "ansible_network_os=iosxr"
ansible -i ar1.lab, all -u james.bensley -k -m iosxr_command -a "commands='show ver'" -c network_cli -e "ansible_network_os=iosxr"
ansible -i rr0.lab, all -u james.bensley -k -m ios_command -a "commands='show ver'" -c network_cli -e "ansible_network_os=ios"
ansible -i me1.lab, all -u james.bensley -k -m ce_command -a "commands='display ver'" -c network_cli -e "ansible_network_os=ce"
ansible -i sr1.lab, all -u james.bensley -k -m sros_command -a "commands='show ver'" -c network_cli -e "ansible_network_os=sros"

# One can use paramiko as a wrapper to SSH, this is helpful if sshpass is unavailable (like on MacOS)
# however, paramiko can't work with jump hosts / bastion hosts:
ansible -i hosts -u james.bensley -k linux_servers -c paramiko -a "ls -l"

# One can set the default connection type for each hosts so that it doesn't need to specified on the CLI:
[linux_servers]
s1.a1.example.com ansible_connection=paramiko
s1.a2.example.com ansible_connection=paramiko


# A jump host / bastion host will be used automatically if already configured in ~/.ssh/config.
# It is also possible to specify an Ansible specific SSH config file by adding the following
# to ansible.cfg:
[ssh_connection]
ssh_args = -F /path/to/ansible_ssh.cfg