DevOps Classroom Series – 11/Aug/2020

Ansible Behavior Inventory Parameters

  • Refer Here for official docs
  • Some Inventory Parameters
    • ansible_host: This will be host name or ipaddress which ypu can set to the host
      • Consider this inventory file
      myfavoriteserver ansible_host="172.31.4.122"
      yourfavoriteserver.com ansible_host="localhost"
      
      • Now lets run simple ansible ping adhoc command ansible -i hosts -m ping all Preview
    • ansible_port: Specifies the port number that Ansible will use to connect to inventory host, Sample inventory
    localhost ansible_port=8922
    
    • ansible_user: Specifies the username that ansible will connect to the inventory host.
    • ansible_ssh_pass: Used to provide the ssh password to authenticate with inventory host.
    192.168.10.23 ansible_user="admin" ansible_ssh_pass="admin123"
    
    • ansible_ssh_private_key_file: Used to specify the SSH private key file to connect to inventory host
    34.25.15.14 ansible_user="ec2_user" ansible_ssh_private_key_file="./ansible.pem"
    
    • ansible_ssh_common_args: This defines ssh arguments to append to default arguments for ssh, sftp and scp
    • ansible_sftp_extra_args: Used to specify additional sftp arguments
    • ansible_scp_extra_args: Used to specify additional scp arguments
    • ansible_ssh_extra_args: Used to specify additional ssh arguments
    • ansible_ssh_executable:
    • ansible_become: This defines whether privilege escalation(sudo or otherwise) should be with this host
    • ansible_become_method: Which method to use for privilege and can be one of sudo, su, pfexec,doas,dzdo,pbrun or ksu
    • ansible_become_user: This is user to become through privilege escalation
    • ansible_become_pass: This is password for become user.
    • ansible_sudo_pass: This is sudo password to use
    • ansible_connection: This is the connection type of the host. values are one of ssh,local,smart,docker, paramiko or winrm. Default is smart
    • ansible_docker_extra_args:
    • ansible_shell_type: Default shell of the inventory host
    • ansible_shell_executable: Default shell types executable
    • ansible_python_interpreter: This is used to manually set the path of python interpreter on a given host in the inventory
      • Consider the below inventory
      [python3]
      172.31.3.192 ansible_python_interpreter="/usr/bin/python3"
      [python2]
      localhost
      
      • Now run simple adhoc command “`ansible -i diffpythonhosts -m ping all Preview

Inventory Limiting

  • Consider the following inventory
[webserver]
172.31.3.192 ansible_python_interpreter="/usr/bin/python3"
172.31.4.122

[local]
localhost
  • Now we have written a playbook or adhoc command for
ansible -m ping all
  • Playbook is
---
- name: just fun
  hosts: all
  tasks:
    - name: ping host
      ping:
    - name: print host
      debug:
        var: inventory_host
  • Now executing this playbook will result as shown below Preview
  • Now we can supply –limit as shown below Preview Preview

Ansible Variables in separate files

  • Rather than writing variable data in inventory, it is a best practice to write them separately in a file, for that we need to understand how to create files for group variables and host variables
  • Lets assume this inventory and create it in a file called as hosts
[webservers]
172.31.4.122
172.31.3.192

[dbservers]
localhost

  • Now the following playbook is created
---
- name: just fun
  hosts: all
  tasks:
    - name: ping host
      ping:
    - name: print host
      debug:
        var: inventory_hostname
    - name: print variable
      debug:
        var: from_where
  • Lets add the following entries in group_vars/webservers.yaml
---
from_where: group_webservers
  • In group_vars/dbservers.yaml
---
from_where: group_dbservers

  • In host_vars/172.31.3.192.yaml
---
ansible_python_interpreter: "/usr/bin/python3"
from_where: hosts_172.31.3.192

  • Lets look at folder structure Preview
  • The convention is in the folder wherever you have inventory in the same folder create folder named "group_vars" and the file name will be group_vars/<groupname>.yaml. Similarly for host variable the file names will be host_vars/<hostname>.yaml
  • lets execute playbook Preview

Next Steps

  • Steps while parsing playbooks
  • Order of operations
  • Play behavior directives
  • Execution strategies

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube