Using ad-hoc commands is an imperative method and is no better than using the SSH client to execute commands remotely.
You need two components to make it into real, imperative orchestration: a playbook and modules. The playbook is the basis for your system’s deployment, configuration, and maintenance. It can orchestrate everything, even between hosts! A playbook is there to describe the state you want to reach. Playbooks are written in the YAML language and can be executed with the ansible-playbook command:
$ ansible-playbook [filename]
The second component is the module. The best way to describe a module is: the task to be executed to reach the desired state. They are also known as task plugins or library plugins.
If you encounter the below error while running the ansible-playbook command:
ansible-playbook: command not found
you may try installing the below package as per your choice of distribution.
|OS X||brew install ansible|
|Debian||apt-get install ansible|
|Ubuntu||apt-get install ansible|
|Alpine||apk add ansible|
|Arch Linux||pacman -S ansible|
|Kali Linux||apt-get install ansible|
|CentOS||yum install ansible|
|Fedora||dnf install ansible|
|Raspbian||apt-get install ansible|
ansible-playbook Command Examples
1. Run tasks in playbook:
$ ansible-playbook playbook
2. Run tasks in playbook with custom host inventory:
$ ansible-playbook playbook -i inventory_file
3. Run tasks in playbook with extra variables defined via the command-line:
$ ansible-playbook playbook -e "variable1=value1 variable2=value2"
4. Run tasks in playbook with extra variables defined in a JSON file:
$ ansible-playbook playbook -e "@variables.json"
5. Run tasks in playbook for the given tags:
$ ansible-playbook playbook --tags tag1,tag2
6. Run tasks in a playbook starting at a specific task:
$ ansible-playbook playbook --start-at task_name
7. To check the Ansible syntax, use:
$ ansible-playbook --syntax-check Ansible/example1.yaml