FreeKB - ansible-playbook command - Getting Started
ansible-playbook command - Getting Started

Home > Search


This assumes you have installed Ansible Tower. The ansible-playbook command can can be used to perform a number of different tasks. By default, the ansible-playbook command is located at /usr/bin/ansible-playbook on Linux. For example, the -v or --version option can be used to list the version of Ansible Playbook, the location of the Ansible configuration file, and the version of Python.

ansible-playbook --version

ansible-playbook 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/your_username/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible playbook module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.5 (default, mm dd yyyy, hh:mm:ss) [GCC 4.8.5 yyyymmdd (Red Hat 4.8.5-39)]

 

Let's say you want to ping target servers. First, you will need to create an inventory.yml file that contains the target hosts

all:
  children:
    windows:
      hosts:
        server1.example.com:
        server2.example.com:
    linux:
      hosts:
        server3.example.com:
        server4.example.com:

 

Next you will create a playbook .yml file. Let's say the name of the file is ping.yml. The ping.yml file could have the following.

  • all will perform the task against every server in inventory.yml, "windows" will perform the task against the Windows servers in inventory.yml, "linux" will perform the task against the Linux servers in inventory.yml.
---
- hosts: all
  remote_user: root
  tasks:
    - name: ping test
      ping:
      remote_user: root

 

The playbook can then be executed using this command.

ansible-playbook ping.yml -i inventory.yml

 

The following should be returned. Check out this article if you want to know what Gathering Facts is all about.

PLAY [all]

TASK [Gathering Facts]
ok: [server1.example.com]
ok: [server2.example.com]
ok: [server3.example.com]
ok: [server4.example.com]

TASK [ping test]
ok: [server1.example.com]
ok: [server2.example.com]
ok: [server3.example.com]
ok: [server4.example.com]

PLAY RECAP
server1.example.com : ok=2  changed=0  unreacable=0  failed=0
server2.example.com : ok=2  changed=0  unreacable=0  failed=0
server3.example.com : ok=0  changed=0  unreacable=1  failed=0
server4.example.com : ok=2  changed=0  unreacable=0  failed=0

 

Indentation matters in playbooks. For example, this will not work, as it does not have the expected indentation.

- hosts: all
tasks:
- debug: msg="improper indentation"

 

This will work, as it has the proper indentation.

- hosts: all
  tasks:
    - debug: msg="improper indentation"

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter f0335 in the box below so that we can be sure you are a human.




Comments