Ansible playbook variables
we can declare and define a variable in playbooks.
within the file declaring and defining a variable
vars: pName: tree--- - hosts: all connection: ssh vars: pName: tree tasks: - name: installing tree in slave-1 action: yum name='{{pName}}' state=present ...passing variable at runtime
--- - hosts: all connection: ssh vars: pName: tree tasks: - name: installing tree in slave-1 action: yum name='{{pName}}' state='{{xyz}}' ... one.ymlansible-playbook one.yml --extra-vars "pName=tree xyz=absent"using different file
set_fact ==> used to declare a variable in a file so that we can use that variable in other files.
include ==> to execute a different file from the current file and also we can import variables.
vim two.yml --- - set_fact: abc=tree - name: uninstalling from two yum: name: '{{abc}}' state: absent ...
vim one.yml
---
- hosts: all
connection: ssh
tasks:
- include: two.yml
- name: installing tree from one.yml
action: yum name='{{abc}}' state=present
...
- include: two.yml --> first it goes to the two.yml and runs that file then it'll do the next operations in the current file.
Installing and starting service
here we are going to write a playbook to install httpd server and start the service.
service:
name: httpd
state: started
---
- hosts: all
connection: ssh
tasks:
- name: installing https
yum:
name: httpd
state: present
- name: starting httpd
service:
name: httpd
state: started
...
to start any service we need to use
service: name=<<service_name>> state=<<restarted/started>>
this can also be written as key-value pairs.