We will to create an playbook sample, then execute it with Ansible.

 

Playbook example

Verify Ansible configuration

Here is my hosts list (2 hosts only right now) :

root@ubuntu15-10:~# cat /etc/ansible/hosts
# here are my Ansible clients :

[redhat_style:children]
fedora
centos

[fedora]
fedora22.argonay.wou

[centos]
centos7.argonay.wou

This configuration works well :

root@ubuntu15-10:~# ansible -m ping redhat_style
centos7.argonay.wou | success >> {
    "changed": false,
    "ping": "pong"
}

fedora22.argonay.wou | success >> {
    "changed": false,
    "ping": "pong"
}

My first playbook

Write this small example :

root@ubuntu15-10:/etc/ansible# cat my_first_playbook.yml
---

- hosts: all
  user: root
  remote_user: root
  sudo: no
  gather_facts: False

tasks:
  - name: retrive hostname
    shell: hostname

  - name: is firewall service running ?
    service: name=firewalld state=started enabled=yes

  - name: security check for "/etc/passwd"
    lineinfile: dest=/etc/passwd line=root mode="u=rw,g=r,o=r" regexp=/^root\:x\:0\:0 state=present

Where :

  • I get the hostname of the Ansible clients.
  • I check if the “firewalld” service is running
  • And  I very “/etc/passwd” file rights and content

Playbook mode run

Now, launch this playbook :

root@ubuntu15-10:~# ansible-playbook /etc/ansible/my_first_playbook.yml

PLAY [all] ********************************************************************

TASK: [retrive hostname] ******************************************************
changed: [centos7.argonay.wou]
changed: [fedora22.argonay.wou]

TASK: [is firewall service running ?] *****************************************
ok: [centos7.argonay.wou]
ok: [fedora22.argonay.wou]

TASK: [security check for "/etc/passwd"] **************************************
changed: [centos7.argonay.wou]
changed: [fedora22.argonay.wou]

PLAY RECAP ********************************************************************
centos7.argonay.wou : ok=3 changed=2 unreachable=0 failed=0
fedora22.argonay.wou : ok=3 changed=2 unreachable=0 failed=0

 

 

www.pdf24.org    Send article as PDF   

Leave a Reply

Your email address will not be published. Required fields are marked *


*