Skip to main content

Practice Variables Conditionals Loops

Updated Jan 09, 2021 ·

Overview

Note that these are not the exact same questions. You can try to answer them, simulate the problems in your own machines.

Use the expandable answers to compare your solution after you finish each exercise.

  1. Our task is to update the nameserver in the /etc/resolv.conf file of the local host with the new ip that is defined in this inventory file:

    $ cat inventory.txt

    localhost ansible_connection=localhost nameserver_ip=10.1.250.10

    Do all the necessary additions in the update.yaml below to achieve the task.**

    -
    name: Update /etc/resolv.conf
    hosts: localhost
    tasks:
    -
    lineinfile:
    path: /etc/resolv.conf
    line: "nameserver "
    Answer
    -
    name: Update /etc/resolv.conf
    hosts: localhost
    tasks:
    -
    lineinfile:
    path: /etc/resolv.conf
    line: "nameserver {{ nameserver_ip }}"
  2. We are asked to update the yaml file and remove the hardcoded snmp port and instead use variable.

    -
    name: 'Update nameserver entry into resolv.conf file on localhost'
    hosts: localhost
    tasks:
    -
    name: 'Update nameserver entry into resolv.conf file'
    lineinfile:
    path: /etc/resolv.conf
    line: 'nameserver {{ nameserver_ip }}'
    -
    name: 'Disable SNMP Port'
    firewalld:
    port: 160-161
    permanent: true
    state: disabled

    The snm port is defined in the inventory file below:

    localhost ansible_connection=localhost nameserver_ip=10.1.250.10 snmp_port=160-161
    Answer
    -
    name: 'Update nameserver entry into resolv.conf file on localhost'
    hosts: localhost
    tasks:
    -
    name: 'Update nameserver entry into resolv.conf file'
    lineinfile:
    path: /etc/resolv.conf
    line: 'nameserver {{ nameserver_ip }}'
    -
    name: 'Disable SNMP Port'
    firewalld:
    port: "{{ snmp_port }}"
    permanent: true
    state: disabled

  3. Update the playbook below to remove hardcoded values for car_model, country_name, and job_title and instead use variable.

    -
    name: 'Update nameserver entry into resolv.conf file on localhost'
    hosts: localhost
    tasks:
    -
    name: 'Print my car model'
    command: 'echo "My car''s model is BMW M3"'
    -
    name: 'Print my country'
    command: 'echo "I live in the USA"'
    -
    name: 'Print my title'
    command: 'echo "I work as a Systems Engineer"'
    Answer
    name: 'Update nameserver entry into resolv.conf file on localhost'
    hosts: localhost
    vars:
    car_model: "BMW M3"
    country_name: "USA"
    job_title: "Systems Engineer"
    tasks:
    -
    name: 'Print my car model'
    command: 'echo "My car''s model is {{ car_model }}"'
    -
    name: 'Print my country'
    command: 'echo "I live in the {{ country_name }}"'
    -
    name: 'Print my title'
    command: 'echo "I work as a {{ job_title }}"'

  4. Update the playboook so that it will start the mysql service if the host is a server4.corp.com

    -
    name: 'Execute a script on all web server nodes'
    hosts: all_servers
    tasks:
    Answer
    -
    name: 'Execute a script on all web server nodes'
    hosts: all_servers
    tasks:
    -
    service: 'name=mysql state=started'
    when: ansible_host == "server4.corp.com"
  5. Update the YAML file below and use conditionals to print statements based on age. If age < 18, then that is consider a child, otherwise, age>=18 is considered an adult.

    -
    name: 'Am I an Adult or a Child?'
    hosts: localhost
    vars:
    age: 25
    tasks:
    -
    command: 'echo "I am a Child"'
    -
    command: 'echo "I am an Adult"'
    Answer
    -
    name: 'Am I an Adult or a Child?'
    hosts: localhost
    vars:
    age: 25
    tasks:
    -
    command: 'echo "I am a Child"'
    when: 'age < 18'
    -
    command: 'echo "I am an Adult"'
    when: 'age >= 18'

  6. The playbook file below is already correct but we are tasked to update it to use the lineinfile instead of the when.

    -
    name: 'Add name server entry if not already entered'
    hosts: localhost
    tasks:
    -
    shell: 'cat /etc/resolv.conf'
    register: command_output
    -
    shell: 'echo "nameserver 10.0.250.10" >> /etc/resolv.conf'
    when: 'command_output.stdout.find("10.0.250.10") == -1'
    Answer
    -
    name: 'Add name server entry if not already entered'
    hosts: localhost
    tasks:
    -
    shell: 'cat /etc/resolv.conf'
    register: command_output
    -
    lineinfile:
    path: '/etc/resolv.conf'
    line: 'nameserver 10.0.250.10'
  7. Continue the playbook below by printing all the item in the fruits variable.

    -
    name: 'Print list of fruits'
    hosts: localhost
    vars:
    fruits:
    - Apple
    - Banana
    - Grapes
    - Orange
    tasks:
    -
    command: 'echo '

    Answer
    -
    name: 'Print list of fruits'
    hosts: localhost
    vars:
    fruits:
    - Apple
    - Banana
    - Grapes
    - Orange
    tasks:
    -
    command: 'echo "{{ item }}"'
    with_items: '{{ fruits }}'

  8. Modify the playbook below to install all the packages.

    -
    name: 'Install required packages'
    hosts: localhost
    vars:
    packages:
    - httpd
    - binutils
    - glibc
    - ksh
    - libaio
    - libXext
    - gcc
    - make
    - sysstat
    - unixODBC
    - mongodb
    - nodejs
    - grunt
    tasks:
    -
    yum: 'name=httpd state=present'
    Answer
    -
    name: 'Install required packages'
    hosts: localhost
    vars:
    packages:
    - httpd
    - binutils
    - glibc
    - ksh
    - libaio
    - libXext
    - gcc
    - make
    - sysstat
    - unixODBC
    - mongodb
    - nodejs
    - grunt
    tasks:
    -
    yum:
    name: '{{ item }}'
    state: present
    with_items: '{{ packages }}'