Skip to content

Search & Find

Within the next several scenarios, you will need to search through Ansible's list of modules and figure out which one(s) will work best. Write a task to implement the actions.

Scenario 1

Copy a file called foo.conf with owner foo, group bar, from /srv/myfiles to /etc. Give the file rw-r--r-- permissions.

Scenario 1 Solution
- name: Copy file with owner and permissions
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: bar 
    mode: '0644'

Scenario 2

Use a template file for a configuration file called file.conf. The template is located at /mytemplates/foo.j2. The templated file should end up in a directory /etc. The owner is bin, group is wheel. Permissions are rw-r--r--.

Scenario 2 Solution
- name: Template a file to /etc/files.conf
  template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: '0644'

Scenario 3

Hard

Download, yum install, and symlink Java 8 ONLY if install directory doesn't exist.

Download from http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.rpm

Hint: Ansible Block

Scenario 3 Solution

This is one solution, but you may have something slightly different.

- name: Check if Java 8 is installed
  stat:
    path: /java/oraclejdk8
  register: oraclejdk8_sym

- block:
    - name: Download Java 8
      get_url:
        url: http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.rpm
        dest: /opt
    - name: Install Java 8
      yum:
        name: /opt/jdk-8u60-linux-x64.rpm
        state: present
    - name: Symlink to Java 8 Install
      file:
        src: /java/oraclejdk8
        dest: /opt/java/oraclejdk8
        state: link
    - name: Clean up Install Directory
      file:
        src: /opt/jdk-8u60-linux-x64.rpm 
        state: absent
  when: oraclejdk8_sym.stat.islink is not defined

Scenario 4

Hard

Update a foo.xml configuration file using a template and restart the foo service only if there are changes found.

Hint: Handler.
Note this one will not work when run as it's just an example.

foo.xml.j2

<Servers>
   <Server name={{ server_name }} port={{ http_port }}>
<Servers>

input.json

{
    "server_name" : "server1"
    "http_port" : "8080"
}

Scenario 4 Solution
- hosts: localhost
  gather_facts: false
  tasks:
    - name: Update Configuration
      template:
        src: foo.xml.j2
        dest: foo.xml
      notify: restart
  handlers:
    - name: restart
      service:
        name: foo
        state: restarted
        enabled: yes

Scenario 5

Extra Hard

You are asked to create a play that reads a json file called input.json, loops through hosts, and writes name, user, directory to a file called output.txt.

Hint: from_json, with_XXXX

input.json

{
  hosts: [ {
    name: server1,
    user: user1,
    directory: /etc/foo,
    }, {
    name: server2,
    user: user2,
    directory: /etc/bar
    }
  ]
}

Scenario 5 Solution
#Solution 1
- name: Read JSON file
  shell: cat input.json
  register: result

- name: Write to file
  lineinfile:
    path: output.txt
    line: "Name: {{ item.name }}, User: {{ item.user }}, Directory: {{ item.directory }}"
    state: present
  with_items:
    - "{{ result.stdout['hosts'][0] | from_json }}" 

# Solution 2
- name: "Read Json"
  set_fact:
    my_var: "{{ lookup('file', 'input.json') | from_json }}"

- name: "Print Json"
  debug: msg="Name:{{ item.name }}, User:{{ item.user }}, Directory:{{ item.directory }}"
  with_items:
    - "{{ my_var['hosts'] }}"

# Solution 3
- name: "Read Json"
  include_vars:
    dir: vars
    extensions:
      - json
      - yml

- name: "Print Json"
  debug: msg="Name:{{ item.name }}, User:{{ item.user }}, Directory:{{ item.directory }}"
  with_items:
    - "{{ hosts }}"