Including variables in Ansible
=====================================
[root@ansible1 ~]$ mkdir nginx
=====================================
[root@ansible1 ~]$ cd nginx
[root@ansible1 nginx]$ mkdir tasks vars
=====================================
[root@ansible1 nginx]$ touch tasks/environment.yml
=====================================
[root@ansible1 nginx]$ touch vars/variables.yml
=====================================
[root@ansible1 ~]$ tree nginx
nginx
|– nginx.yml
|– tasks
| `– environment.yml
`– vars
`– variables.yml
=====================================
[root@ansible1 nginx]$ vim tasks/environment.yml
—
– name: install package
yum:
name: “{{ package }}”
state: latest
– name: start service
service:
name: “{{ service }}”
state: “{{ svc_state }}”
=====================================
[root@ansible1 nginx]$ vim vars/variables.yml
—
firewall_pkg: firewalld
=====================================
[root@ansible1 nginx]$ vim nginx.yml
—
– name: anything
hosts: db
remote_user: root
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
=====================================
[root@ansible1 nginx]$ ansible-playbook –syntax-check nginx.yml
playbook: nginx.yml
=====================================
[root@ansible1 nginx]$ ansible-playbook nginx.yml
PLAY [anything] ****************************************************************
TASK [setup] *******************************************************************
ok: [192.168.1.23]
TASK [Include the variables from yaml file] ************************************
ok: [192.168.1.23]
TASK [install package] *********************************************************
ok: [192.168.1.23]
TASK [start service] ***********************************************************
ok: [192.168.1.23]
TASK [install fiirewall pkg] ***************************************************
ok: [192.168.1.23]
TASK [start firewalld] *********************************************************
ok: [192.168.1.23]
PLAY RECAP *********************************************************************
192.168.1.23 : ok=6 changed=0 unreachable=0 failed=0
=====================================
Recent Comments