Using Ansible

sbalasa

Santhosh Balasa

Posted on May 31, 2021

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

💖 💪 🙅 🚩
sbalasa
Santhosh Balasa

Posted on May 31, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related