---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
hosts: centos
user: root
# Vars: variables that will apply to the play, on all target systems
# Tasks: the list of tasks that will be executed within the playbook
tasks:
- name: Configure a MOTD (message of the day)
copy:
src: centos_motd
dest: /etc/motd
# Handlers: the list of handlers that are executed as a notify key from a task
# Roles: list of roles to be imported into the play
# Three dots indicate the end of a YAML document
...
---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
hosts: linux
# Vars: variables that will apply to the play, on all target systems
vars:
motd_centos: "Welcome to CentOS Linux - Ansible Rocks\n"
motd_ubuntu: "Welcome to Ubuntu Linux - Ansible Rocks\n"
# Tasks: the list of tasks that will be executed within the playbook
tasks:
- name: Configure a MOTD (message of the day)
copy:
content: "{{ motd_centos }}"
dest: /etc/motd
notify: MOTD changed
when: ansible_distribution == "CentOS"
- name: Configure a MOTD (message of the day)
copy:
content: "{{ motd_ubuntu }}"
dest: /etc/motd
notify: MOTD changed
when: ansible_distribution == "Ubuntu"
# Handlers: the list of handlers that are executed as a notify key from a task
handlers:
- name: MOTD changed
debug:
msg: The MOTD was changed
# Roles: list of roles to be imported into the play
# Three dots indicate the end of a YAML document
...
So in here I've specified hosts as linux so that it will pick both centos and ubuntu hosts. And the motd has been defined as a variable for each os. Then I've added two tasks with a condition to execute based on the ansible_distribution. Finally a handler to inform us after exeuting each task.
Facts
Ansible facts are host specific confiruations presented to us as variables. Basically when ansible executes it's setup module we can usually notice that it collects facts. In order to get an understanding of how the facts are collected let's execute this command.
In the output of above command you can notice that there's a root dictionary named ansible_facts. Ansible itself will populate all the key value pairs of this dictionary at the root of each hosts variable section, so that these are accessible without any prefix. For example, if we create a file like below,
# facts_playbook.yaml
---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
hosts: all
# Tasks: the list of tasks that will be executed within the play, this section
# can also be used for pre and post tasks
tasks:
- name: Show IP Address
debug:
msg: "{{ ansible_default_ipv4.address }}"
# Three dots indicate the end of a YAML document
...
You can notice that the IPv4 address of the host is accessed in ansible_default_ipv4.address form, where it actually is represented in ansible_facts.ansible_default_ipv4.address in the facts. Now if we run the playbook we might be able to see the IPv4 address of each host specified in our host file.
It is also possible to define custom facts in ansible that will be gatherred during the fact gathering. A custom fact can be any script that returns a JSON structure or ini file. By default it expects to keep these custom facts in /etc/ansible/facts.d/.
In order to try this our let's create two files inside /etc/ansible/facts.d/.
getdate1.fact:
#!/bin/bash
echo {\""date\"" : \""`date`\""}
and
gatedate2.fact:
#!/bin/bash
echo [date]
echo date=`date`
Now if we execute below,
$ ansible ubuntu-c -m setup | more
We'll get the data output from the ansible_local section.