Practice Variables Conditionals Loops
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.
-
Our task is to update the nameserver in the
/etc/resolv.conffile of the local host with the new ip that is defined in this inventory file:$ cat inventory.txtlocalhost ansible_connection=localhost nameserver_ip=10.1.250.10Do all the necessary additions in the update.yaml below to achieve the task.**
-name: Update /etc/resolv.confhosts: localhosttasks:-lineinfile:path: /etc/resolv.confline: "nameserver "Answer
-name: Update /etc/resolv.confhosts: localhosttasks:-lineinfile:path: /etc/resolv.confline: "nameserver {{ nameserver_ip }}" -
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: localhosttasks:-name: 'Update nameserver entry into resolv.conf file'lineinfile:path: /etc/resolv.confline: 'nameserver {{ nameserver_ip }}'-name: 'Disable SNMP Port'firewalld:port: 160-161permanent: truestate: disabledThe snm port is defined in the inventory file below:
localhost ansible_connection=localhost nameserver_ip=10.1.250.10 snmp_port=160-161Answer
-name: 'Update nameserver entry into resolv.conf file on localhost'hosts: localhosttasks:-name: 'Update nameserver entry into resolv.conf file'lineinfile:path: /etc/resolv.confline: 'nameserver {{ nameserver_ip }}'-name: 'Disable SNMP Port'firewalld:port: "{{ snmp_port }}"permanent: truestate: disabled -
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: localhosttasks:-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: localhostvars: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 }}"' -
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_serverstasks:Answer
-name: 'Execute a script on all web server nodes'hosts: all_serverstasks:-service: 'name=mysql state=started'when: ansible_host == "server4.corp.com" -
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>=18is considered an adult.-name: 'Am I an Adult or a Child?'hosts: localhostvars:age: 25tasks:-command: 'echo "I am a Child"'-command: 'echo "I am an Adult"'Answer
-name: 'Am I an Adult or a Child?'hosts: localhostvars:age: 25tasks:-command: 'echo "I am a Child"'when: 'age < 18'-command: 'echo "I am an Adult"'when: 'age >= 18' -
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: localhosttasks:-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: localhosttasks:-shell: 'cat /etc/resolv.conf'register: command_output-lineinfile:path: '/etc/resolv.conf'line: 'nameserver 10.0.250.10' -
Continue the playbook below by printing all the item in the fruits variable.
-name: 'Print list of fruits'hosts: localhostvars:fruits:- Apple- Banana- Grapes- Orangetasks:-command: 'echo 'Answer
-name: 'Print list of fruits'hosts: localhostvars:fruits:- Apple- Banana- Grapes- Orangetasks:-command: 'echo "{{ item }}"'with_items: '{{ fruits }}' -
Modify the playbook below to install all the packages.
-name: 'Install required packages'hosts: localhostvars:packages:- httpd- binutils- glibc- ksh- libaio- libXext- gcc- make- sysstat- unixODBC- mongodb- nodejs- grunttasks:-yum: 'name=httpd state=present'Answer
-name: 'Install required packages'hosts: localhostvars:packages:- httpd- binutils- glibc- ksh- libaio- libXext- gcc- make- sysstat- unixODBC- mongodb- nodejs- grunttasks:-yum:name: '{{ item }}'state: presentwith_items: '{{ packages }}'