ansible notes

From thelinuxwiki
Revision as of 15:44, 16 April 2025 by Nighthawk (Talk | contribs)

Jump to: navigation, search

https://www.redhat.com/en/services/training/ex457-red-hat-certified-specialist-in-ansible-network-automation-exam?section=objectives

Contents

installation

pip install ansible

variables

types

get var type

Template:myvar

Adding variables to inventory

setting variables

setfact

- name: set a var
  set_fact:
    somevar: "foo"

Connecting to hosts

Secure credentials with Ansible Vault

common commands

$ ansible-vault create <path to file>

$ ansible-vault edit <path to file>

$ ansible-vault rekey <path to file>

$ ansible-vault view <path to file>

default directories for encrypted vault files

group_vars

host_vars

use hostname/groupname from inventory file as name of the file

connection plugins

variable ansible_connection

listing connection types/plugins

$ ansible-doc -t connection -l
kubectl      Execute tasks in pods running on Kubernetes                                                                                      
libvirt_lxc  Run tasks in lxc containers via libvirt                                                                                          
chroot       Interact with local chroot                                                                                                       
psrp         Run tasks over Microsoft PowerShell Remoting Protocol                                                                            
network_cli  Use network_cli to run command on network appliances                                                                             
vmware_tools Execute tasks inside a VM via VMware Tools                                                                                       
ssh          connect via ssh client binary                                                                                                    
httpapi      Use httpapi to run command on network appliances                                                                                 
docker       Run tasks in docker containers                                                                                                   
...

playbooks

examples

- name: My first play
 hosts: myhosts
 tasks:
  - name: Print wall message
    ansible.builtin.command: /usr/bin/wall hello


- name: output test 
 hosts: myhosts
 tasks:
  - name: run uname thru awk 
    ansible.builtin.shell:  /usr/bin/uname -a | awk '{print $NF}'
    register: results
  - debug:
      var: results.stdout
$ ansible-playbook -i inventory.ini shelltest.yaml
PLAY [output test] ********************************************************************************************************************************

TASK [run uname thru awk] ************************************************************************************************************************* changed: [10.0.0.15]
TASK [debug] ************************************************************************************************************************************** ok: [10.0.0.15] => { "results.stdout": "GNU/Linux" }
PLAY RECAP **************************************************************************************************************************************** 10.0.0.15  : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

programming

conditionals

loops

jinja2

Templating (Jinja2)

output

parsing json

use community.general.json_query which uses jmespath

Selecting JSON data: JSON queries

writing to files

- name: output to file 
  lineinfile: 
    insertafter: EOF
    dest: "out.txt"
    line: "foo bar"

modules and plugins

Using Ansible modules and plugins

Difference between Modules and Plugins in Ansible

collections

Using collections Collections are a distribution format for Ansible content that can include playbooks, roles, modules, and plugins. As modules move from the core Ansible repository into collections, the module documentation will move to the collections pages.

use cases

network/security automation

platform modules (Maintained by Ansible Network Team)

checkpoint modules (gaia / mgmt)

fortinet

fortinet

Jinja Filters and Functions

paloalto

cloud

kvm

KVM libvirt module

Ansible libvirt dynamic inventory

Connecting the Serial Console for the Guest Virtual Machine


Ansible export facts to simple csv file