Installing and learning Ansible

I am learning Ansible and am documenting my progress. I built two Centos 7 test servers, the main one is called Ansible and the remote server is test1.

To install Ansible first add the EPEL Repository for Centos 7

rpm -iUvh

Now you can install it.

yum install Ansible

Check for version installed.

# ansible –version
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides

Create an inventory file, I named the file hosts. The remote server is called test1. I created a group called web-server, this will be helpful if I have more than one server. I can then reference it by the group name.

# cat hosts


Below is the content of my first playbook called site.yml, I have been appending to it so it continues to grow as I learn Ansible.

# cat site.yml
– name: Install Packages
hosts: web-server
remote_user: root

– name: Installing Latest version of Apache
yum: name=httpd state=present

– name: Copy default web page
template: src=/root/work/index.html dest=/var/www/html
owner=apache group=apache mode=0644

– name: Start httpd
service: name=httpd state=restarted enabled=yes

– name: Install all PHP packages
yum: name={{item}} state=present
– php
– php-fpm
– php-mysql
– php-xml

– name: Install MySQL
yum: name=mysql-server state=present

MySQL has been replaced with MaridDb in Centos 7. Since I am familiar with MySQL I decided to install that instead. I had to install the repository for MySQl to do that.

Download mysql community repository and install it. I did this on the remote server test1.

rpm -ivh mysql-community-release-el7-5.noarch.rpm

To run the playbook use the command ansible-playbook. To prompt for the password use –ask-pass.

# ansible-playbook –ask-pass -i ./hosts site.yml
SSH password:

PLAY [Install Packages] ********************************************************

TASK [setup] *******************************************************************
ok: [test1]

TASK [Installing Latest version of Apache] *************************************
changed: [test1]

TASK [Copy default web page] ***************************************************
changed: [test1]

TASK [Start httpd] *************************************************************
changed: [test1]

TASK [Install all PHP packages] ************************************************
changed: [test1] => (item=[u’php’, u’php-fpm’, u’php-mysql’, u’php-xml’])

TASK [Install MySQL] ***********************************************************
changed: [test1]

PLAY RECAP *********************************************************************
test1 : ok=6 changed=5 unreachable=0 failed=0

About Andrew Lin

Hi, I have always wanted to creat a blog site but never had the time. I have been working in Information Technology for over 15 years. I specialize mainly in networks and server technologies and dabble a little with the programming aspects. Andrew Lin

View all posts by Andrew Lin →