Inventory Parameters
inventory is called as collection of hosts
inventory parameters are variables or settings you define in your inventory file to control how hosts and groups behave during playbook execution.
ansible_host # Hostname or IP address to SSH to
ansible_port # Port to SSH to
ansible_user # User to SSH as
ansible_password # Password to use for SSH authentication
ansible_ssh_private_key_file # SSH private key to use for SSH authentication
ansible_become=true
ansible_become_user=root
ansible_become_method=sudo(or)su
localhost ansible_connection=local
Custom variable
web1 app_port=8080 env=prod
ansible_connection
Ansible supports multiple transports to connect hosts
The default transport, smart
Ansible will check whether the locally installed SSH client supports a feature called ControlPersist. If the SSH client supports ControlPersist, Ansible will use the local SSH client.
If not, the smart transport will fall back to using a Python-based SSH client library called Paramiko.
ansible_shell_type
Ansible works by making SSH connections to remote machines and then invoking scripts. By default, Ansible assumes that the remote shell is the Bourne shell located at /bin/sh, and will generate the appropriate command-line parameters that work with that. It creates temporary directories to store these scripts.
Ansible also accepts csh, fish, and (on Windows) powershell as valid values for this parameter. Ansible doesn’t work with restricted shells.
ansible_python_interpreter
Ansible needs to know the location of the Python interpreter on the remote machine
ansible_python_interpreter="/usr/bin/env python3"
ansible_*_interpreter
If you are using a custom module that is not written in Python, you can use this parameter to specify the location of the interpreter
eg
[web]
web1 ansible_host=192.168.1.10 ansible_user=ec2-user ansible_port=22
[web]
web1
web2
[web:vars]
ansible_user=ec2-user
ansible_ssh_private_key_file=/home/user/key.pem
eg
web1 app_port=8080 env=prod
Pattern Matching Inventory
$ ansible web -m ping
$ ansible 'web:&prod' -m ping
$ ansible 'web:!db' -m ping
Operators
: → OR
& → AND
! → NOT
$ ansible-inventory --list
$ ansible-inventory --graph
Ansible automatically defines a group called all (or *)
$ ansible all -a "date"
or
$ ansible '*' -a "date"
Bill Baker of Microsoft came up with the distinction between treating servers as pets versus treating them like cattle.
The “cattle” approach to servers is much more scalable
20 servers are named web1.example.com, web2.example.com ... and so on
[web]
web[1:20].example.com
web-a.example.com, web-b.example.com, and so on....
[web]
web-[a:t].example.com
Ansible will let you add hosts and groups to the inventory during the execution of a playbook.
Adding Entries at Runtime with add_host and group_by
This is useful when managing dynamic clusters, such as Redis Sentinel.
add_host
The add_host module adds a host to the inventory; this is useful if you’re using Ansible to provision new virtual machine instances
- name: Add the host
add_host
name: hostname
groups: web,staging
myvar: myval
group_by
Ansible’s group_by module allows you to create new groups while a playbook is executing.
Any group you create will be based on the value of a variable that has been set on each host, which Ansible refers to as a fact.
- name: Create groups based on Linux distribution
group_by:
key: "{{ ansible_facts.distribution }}"
No comments:
Post a Comment