DEV Community

Santhosh Balasa
Santhosh Balasa

Posted on • Updated on

Using Ansible

-> Command to validate Ansible playbook syntax:

ansible-playbook <playbook.yml> --syntax-check
Enter fullscreen mode Exit fullscreen mode

-> Sample playbook to run command in localhost:

---

- name:Sample Ansible playbook 
  hosts: 127.0.0.1
  connection: local

  tasks:
    - name: Display hostname
      command: hostname
      register: out

    - debug: var=out.stdout_lines
Enter fullscreen mode Exit fullscreen mode

-> Run a playbook:

ansible-playbook <playbook.yml>
Enter fullscreen mode Exit fullscreen mode

-> Run multiple commands using playbook:

---

- name: Run multiple commands
  hosts: 127.0.0.1
  connection: local

  vars:
    unix_command: ls

  tasks:
    - name: Display name of the host
      command: hostname
      register: task1

    - name: Execute {{ unix_command }}
      command: {{ unix_command }}
      register: task2

    - debug: var=task1.stdout_lines
    - debug: var=task2.stdout_lines
Enter fullscreen mode Exit fullscreen mode

-> For more info refer: https://spacelift.io/blog/ansible-playbooks

Discussion (1)

Collapse
arvindpdmn profile image
Arvind Padmanabhan • Edited on

Article is a good intro to playbooks. Beginners looking for a more basic intro to Ansible can see devopedia.org/ansible