including variables in Ansible

=====================================
[mohammedrafi@ansible1 ~]$ mkdir nginx
=====================================
[mohammedrafi@ansible1 ~]$ cd nginx
[mohammedrafi@ansible1 nginx]$ mkdir tasks vars

=====================================
[mohammedrafi@ansible1 nginx]$ touch tasks/environment.yml
=====================================
[mohammedrafi@ansible1 nginx]$ touch vars/variables.yml
=====================================
[mohammedrafi@ansible1 ~]$ tree nginx
nginx
|– nginx.yml
|– tasks
| `– environment.yml
`– vars
`– variables.yml
=====================================
[mohammedrafi@ansible1 nginx]$ vim tasks/environment.yml

– name: install package
yum:
name: “{{ package }}”
state: latest
– name: start service
service:
name: “{{ service }}”
state: “{{ svc_state }}”
=====================================
[mohammedrafi@ansible1 nginx]$ vim vars/variables.yml

firewall_pkg: firewalld
=====================================
[mohammedrafi@ansible1 nginx]$ vim nginx.yml

– name: anything
hosts: db
remote_user: mohammedrafi
become: true
become_method: sudo
become_user: root
vars:
rule: http
tasks:
– name: Include the variables from yaml file
include_vars: vars/variables.yml

– name: include env variables and set the variables
include: tasks/environment.yml
vars:
package: nginx
service: nginx
svc_state: started

– name: install fiirewall pkg
yum:
name: “{{ firewall_pkg }}”
state: latest
– name: start firewalld
service:
name: firewalld
state: started
enabled: true
=====================================
[mohammedrafi@ansible1 nginx]$ ansible-playbook –syntax-check nginx.yml

playbook: nginx.yml
=====================================
[mohammedrafi@ansible1 nginx]$ ansible-playbook nginx.yml

PLAY [anything] ****************************************************************

TASK [setup] *******************************************************************
ok: [104.198.26.13]

TASK [Include the variables from yaml file] ************************************
ok: [104.198.26.13]

TASK [install package] *********************************************************
ok: [104.198.26.13]

TASK [start service] ***********************************************************
ok: [104.198.26.13]

TASK [install fiirewall pkg] ***************************************************
ok: [104.198.26.13]

TASK [start firewalld] *********************************************************
ok: [104.198.26.13]

PLAY RECAP *********************************************************************
104.198.26.13 : ok=6 changed=0 unreachable=0 failed=0
=====================================

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s