DEV Community

loading...

Using Ansible

Santhosh Balasa
Passionate Senior Full Stack Developer around 10+ yrs of IT experience.
・1 min read

-> 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

Discussion (1)

Collapse
arvindpdmn profile image
Arvind Padmanabhan • Edited

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