Facts and conditionals
Gathering info
Gathering facts may be costly – use setup for specific machines you want to check. * If gather_facts is enabled, we can use it to verify system facts * And set the task blocks accordingly * Use when for conditionals * Or variables with the assert module
# Example Gathering info
gather_facts: true
task:
- name: Veryfie that this si the debian dirsto
when: "'Debian' is ansiable_fact['distribiution']"
block:
- name: This is Debian
- ansible.builtin.debug:
- msg: This is Debian
User defined facts
- Create
/etc/ansible/facts.d
on the managed nodes- The file has tave to have .fact extentsion - Has to be in the Json Format or .ini
- Theare stored in
ansible_facts[’ansible_local’]
Managing Software and repos
U can provide a list to the package argument instead of looping over the packages
Downloading the local repo
Ansible doesn't provide the module for creating the repos u have to do it manually
Also there's a rmp_key
module for downloading the gpg keys
# Example downloading and creating a local repo
- name: make directory
file:
path: /var/ftp/repo
state: directory
- name: download packages
yum:
name: nmap
download_only: yes
download_dir: /var/ftp/repo
- name: createrepo
command: createrepo /var/ftp/repo
Managing subscriptions
There are two models that deal with the subscription management
red hat_subscription
Subscription and Registration in one taskrhsm_repository
add subscription manager repos
Example Using subscription manager to set up Ansible
Commands
Defining Targets
You can also specify multiple hosts and groups by separating them with colons:
# Exmaple Test Host connection
ansible server1:server2:dbservers -i inventory -m ping
ansible all -i inventory -m ping
Runing modules
- Probably u will need to use the setup module
- To execute a module with arguments, include the -a flag followed
by the appropriate options in double quotes
- U can use whatever the cmd with -a
ansible -i hosts servers -m setup -a 'filter: ansible_distribution'
ansible servers -i hosts -m file -a 'state: directory'
ansible <target group> -i inventory -m module -a "module options"
Setup module
- pages of information about the server that can be used as variable in the playbooks later
Ansible Debuging
U can skip the deubiging message by using etiher the:
* Verbosity Level in ur playbook Just use -vvv
flag
- U can also prettyfie the ansible message in ansible.cfg
Useful modules
- Uri
- To perform quick checks and so on
- Stat
- stat the files and dirs
- Assert
Name | Use |
---|---|
command | Runs arbitrary commands but not using a shell |
shell | Runs arbitrary commands using a shell |
raw | Runs commands directly on top of SSH without using Python |
copy | Copies files or lines of text to files |
yum | Manages packages on RHEL family-managed hosts |
service | Manages the current state of systemd and system-V services |
ping | Checks whether managed hosts are in a manageable state |
# raw is used to do it over ssh without the python so can be used for installing things for ansible
ansible -u root -i inventory ansible3 --ask-pass -m raw -a “yuminstall python3"
Execution Modes
Check Mode
Some modules don't support the check mode
check_mode: yes
yes can be used with any task it also overwrites the entire playbook
Diff mode --diff
Display changes in file contents
Mange task tags and list task
List the task
Tags
- U can list tags with just --list-tags
- If u set the tag to
never
it want run unless specified (useful for debugging)