Section 7 Flashcards

1
Q

What statement do you use for error handling? For instance, if you have code in a block and it fails, give it something to do to roll with the punches and maybe fix the error.

A

rescue:
- name:
module:

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

In your block there are tasks that fail. What do you put to run the next code in the block regardless if these fail.

A

always:
- name:
module:

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

Create a block of tasks
First it will remove a file
If there are any issues with this task failing, create a file in /tmp called ‘rescuefile’ and allow the playbook to complete.
Next, regardless of success or failure of the first task, have a task run make a log message.
There should be a message noting everything that’s happening in the playbook.

A
  • name: using blocks
    hosts: all
    tasks:
    • name: Intended to be successful block
      block:
      • name: remove file
        shell:
        cmd: rm /var/www/html/index.html
      • name: print status
        debug:
        msg: ‘block was operated’
      • name: create a file
        rescue:
        • name: create a file
          shell:
          cmd: touch /tmp/rescuefile
        • name: print rescue status
          debug:
          msg: ‘Rescue complete’
          always:
          • name: log message
            shell:
            cmd: logger hello
        • name: always print this message
          debug:
          msg: logger update
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Blocks are great, but what’s a feature they don’t have?

A

You can’t use a loop that all tasks in block share
block:
tasks:
loop: {{ whatever }}

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

What are the most commonly used file modules? There are 10

A

file
copy
fetch - fetch files from remote locations
acl
find
lininfile
blockinfile
replace
synchronize
stat

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

How do you find related modules to file?

A

ansible-doc file

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

Show the statistics of /tmp/temporary

A
  • stat:
    path: /tmp/temporary
    register: fs
  • debug:
    msg: “{{ fs }}”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Register a files stats and make a condition based on one of the pieces of info. If it is not met, force the playbook to fail

A
  • command: touch /tmp/statfile
  • name stat file
    stat:
    path: /tmp/statfile
    register: fs
  • fail:
    msg: ‘unexpected file mode’
    when: st.stat.mode != ‘0640’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Check SSHD for the permitrootlogin line and change it to no
If this causes a change, restart sshd

A
- name: SSH config
  hosts: all
  tasks:
    - name: Disable Root Login
	  lineinfile:
	    dest: /etc/ssh/sshd_config
		regexp: '^PermitRootLogin'
		line: 'PermitRootLogin no'
	  notify: restart sshd
	  
	handlers: 
	  - name: Restart SSHD
		service:
		  name: sshd
		state: restarted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create a file named /tmp/hosts and add the below lines to it:
192.168.4.110 host1.example.com
192.168.4.110 host1.example.com

A
- name: Add Hosts
  hosts: all
  tasks:
    - name: Create file
	  file:
	    path: /tmp/hosts
		state: touch
	- name: Add junk	
	  blockinfile:
		path: /tmp/hosts
		block: |
		192.168.4.110 host1.example.com
		192.168.4.110 host1.example.com
	state: present
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are four things the file module can do?

A

Create new files or directories
create links
remove files
set permissions and ownership

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

What’s the difference between the modules synchronize and copy?

A

Copy always makes a new file
synchronize just updates it

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

Copy is used to copy files FROM the control node
How do you copy files FROM the managed node

A

fetch

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

What is a checksum used for?

Copy over /etc/hosts to the managed node’s /tmp directory.
Add two lines to it for whatever hosts you want
Register a checksum for /tmp/hosts
Print the checksum
grab the file from /tmp/hosts and put it in your tmp folder
Where did the file go?

A

Checksums are used to determin if a file has changed and needs to be copied or updated.

- name: Test
  hosts: all
  tasks:
    - name: copy
	  copy:
	    src: /etc/hosts
		dest: /tmp/hosts
		
	- name: Add junk	
	  blockinfile:
		path: /tmp/hosts
		block: |
		192.168.4.110 host1.example.com
		192.168.4.110 host1.example.com
	state: present
	
	- name: checksum
	  stat:
	    path:
		checksum_algorithm: md5
	  register: result
	  
	- name: debug
	  debug:
	    msg: {{ result.stat.checksum }}
	
	- name: fetch file
	  fetch:
	    src: /tmp/hosts
		dest: /tmp

A directory was created for it in tmp with it’s name

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

Create a file on ansible1
register it’s status in a variable and print it
Change the user to ‘ansible’ if that isn’t the owner

Add another play that:
gets the motd from ansible1, put it in your temp directory

Add a play that adds text the motd

copy the motd to ansible2

A
  • name: Testing file manipulation
    hosts: ansible1
    tasks:
    • name: Create a new file
      file:
      path: /tmp/newfile
      state: touch
    • name: Check file status
      stat:
      path: /tmp/newfile
      register: newfile
    • name: debugging
      debug:
      msg: “{{ newfile }}”
    • name: Change file owner if needed
      file:
      path: /tmp/newfile
      owner: ansible
      when: newfile.stat.pw_name != ‘ansible’
  • name: Fetching remote file
    hosts: ansible1
    tasks:
    • name: Fetch file from a remote machine
      fetch:
      src: /etc/motd
      dest: /tmp
  • name: Add text to the file that is now on localhost
    hosts: localhost
    tasks:
    • name: Add a message
      blockinfile:
      path: /tmp/ansible1/etc/motd
      block: |
      Welcome to this server
      for authorized users only
      state: present
    • name: Copy file to ansible2
      hosts: ansible2
      tasks:
      • name: copy motd file
        copy:
        src: /tmp/ansible/etc/motd
        dest: /tmp
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s setting the context at file level rather than the selinux context

A

chcon vs semanage fcontext

17
Q

What do you need to run selinux ansible playbooks on a managed node

A

policycoreutils-python-utils

18
Q

Install Selinux commands
Create a file
Give it the contenxt type httpd_sys_content_type
Run restorecon

A
  • name: show selinux
    hosts: all
    tasks:
    • name: Install required packages
      dnf:
      name: policycoreutils-python-utils
      state: latest
    • name: Create test file
      file:
      name: /tmp/testfile
      state: touch
    • name: Set Selinux Context
      sefcontext:
      target: /tmp/selinux
      setype: httpd_sys_content_t
      state: present
      notify:
      • run restorecon
    handlers:
    - name: run restorecon
    command: restorecon -v /tmp/selinux
19
Q

What does it mean to configure a service with a nondefault document root

A

Changing /var/www/html to a different path via httpd <- example

20
Q

Create a playbook variable of httpd_read_user_content

Enable SElinux in targeted mode

Check the boolean’s status/info and register it

Show the boolean’s status

Enabled the boolean

A
  • name: enable selinux and boolean
    hosts: ansible1
    vars:
    • my_boolean: httpd_read_user_content
      tasks:
    • name: Enable SELinux
      selinux:
      policy: targeted
      state: enforcing
    • name: Check current {{ my_boolean }} boolean status
      shell: getsebool -a | grep {{ my_boolean }}
      register: bool_stat
    • name: Show boolean status
      debug:
      msg: the current {{ my_boolean }} status is {{ bool_stat.stdout }}
    • name: enable boolean
      seboolean:
      name: “{{ my_boolean }}”
      state: yes
      persistent: yes
21
Q

Install , start and configure a webserver that has the DocumentRoot set to the /web directory. The file should be called index.html and it should say something welcoming the user to the server.
Ensure that SElinux is enabled and allows acces to the web server document root.
SElinux should allow user to publish web pages from their home directory.
This will reveal something is wrong when you try to curl the page, figure out why.
Best practice for a long playbook like this it to create the file header and add the task names prior.

A
  • name: Managing web server SELinux properties
    hosts: ansible1
    tasks:
    • name: Ensure SELinux is enabled and enforcing
      selinux:
      policy: targeted
      state: enforcing
    • name: install webserver
      dnf:
      name: httpd
      state: latest
    • name: start and enable webserver
      service:
      name: httpd
      state: started
      enabled: true
    • name: open firewall service
      block:
      • firewalld:
        service: http
        state: enabled
        immediate: yes
    • name: create /web directory
      file:
      path: /web
      state: directory
    • name: create index.html file in /web
      copy:
      content: |
      welcome to the web server
      dest: /web/index.html
    • name: use lineinfile to change webserver config
      lineinfile:
      path: /etc/httpd/conf/httpd.conf
      regexp: ‘^<Directory “/var/www”>
      line: ‘<Directory “/web”>’
    • name: use sefcontext to set context on new documentroot
      sefcontext:
      target: /web(/.*)?
      setype: httpd_sys_content_t
      state: present
    • name: run resorecon
      shell: restorecon -Rv /web
    • name: allow web server to run user content
      seboolean:
      name: httpd_read_user_content
      state: yes
      persistent: yes