Section 4 Flashcards

1
Q

Via Ansible facts show
hostname
distribution
ipv4
network interfaces
storage devices
size of /dev/sda1
version distribution

A

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’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

show modules that are good for gathering facts

A

ansible-doc -l | grep fact

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Ansible Facts vs. Injected Variable

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a disadvantage of gathering facts for during your playbooks? What can you do about this?

A

This can slow down your playbook
You can turn this off in the play head with:
gather_facts: no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

/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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

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’] }}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are three requirements for variables

A

Must start with a letter
They’re case sensitive
Can only contain letters numbers and variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create a variable file, it should be in it’s own directory.
Use that variable file to install a package

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

create variables for ansible2
create variables for nodes

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are variable files stored in vars host_vars and group_vars known as?

A

include files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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”,
},
]

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are arrays and dictionaries know as using a different term?

A

array - list
dictionary - hash

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Create the yaml equivalent of the below:

my_dictionary = {
“name”: “John”,
“age”: 28,
“profession”: “Engineer”,
“skills”: [“Python”, “Linux”, “Networking”]
}

A

my_dictionary:
name: “John”
age: 28
profession: “Engineer”
skills:
- “Python”
- “Linux”
- “Networking”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Name 5 important Magic Variables

A

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.

17
Q

Using an ad-hoc command and magic variable, show all the variables specific to ansible2

A

ansible localhost -m debug -a “var=hostvars[‘ansible1’]”

18
Q

What variables take precedence? Give the order in terms of three different areas

A

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