Ansible Automation Choral

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

-------------------------- Hands On -----------------------------------

# Workout - hostvars and inventory_hostname

#Step 1

vim test.xml

#Insert the following code inside test.yml. Press "i" without quotes to insert
text.

---
- name: built-in variables
hosts: all
tasks:
- debug: var=hostvars[inventory_hostname]

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

#Step 2

ansible-playbook -i myhosts test.yml

-------------------------- Hands On -----------------------------------

# Try It Out-Registered Variable

touch afile.txt

vim test.yml

#Insert the following code inside test.yml. Press "i" without quotes to insert
text.

---
- hosts: all
become: yes
name: Copy file
tasks:
- debug:
- template: src=afile.txt dest=/home/ubuntu/afile_copy.txt

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

ansible-playbook -i myhosts test.yml

#Run the following command just to validate that the file was successfully
copied.

ansible host01 -i myhosts -m command -a "ls"

--------------------------------------------------------------------------------
-------------------------------

Which ones are built-in variables?


All the options ~~~
hostvars
inventory_hostname
groups

Which option will give you error while accessing your complex variable?
{{ ansible_eth0['ipv4']['address'] }}

This study source was downloaded by 100000839603243 from CourseHero.com on 01-17-2022 08:15:28 GMT -06:00

https://www.coursehero.com/file/66202666/Ansible-Automation-Choraltxt/
{{ ansible_eth0[ipv4][address] }} ~~~
{{ ansible_eth0.ipv4.address }}
{{ ansible_eth0["ipv4"]["address"] }}

Which variable allows you to save the output of a task?


register ~~~
save
hostvars

Fact variables have higher precedence than Playbook Variables


True False ~~~

You can skip the task of gathering facts through which of the following way?
gather_facts: no ~~~
gather_fact: no

Which module can tell you the fact variables of a host?


service
fetch
setup ~~~
apt

--------------------------------------------------------------------------------
---------------------------------

-------------------------- Hands On -----------------------------------

# Practicals - Special Tags

#Step 1

vim tag.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

---
- name: Special Tags
hosts: all
sudo: yes
tasks:
- name: location 1
debug: msg="california"

- name: location 2
debug: msg="mumbai"
tags:
- tag1

- name: location 3
debug: msg="bangalore"
tags:
- tag2

- name: location 4
debug: msg="chennai"
tags:
- always

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

#Step 2

This study source was downloaded by 100000839603243 from CourseHero.com on 01-17-2022 08:15:28 GMT -06:00

https://www.coursehero.com/file/66202666/Ansible-Automation-Choraltxt/
ansible-playbook -i myhosts tag.yml --tags "tag1"
ansible-playbook -i myhosts tag.yml --tags "tagged"
ansible-playbook -i myhosts tag.yml --tags "untagged"
ansible-playbook -i myhosts tag.yml --tags "untagged" --skip-tags "always"

--------------------------------------------------------------------------------
---------------------------------

-------------------------- Hands On -----------------------------------

# Workout - Include

#Step 1

touch apache.yml
touch playbook_include.yml
touch create_folder.yml
touch content.yml
touch nginx.yml

#Step 2

vim playbook_include.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

---
- name: testing includes
hosts: all
sudo: yes
tasks:
- include: apache.yml
- include: content.yml
- include: create_folder.yml
- include: content.yml
- include: nginx.yml

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

#Step 3

vim apache.yml

#Insert the following code inside plabook_include.yml

---
- name: install apache2
apt: name=apache2 update_cache=yes state=latest
- name: displaying message
debug: msg="you are awesome!!"

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

#Step 4

vim create_folder.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes

This study source was downloaded by 100000839603243 from CourseHero.com on 01-17-2022 08:15:28 GMT -06:00

https://www.coursehero.com/file/66202666/Ansible-Automation-Choraltxt/
to insert text.

---
- name: creating folder
file: path=/home/ubuntu/folder1 state=directory

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

#Step 5

vim content.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

---
- name: list contents of directory in host
command: ls /home/ubuntu
register: contents

- name: check dir is empty


debug: msg="Directory is empty"
when: contents.stdout == ""

- name: check dir has contents


debug: msg="Directory is not empty"
when: contents.stdout != ""

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

#Step 6

vim nginx.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

---
- name: installing nginx
hosts: all
sudo: yes
tasks:
- name: install nginx
apt: name=nginx update_cache=yes state=latest
- name: displaying message
debug: msg="yayy!! nginx installed"

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

#Step 7

ansible-playbook -i myhosts playbook_include.yml

--------------------------------------------------------------------------------
--------------------------------

This keyword gives you flexibility to run specific part of your playbook.

This study source was downloaded by 100000839603243 from CourseHero.com on 01-17-2022 08:15:28 GMT -06:00

https://www.coursehero.com/file/66202666/Ansible-Automation-Choraltxt/
Tags ~~~
All the options
include
tag

Which tag is executed by default while running a Playbook?


always
None of the options
tagged
all ~~~

You can run non tagged tasks using which magical word?
None of the options ~~~
nontag
untag
tagless

You can Tag entire play.


False True ~~~

Which of the folowing can you include in your Playbook?


All the options ~~~
Task
Role
Playbook

Tagging allows you to run or skip certain part of Playbook. Can you run tasks
that are not tagged?
Yes ~~~ No

--------------------------------------------------------------------------------
--------------------------------

-------------------------- Hands On -----------------------------------

# Workout - Ansible Roles

#Step 2

mkdir roles

sed -i '$ a roles_path = /home/scrapbook/tutorial/roles/' ansible.cfg

vim master_playbook.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

---
- name: my first role in ansible
hosts: all
sudo: yes
roles:
- sample_role

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

mkdir roles/sample_role && cd roles/sample_role

mkdir defaults files handlers tasks templates vars

#Step 4

This study source was downloaded by 100000839603243 from CourseHero.com on 01-17-2022 08:15:28 GMT -06:00

https://www.coursehero.com/file/66202666/Ansible-Automation-Choraltxt/
vim tasks/nginx.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

---
- name: Installs nginx
apt: pkg=nginx state=installed update_cache=true
notify:
- start nginx

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

vim handlers/main.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

---
- name: start nginx
service: name=nginx state=started

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

vim tasks/main.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

---
- include: nginx.yml

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

cd ..
cd ..
pwd
ansible-playbook -i myhosts master_playbook.yml

#Step 5

cd roles/sample_role/
touch files/some-file.txt
vim tasks/copy-static.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

---
- name: Copy a file
copy: src=some-file.txt dest=/home/ubuntu/file1.txt

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

vim tasks/main.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

This study source was downloaded by 100000839603243 from CourseHero.com on 01-17-2022 08:15:28 GMT -06:00

https://www.coursehero.com/file/66202666/Ansible-Automation-Choraltxt/
---
- include: nginx.yml
- include: copy-static.yml

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

cd ..
cd ..
pwd
ansible-playbook -i myhosts master_playbook.yml

#Step 6

cd roles/sample_role/
vim templates/template-file.j2

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

this is {{item}}

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

vim vars/main.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

var_x:
- 'variable x'
var_y:
- 'variable y'

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

vim tasks/copy-template.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

---
- name: sample template - x
template:
src: template-file.j2
dest: /home/ubuntu/copy-template-file.j2
with_items: var_x

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in
order to save changes.

vim tasks/main.yml

#Insert the following code inside plabook_include.yml. Press "i" without quotes
to insert text.

---
- include: nginx.yml
- include: copy-static.yml
- include: copy-template.yml

#Press "Esc" key and then write ":wq", without quotes, and then press "Enter" in

This study source was downloaded by 100000839603243 from CourseHero.com on 01-17-2022 08:15:28 GMT -06:00

https://www.coursehero.com/file/66202666/Ansible-Automation-Choraltxt/
order to save changes.

cd ..
cd ..
pwd
ansible-playbook -i myhosts master_playbook.yml

--------------------------------------------------------------------------------
---------------------------------

-------------------------- Hands On -----------------------------------

# Galaxy Useful Commands

#Step 1

ansible-galaxy init sample_role

#Step 2

ansible-galaxy install geerlingguy.apache


ansible-galaxy init geerlingguy.apache --force
ansible-galaxy list
ansible-galaxy remove geerlingguy.apache

--------------------------------------------------------------------------------
---------------------------------

This folder stores your dynamic files.


tasks
files
templates ~~~
vars

Variables defined in this folder have least priority.


None of the options
templates
defaults ~~~
vars

How can your scaffold your roles?


ansible
ansible-scaffold
ansible-galaxy ~~~
ansible-playbook

Which folder does not require main.yml file?


defaults
tasks
templates ~~~
handlers

Which of the following can you use to delete your installed role?
remove ~~~
omit
delete
destroy

This folder defines the dependencies of your Role.


tasks
handlers
files
meta ~~~

This study source was downloaded by 100000839603243 from CourseHero.com on 01-17-2022 08:15:28 GMT -06:00

https://www.coursehero.com/file/66202666/Ansible-Automation-Choraltxt/
--------------------------------------------------------------------------------
---------------------------------

This allows you to keep secret data in Playbook.


Ansible-vault ~~~
Ansible-playbook
None of the options
Ansible-galaxy

You can view the details of a role using which of the following?
None Of The Options
ansible-galaxy info username.role ~~~
ansible-galaxy search username.role
ansible-galaxy init username.role

Default variables in Roles have least precedence.


False True ~~~

Which is NOT a valid file extension to store inventory variables in a file?


.json
.yml
.ini ~~~
no file extension

Defining same variable in multiple locations will cause error .


False ~~~ True

How to access variables defined in one host from another host?


groups
inventory_hostname
None of the options
hostvars ~~~

Fact variables have higher precedence than Playbook Variables


True False ~~~

You can contribute your roles to Ansible community through GitHub Profile.
False True ~~~

How do you define Variables in Command Line Interface while executing playbook?
--variables-
--vars-extra
--extra-vars ~~~
None of the options

Handler names and listen topics live in a global namespace.


True ~~~ False

This study source was downloaded by 100000839603243 from CourseHero.com on 01-17-2022 08:15:28 GMT -06:00

https://www.coursehero.com/file/66202666/Ansible-Automation-Choraltxt/
Powered by TCPDF (www.tcpdf.org)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy