How To Run Ansible Playbook Locally or Localhost Machine-DecodingDevOps

How To Run Ansible Playbook Locally or Localhost Machine-DecodingDevOps

we can run ansible playbook locally or in ansible control machine in different ways those are

  • using localhost in ansible playbook host argument
  • by passing variable in command line for hosts argument.

Using localhost in ansible playbook hosts argument

we can run ansible playbook locally or in ansible control machine by mentioning ‘localhost’ in ansible playbook in hosts argument. In the hosts argument mention localhost so this entire playbook will run on locally or ansible control machine.

root@ip-172-31-37-35:~# cat local.yml
---
- hosts: localhost
gather_facts: no

tasks:
- name: run ansible playbook locally
debug:
msg: "Hi This Is DecodingDevOps"

This playbook will run on your local machine.

Note: you can also mention 127.0.0.1 in the place of localhost the playbook, Both localhost and 127.0.0.1 represents local machine only.

Output Log

root@ip-172-31-37-35:~# ansible-playbook local.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [localhost] ***********************************************************************************************************************************************************

TASK [run ansible playbook locally] ****************************************************************************************************************************************
ok: [localhost] => {
"msg": "Hi This Is DecodingDevOps"
}

PLAY RECAP *****************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

By passing variable in command line for hosts argument

we can modify the same above playbook using variables, and we have to pass the variable value through command line. in the below playbook i variablized the hosts argument with variable “my_hosts”. you can mention any name here, but you have to pass the same variable in command line.

root@172-31-37-35:~# cat local.yml
---
- hosts: "{{ my_hosts }}"
gather_facts: no

tasks:
- name: run ansible playbook locally
debug:
msg: "Hi This Is DecodingDevOps"

Run Ansible Playbook.

ansible-playbook local.yml -e "my_hosts=localhost"

here i passed the my_hosts value equals to localhost. This value will be replaced in playbook.

Output

root@ip-172-31-37-35:~# ansible-playbook local.yml -e "my_hosts=localhost"

PLAY [localhost] ***********************************************************************************************************************************************************

TASK [run ansible playbook locally] ****************************************************************************************************************************************
ok: [localhost] => {
"msg": "Hi This Is DecodingDevOps"
}

PLAY RECAP *****************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  • run ansible playbook locally
  • run ansible playbook in ansible control machine
  • ansible playbook locally
  • best ansible training in hyderabad
  • how to run ansible playbook on localhost
  • ansible playbook in ansible control machine.

Leave a Reply

Your email address will not be published. Required fields are marked *