Вы находитесь на странице: 1из 8

include:

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

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

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

content.yml
---
- 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 != ""

nginx.yml
---
- 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"

Built-in varaibles:
Consider you need to run a task in host1 that need IP address of the eth1 interface
of the host2

{{ hostvars['host2'].ansible_eth1.ipv4.address }}

Scope Of Host Variables


Wondering the scope of variables defined in group of servers??

#host variables
[group1]
host1 http_port=80
host2 http_port=303
host3
#group variables
[group1:vars]
ntp_server= example.com
proxy=proxy.example.com

Groups
You can find the list of group names defined in your inventory file using groups
variable.

For Example: Consider you need the IP address of all the servers in you web group

{% for host in groups.web %}

server {{ host.inventory_hostname }} {{ host.ansible_default_ipv4.address }}:8080

{% endfor %}

The generated file might look like this:

server chennai.fresco.me 192.0.115.17:8080

server california.fresco.me 192.0.115.78:8080

server singapore.fresco.me 193.0.115.68:8080

Inventory_hostname
You can find the name of the current host using inventory_hostname.

#inventory file

[group1]

server1 ansible_ssh_host=192.169.67.34

inventory_hostname would be server1 instead of 192.169.67.34

Registered Variable
Ansible allows you to save the output of a task in a variable at run time, through
register keyword.

Syntax: register: variable_name

Let us now see how the return value of command module looks like. Go ahead and run
the following playbook in Katacoda

file: test.yml

---
- name: show return value of command module
hosts: all
tasks:
- name: creating folder
command: mkdir folder7
register: output
- debug: var=output
Run your playbook: ansible-playbook -i myhosts test.yml and observe the output
carefully

You might observe the output of running a task is returned in JSON format

Registered Variable
This playbook will check the contents of the home directory of your host machine
(host01) and display a message accordingly

Go ahead and run this in Katacoda

---

- name: check registered variable for emptiness

hosts: all

tasks:

- name: list contents of the directory in the 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 != ""

register variable stores the output, after executing command module, in contents
variable

stdout is used to access string content of register variable

Special Tags:

Tags
Tags are names pinned on individual tasks, roles or an entire play, that allows you
to run or skip parts of your Playbook.

Tags can help you while testing certain parts of your Playbook.

file: tag.yml
---
- name: Play1-install apache
hosts: all
sudo: yes
tasks:
- name: install apache2
apt: name=apache2 update_cache=yes state=latest
- name: displaying "hello world"
debug: msg="hello world"
tags:
- tag1
- name: Play2-install nginx
hosts: all
sudo: yes
tags:
- tag2
tasks:
- name: install nginx
apt: name=nginx update_cache=yes state=latest
- name: debug module displays message in control machine
debug: msg="have a good day"
tags:
- mymessage
- name: shell module displays message in host machine.
shell: echo "yet another task"
tags:
- mymessage

Running Tag.yml
You may save the above Playbook with name tag.yml and run the following commands in
Katacoda

ansible-playbook -i myhosts tag.yml --list-tasks : displays the list of tasks in


the Playbook

ansible-playbook -i myhosts tag.yml --list-tags : displays only tags in your


Playbook

ansible-playbook -i myhosts tag.yml --tags "tag1,mymessage" : executes only certain


tasks which are tagged as tag1 and mymessage
Special Tags
Ansible has some special keywords for tags:

always: runs the task always

tagged: run only those tasks which have some tag

untagged: run only those tags which do not have any tags

all: run all the tags

You can skip always tag by defining: --skip-tags always

Roles:
Phase 1- Creating Handler
Now you may come out of tasks folder (cd ..) and write your special handler task
inside handlers folder

file: handlers/main.yml
---
- name: start nginx
service: name=nginx state=started

Phase 1-Including Tasks In Main.yml


*Just like nginx.yml, later you will be adding more files in tasks folder.

How will ansible know which task to execute first?*

Tasks, Handlers, and Vars folder always have main.yml file

You need to include those tasks in main.yml file.

file: tasks/main.yml
---
- include: nginx.yml
As you can see, tasks/main.yml is just list of tasks.

Creating Master Playbook


To run all your tasks, you need to create a Playbook in root location
(/home/scrapbook/tutorial) location, which will call your role (sample_role).

Use cd .. to move up one directory

Use pwd to know your present working directory

Use ls to check files and folder in your present folder

file: master_playbook.yml
---
- name: my first role in ansible
hosts: all
sudo: yes
roles:
- sample_role
Telling Ansible About Your Roles
You called your role in Playbook but how will Ansible know, where your roles are
defined?

You need to explicitly tell the path(of roles) in ansible.cfg file

Remove the default configuration file: rm ansible.cfg (present in


/home/scrapbook/tutorial/ansible.cfg)

Add your new configuration settings: vi ansible.cfg, press i and use the settings
as shown:

file: ansible.cfg
[defaults]
host_key_checking=False
roles_path = /home/scrapbook/tutorial/roles/
```

What Is Happening?
So, let us have a look at what exactly is happening:

You have given the location of your roles by setting roles_path in Ansible
configuration file.

When you run this playbook, the Role will first check main.yml file in tasks folder
and execute tasks.

You can also define multiple roles in master_playbook and they are executed in a
sequential manner.

- .....
roles:
- sample_role
- sample_role2
....

Phase 2: Copying a static file

file: tasks/copy-static.yml

---
- name: Copy a file
copy: src=some-file.txt dest=/home/ubuntu/file1.txt
But, do we have sample-file.txt ?? No!!

Static files are kept in files folder of a Role.

Let us create a file in files folder: touch some-file.txt

Phase 2 - Including Task in Main Task File


Include the new task in main.yml file

file: tasks/main.yml

---
- include: nginx.yml

- include: copy-static.yml

Run your master_playbook and check the output: ansible-playbook -i myhosts


master_playbook.yml

Note: To run your master_playbook, you must be present at /home/scrapbook/tutorial

Phase 3 - Creating Template And Variable File

Templates/Dynamic/Configuration files are kept in templates folder of a role.

file: templates/template-file.j2
this is {{item}}
Variables are kept in vars folder

file: vars/main.yml
var_x:
- 'variable x'
var_y:
- 'variable y'

Phase 3 - Creating Task To Send Your Configurations


Let us create a task to send your template/dynamic/configuration file to your host
machine.

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

Phase 3 - Including Task in Main Task File


Include the task you just created in your main file:

file: tasks/main.yml
---
- include: nginx.yml
- include: copy-static.yml
- include: copy-template.yml
Run your master_playbook and check the output: ansible-playbook -i myhosts
master_playbook.yml

Running Role
Let us run the master_playbook and check the output:

ansible-playbook -i myhosts master_playbook.yml


Note: You can enter yes if prompted while running master_playbook

Вам также может понравиться