Having installed Ansible with our packet manager, the first thing to do is to insert into /etc/ansible_hosts the server we want to manage.

For example

[all:vars]
ruser=root

[dns]
prd-dns-0

[logs]
prd-logs-0

[mail]
prd-mail-1

[mysql]
prd-mysql-1

[frontend]
prd-webfrontend-1

[web]
prd-web-[1:2]

With this in mind, let’s define in the .bashrc file of the user we want to use (in this case root) the env variable $ANSIBLE_HOSTS  with the value ANSIBLE_HOSTS=/etc/ansible_hosts.
The step implied here is that in every node managed from Ansible the user root has an ssh-passwordless secure connection to use.

Now is time to write our first playbook

---
- name: Prepare NFS for VM
  remote_user: "{{ ruser }}"
  hosts: logs
  tasks:
    - name: mountpoint /store
      mount: name=/store src=xx.xx.xx.xx:/store/prd-logs-0 fstype=nfs4 state=present
    - name: install ntf-utils
      yum: name=nfs-utils state=present

As you can  see we are using ruser and hosts from our ansible_host file in to parametrize the node we want.

Before executing the playbook it’s a good idea to check the syntax

[root@xxxxx playbooks]# ansible-playbook prd-logs-0.yml --check

PLAY [Prepare NFS for VM] *************************************************************

GATHERING FACTS ***************************************************************
ok: [prd-logs-0]

TASK: [mountpoint /store] *****************************************************
skipping: [prd-logs-0]
ok: [prd-logs-0]

TASK: [install ntf-utils] *****************************************************
ok: [prd-logs-0]

PLAY RECAP ********************************************************************
prd-logs-0                 : ok=2    changed=0    unreachable=0    failed=0

And that is the “go go” from Ansible, without errors.

To launch the playbook for real just substitute the check option with a -f X where X is the parallelism that you want (usually is the number of vms you want to change at one time)

This was the first of, hopefully, many howtos on Ansible.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s