Writing a Playbook
Exercise - Writing a Playbook
The purpose of the playbook we are going to consider now, is to ensure that the httpd
package is installed and the service is enabled and started
Create a file setup_apache.yaml in master terminal window and add the following text to it.
---
- hosts: all
remote_user: test
tasks:
- name: Ensure the HTTPd package is installed
yum:
name: httpd
state: present
become: True
- name: Ensure the HTTPd service is enabled and running
service:
name: httpd
state: started
enabled: True
become: True
Let's run it in the usual way:
$ ansible-playbook -i client.test.org, setup_apache.yaml
$ rpm -qa | grep httpd
$ systemctl status httpd
Exercise - Variables in Playbooks
Create a file variable.yaml in master terminal window and add the following text to it.
---
- hosts: all
remote_user: test
tasks:
- name: Set variable 'name'
set_fact:
name: Test machine
- name: Print variable 'name'
debug:
msg: '{{ name }}'
Let's run it in the usual way:
$ ansible-playbook -i client.test.org, variables.yaml
$ ansible all -i client.test.org, -m setup
Create a file setup_variable.yaml in master terminal window and add the following text to it
---
- hosts: all
remote_user: test
tasks:
- name: Print OS and version
debug:
msg: '{{ ansible_distribution }} {{ ansible_distribution_version }}'
Run it with the following:
$ ansible-playbook -i client.test.org, setup_variables.yaml
As you can see, it printed the OS name and version, as expected, it's also possible to pass a variable using a command-line argument
Create a file cli_variable.yaml in master terminal window and add the following text to it
---
- hosts: all
remote_user: test
tasks:
- name: Print variable 'name'
debug:
msg: '{{ name }}'
Execute it with the following:
$ ansible-playbook -i client.test.org, cli_variables.yaml -e 'name=test01'
In case we forgot to add the additional parameter to specify the variable, we would have executed it as:
$ ansible-playbook -i client.test.org, cli_variables.yaml
Comments
Post a Comment