Section 4 Flashcards
Via Ansible facts show
hostname
distribution
ipv4
network interfaces
storage devices
size of /dev/sda1
version distribution
ansible_facts[‘hostname’]
ansible_facts[‘distribution’]
ansible_facts[‘default_ipv4’][‘address’]
ansible_facts[‘intefaces’]
ansible_facts[‘devices’]
ansible_facts[‘devices’][‘sda][‘partitions’][‘sda1’]
ansible_facts[‘distribution_version’]
show modules that are good for gathering facts
ansible-doc -l | grep fact
Ansible Facts vs. Injected Variable
ansible_fact <- New way of doing things
ansible_ <- injected variable, old way
ansible_fact[‘hostname’]
ansible_hostname
ansible_devices[‘sda’[‘partitions’][‘sda1’][‘size’]
ansible_default_ipv4.address <- you can use doted notation with fact or brackets
What is a disadvantage of gathering facts for during your playbooks? What can you do about this?
This can slow down your playbook
You can turn this off in the play head with:
gather_facts: no
Fact caching might be something you want to do which stores the facts so you don’t need to gather them with every playbook. How would you set this up?
What is the issue with this
dnf install redis
service redis start
pip install redis
ansible.cfg
[defaults]
gathering = smart
fact_caching = redis
fact_caching_timeout
This might not be best for you to use if you’re running playbooks that will do things based on changing faces like diskspace. You don’t want to rely on old cached information.
Create Custom Facts and store them on the managed hosts
This should give variables for web packages, ftp packages and then their services
Show how you would call that information in a playbook
Show Custom Facts
/etc/ansible/facts.d
[packages]
web_package = httpd
ftp_package = vsftpd
[services]
web_service = httpd
ftp_service = vsftpd
create a playbook to store them, file should end with .fact
The file should be called listing68.fact and stored in /etc/ansible/facts.d
{{ ansible_facts[‘ansible_local’][‘listing68’][‘packages’][‘web_package’] }}
ansible all -m setup -a ‘filter=ansible_local’
Create Cutsom Facts in under a group called software.
The facs should be for a package, service, state, and enabled = True.
Check if you can see the facts
Install a package Using all these custom facts
Use a playbook to copy the file over
vi custom.facts
[packages]
package = httpd
service = httpd
state = started
enabled = true
ansible all -m setup -a ‘filter=ansible_local’
vi install.yml
- name: install
hosts: all
tasks:
- name install
dnf:
name: “{{ ansible_facts[‘ansible_local’][‘custom’][‘software’][‘package’] }}”
state: “{{ ansible_facts[‘ansible_local’][‘custom’][‘software’][‘state’] }}”
- name: Start Service
service:
name: “{{ ansible_facts[‘ansible_local’][‘custom’][‘software’][‘service’] }}”
state: {{ ansible_facts[‘ansible_local’][‘custom’][‘software’][‘state’] }}
enabled: {{ ansible_facts[‘ansible_local’][‘custom’][‘software’][‘enabled’] }}
What are three requirements for variables
Must start with a letter
They’re case sensitive
Can only contain letters numbers and variables
Create a variable file, it should be in it’s own directory.
Use that variable file to install a package
mkdir /vars/
vi /vars/common
my_package: nmap
my_ftp_service: vsftpd
my_file_service: smb
- name: Install
hosts: ansible2
vars_files: vars/common
tasks:- name: Install
dnf:
name: “{{ my_package }}”
state: latest
- name: Install
create variables for ansible2
create variables for nodes
REMEMBER: You do not have to specify the file, ansible will know what variables to grab based off of the hosts key.
In project folder:
mkdir host_vars
vim host_vars/ansible2
package: httpd
mkdir group_vars
vim group_vars/nodes
package: vsftpd
What are variable files stored in vars host_vars and group_vars known as?
include files
How would you create the yaml equivalent of:
bands = [
{
“name”: “The Beatles”,
“drums”: “Ringo Star”,
“bass”: “Paul McCartney”,
“guitar”: [“George Harrison”, “John Lennon”],
“vocals”: [“John Lennon”, “Paul McCartney”, “George Harrison”, “Ringo Star”],
},
{
“name”: “The Police”,
“drums”: “Stewart Copeland”,
“bass”: “Sting”,
“guitar”: “Andy Summers”,
“vocals”: “Sting”,
},
]
bands:
- name: The Beatles
drums: Ringo Star
bass: Paul McCartney
guitar:
- George Harrison
- John Lennon
vocals:
- John Lennon
- Paul McCartney
- George Harrison
- Ringo Star
- name: The Police
drums: Stewart Copeland
bass: Sting
guitar: Andy Summers
vocals: Sting
What are arrays and dictionaries know as using a different term?
array - list
dictionary - hash
Create the yaml equivalent of the below:
my_dictionary = {
“name”: “John”,
“age”: 28,
“profession”: “Engineer”,
“skills”: [“Python”, “Linux”, “Networking”]
}
my_dictionary:
name: “John”
age: 28
profession: “Engineer”
skills:
- “Python”
- “Linux”
- “Networking”
Create a variable file called users-dictionary
One dictionary should be named linda and should contain a username, shell, and home directory..
Call these all in a playbook and print it to stdout
mkdir /vars
cd /vars
vi users-dictionary
users:
linda:
username: linda
homedir: /home/linda
shell: /bin/bash
lisa:
username: lisa
homedir: /home/linda
shell: /bin/bash
Name 5 important Magic Variables
hostvars - all hosts in inventory and their assigned variables
groups - All groups in inventory
group_names - List groups this host is currently a member of
inventory_hostname - Specifies inventory hostname of current host
inventory_file - Name of current inventory file that is used.
Using an ad-hoc command and magic variable, show all the variables specific to ansible2
ansible localhost -m debug -a “var=hostvars[‘ansible1’]”
What variables take precedence? Give the order in terms of three different areas
This is for variables with the same name
Command line variables
Variables defined in a playbook
inventory variables
So if you have the variable named ‘username: anthony’ and then run the playbook with ‘–extra-vars username: jacob’
jacob wins
In you have username: anthony in vars file and then specify the variable username: jacob in your playbook while also pointing to the variable file, jacob wins again