Section 6 Flashcards

1
Q

What statement do you use to perform a conditional?

If this_is_true then do this

How are variables different here?

A

when

You don’t use curly braces

when: ansible_facts[‘thing’] == ‘Debian’

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

How do you determine what is an ansible_fact from:
ansible ansible2 -m setup
why does ansible_os_family work as well as ansible_facts[‘os_family’]

A

To find ansible facts, they start with ansible_
The reason ansible_os_family works is because it’s an injected variable
ansible_facts[‘os_family’] is the new way of doing things.

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

What are the main conditionals you can test?

A

variable is defined - if the var exists

variable is not defined - if the variable doesn’t exist

ansible_distribution in distributions - first variable is present in list mentioned as second
EXAMPLE:
when: ansible_os_family in [‘Debian’, ‘RedHat’, ‘Suse’]

variable - variabe is true, 1, or yes

not variable - variable is false, 0, or no

key == ‘value’
key > ‘value’
key <= ‘value’
key > ‘value’
key >= ‘value’
key != value

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

Create a playbook that check if sda, sdb exists, the last should check that sdc DOESN’T exist

A

tasks:
- name: Does SDA exist
debug:
msg: ‘SDA does exist’
when: ansible_facts[‘devices’][‘sda’] is defined
- name: Does SDB exist
debug:
msg: ‘SDB does exist’
when: ansible_facts[‘devices’][‘sdb’] is defined
- name: Does SDC exist
debug:
msg: ‘SDB does not exist’
when: ansible_facts[‘devices’][‘sdc’] is not defined

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

Create a playbook, have it ask the user a question on what package to install and store it in a variable.
Create a variable with a list of packages.
If the defined package doesn’t exist in the list, let the user know

A
  • name: Testing with the IN statement
    hosts: all
    vars_prompt:
    • name: my_answer
      prompt: Which package do you want to install?
      vars:
      supported_packages:
      • httpd
      • nmap
        gather_facts: false
        tasks:
    • name: Something
      debug:
      msg: ‘You are trying to install a supported package’
      when: my_answer in supported_packages
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Create a variable with a boolean value. Check if it’s true

A
  • name: test
    hosts: all
    vars:
    aged: True
    tasks:
    • name: Check if aged is True
      debug:
      msg: ‘aged is True’
      when: aged
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create a playbook that check is the memory of the managed node is about 50 megs

What form of measurement is disk space measured in?

A

debug:
msg: ‘test’
when: ansible_facts[‘memory_mb’][‘real’][‘free’] > 50

disks are measured in bytes

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

Create a playbook that says ‘using CentOS 8.1’ if the distribution is 8.1 and the distribution is centos

A

debug:
msg: ‘using CentOS 8.8’
when: ansible_facts[‘distribution_version’] == ‘8.1’ and ansible_facts[‘distribution’] == ‘CentOS’

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

Over multiple lines, write a conditional that needs the distribution to be redhat and have 512 megs free memory or is CentOS and has 256 megs of memory free

A

when: >
( ansible_facts[‘distribution’] == ‘RedHat’ and ansible_facts[‘memfree_mb’] == 512 )
or
( ansible_facts[‘distribution’] == ‘CentOS’ and ansible_facts[‘memfree_mb’] == 256 )

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

Update the kernel if there is a drive mounted to /boot and there is 200000000 disk space available on it

A
  • name:
    package:
    name: kernel
    state: latest
    loop: “{{ ansible_facts[‘mounts’] }}”
    when: item.mount == “/boot/ and item.size_available > 200000000
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Create a playbook that opens /etc/passwd and stores the output in a variable. If the user lisa is found print a message

A
  • shell: cat /etc/passwd
    register: passwd_contents
  • debug:
    msg: passwd contains user lisa
    when: passwd_contents.stdout.find(‘lisa’) != -1

this uses the password find command. The output for ‘not’ found is -1

find will output the index of the first occurence. This means if you wanted to redo this code you could put

when: passwd_contents.stdout.find(‘lisa’) == >= 0

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

How do you let a playbook know to run even if a certain task failes?

A

ignore_errors: yes

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

Create a playbook that tries to get the status of httpd but will run regardless of errors. Place the output in a variable.

Next print the results.

Next start the httpd service only if the return of the first task is a success.

A

tasks:
- name: get httpd service status
command: systemctl is-active httpd
ignore_errors: yes
register: result
- name: show result variable contents
debug:
msg: printing contents of registered variable {{ result }}
- name restart sshd service
service:
name: sshd
state: restarted
when: result.rc ==0

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

What is a handler?

A

Conditional task ran if another specific task CHANGES something

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

Create a playbook with two plays in it.
the only task in the first play is for the localhost and it should create a file named index.html in its tmp directory.

The next play should have the below tasks:
install httpd
copy the index file to var/www/html/index.html - if this is successful then you should run a handler that restarts httpd

A
  • name: Create file on localhost
    hosts: localhost
    tasks:
    • name: Create index.html on localhost
      copy:
      content: ‘welcome to the webserver’
      dest: /tmp/index.html
  • name: Set up Web Server
    hosts: all
    tasks:
    • name: Install httpd
      dnf:
      name: httpd
      state: latest
    • name: copy over index.html
      copy:
      src: /tmp/index.html
      dest: /var/www/html/
      notify: restart_web
    handlers:
    - name: restart_web
    service:
    name: httpd
    state: restarted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If a task fails, make it to where the previous tasks that were changed have their handlers ran

A

force_handlers: true <- all handlers notified prior to error will run. They still need to notify the handlers by being changed.

ignore_errors

__ __
|_ | | | |/ | |

s|_| ( k |) | C k

|| | |\ |_|

17
Q

When do handlers run

A

After the play they’re in finishes
If there are two plays and a handler is called in the first play, it will run after all tasks in the first play have been ran. After it is ran, the next play will be ran.

18
Q

Create a playbook that forces the handlers to run, updates the kernel, and then reboots the server if the kernel update results in a change

A
  • name: Update the kernel
    hosts: all
    force_handlers: true
    tasks:
    • name: Update kernel
      yum:
      name: kernel
      state: latest
      notify: reboot_server
      handlers:
    • name: reboot_server
      command: rebootforce_handlers just means that handlers will be ran even if nothing changes. It does NOT mean that they will run if there is an error prior to them.
19
Q

When a failing task is encountered, how do you stop the playbook from proceeding on all servers

A

any_errors_fatal: true

20
Q

Create a playbook with two tasks:
print hello world
if world exists in the output make the task fail
don’t let the failure prevent the next task from running

A
  • name: Update the kernel
    hosts: all
    tasks:
    • name: Print
      command: echo hello world
      ignore_errors: true
      register: command_result
      failed_when: “‘world’ in command_result.stdout”
    • name: See if we get here
      debug:
      msg: second task executed
21
Q

Create a playbook that prints a fail message when the word ‘word’ is found in an echo command but continues going

A
  • name: Update the kernel
    hosts: all
    register_errors: yes
    tasks:
    • name: Print
      command: echo hello world
      ignore_errors: true
      register: command_result
    • name: Error
      fail:
      msg: Command has failed
      when: “‘world’ in command_result.stdout”
    • name: See if we get here
      debug:
      msg: second task executed
22
Q

What directive can we used to make sure that the output of a command never comes out as ‘changed’ only ‘ok’ and ‘failed’

A

changed_when: false

23
Q

What is a block?
How would you create a block statement?

A

Group of tasks that a when statement can be applied to

  • name: Setting up http
    block:
    • name: List everything else here and put a conditional at the end
24
Q

How do you make a block statment

A

each task is indented two spaces and the when statement is placed at the same indentation as the top level task