Inventory and configuration files Flashcards
Default inventory location
Default: /etc/ansible/hosts
Specified by cli: ansible -i
Can be set in ansible.cfg
Static vs Dynamic
Static:
-ini or yaml
- esy to manage for static config
Dynamic:
-executable (bash, python script etc
-script returns json containing inventory information
-good for use with cloud resources when sudden changes
Vars
Ansible recommends that vars not to be defined in inventory files:
- should be stored in yaml files located relative to inventory file
- group_vars dir
- host_vars dir
Conf files which are typically used for each project
ansible.cfg in project dir: [defaults] inventory = hosts.yaml remote_user = ansible host_key_checking = false forks = 5 [privilege_escalation] become = true become_method = sudo become_user = root become_ask_pass = false
Setup ansible user to be not asked for password while sudo
sudo visudo to update /etc/sudoers file
ansible ALL=(ALL) NOPASSWD:ALL
Typical yaml static inventory
--- all: hosts: children: media: hosts: media1: media2: webservers: hosts: 10.0.1.1: 10.0.1.2:
…
Show which conf file will be used
ansible –version
Show list of hosts
ansible all -i inventory/hosts.yaml –list-hosts
Create custom facts in facts.d dir
mkdir -p /etc/ansible/facts.d on a slave node- node1 vim /etc/ansible/facs.d/pref.fact [server] software=adobe [params] adobe_v=1.7.1
on a control node run:
ansible node1 -m setup -a “filter=ansible_local”
Possible location of conf files
ANSIBLE_CONFIG (conf variable)
ansible.cfg (current dir)
~/.ansible.cfg (in home dir)
/etc/ansible/ansible.cfg
ansible-config options
- ansible-config list -Print config options
- ansible-config dump -dumps config
- ansible-config view -view config file
Useful params in ansible.cfg
[defaults] inventory=inventory roles_path=/home/ansible/.ansible/roles forks = 10 remote_user = ansible host_key_checking = False log_path = /my/project/ansible-errors.log
[priviledge_escalation]
become = yes
become_method = sudo
become_user = root