Академический Документы
Профессиональный Документы
Культура Документы
---
- 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 }}
#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
{% endfor %}
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
Registered Variable
Ansible allows you to save the output of a task in a variable at run time, through
register keyword.
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
---
hosts: all
tasks:
command: ls /home/ubuntu
register: contents
register variable stores the output, after executing command module, in contents
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
untagged: run only those tags which do not have any tags
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
file: tasks/main.yml
---
- include: nginx.yml
As you can see, tasks/main.yml is just list of tasks.
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?
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
....
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!!
file: tasks/main.yml
---
- include: nginx.yml
- include: copy-static.yml
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'
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
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: