install and start apache2


In [1]:
import yaml

with open('ansible-playbook-install-apache2.yml') as f:
    y = yaml.load(f)
y


Out[1]:
[{'hosts': '192.168.33.10',
  'sudo': True,
  'tasks': [{'apt': 'name=apache2 state=present update_cache=yes',
    'name': 'install apache2'},
   {'name': 'start apache2', 'service': 'name=apache2 state=restarted'}],
  'user': 'vagrant'}]

In [2]:
!ansible-playbook -v -i inventory ansible-playbook-install-apache2.yml


PLAY [192.168.33.10] ********************************************************** 

GATHERING FACTS *************************************************************** 
ok: [192.168.33.10]

TASK: [install apache2] ******************************************************* 
changed: [192.168.33.10] => {"changed": true, "stderr": "", "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following extra packages will be installed:\n  apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3\n  libaprutil1-ldap ssl-cert\nSuggested packages:\n  www-browser apache2-doc apache2-suexec-pristine apache2-suexec-custom\n  apache2-utils openssl-blacklist\nThe following NEW packages will be installed:\n  apache2 apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3\n  libaprutil1-ldap ssl-cert\n0 upgraded, 8 newly installed, 0 to remove and 151 not upgraded.\nNeed to get 1288 kB of archives.\nAfter this operation, 5342 kB of additional disk space will be used.\nGet:1 http://archive.ubuntu.com/ubuntu/ trusty/main libapr1 amd64 1.5.0-1 [85.1 kB]\nGet:2 http://archive.ubuntu.com/ubuntu/ trusty/main libaprutil1 amd64 1.5.3-1 [76.4 kB]\nGet:3 http://archive.ubuntu.com/ubuntu/ trusty/main libaprutil1-dbd-sqlite3 amd64 1.5.3-1 [10.5 kB]\nGet:4 http://archive.ubuntu.com/ubuntu/ trusty/main libaprutil1-ldap amd64 1.5.3-1 [8634 B]\nGet:5 http://archive.ubuntu.com/ubuntu/ trusty-updates/main apache2-bin amd64 2.4.7-1ubuntu4.4 [843 kB]\nGet:6 http://archive.ubuntu.com/ubuntu/ trusty-updates/main apache2-data all 2.4.7-1ubuntu4.4 [160 kB]\nGet:7 http://archive.ubuntu.com/ubuntu/ trusty-updates/main apache2 amd64 2.4.7-1ubuntu4.4 [87.4 kB]\nGet:8 http://archive.ubuntu.com/ubuntu/ trusty/main ssl-cert all 1.0.33 [16.6 kB]\nPreconfiguring packages ...\nFetched 1288 kB in 17s (74.6 kB/s)\nSelecting previously unselected package libapr1:amd64.\n(Reading database ... 59157 files and directories currently installed.)\nPreparing to unpack .../libapr1_1.5.0-1_amd64.deb ...\nUnpacking libapr1:amd64 (1.5.0-1) ...\nSelecting previously unselected package libaprutil1:amd64.\nPreparing to unpack .../libaprutil1_1.5.3-1_amd64.deb ...\nUnpacking libaprutil1:amd64 (1.5.3-1) ...\nSelecting previously unselected package libaprutil1-dbd-sqlite3:amd64.\nPreparing to unpack .../libaprutil1-dbd-sqlite3_1.5.3-1_amd64.deb ...\nUnpacking libaprutil1-dbd-sqlite3:amd64 (1.5.3-1) ...\nSelecting previously unselected package libaprutil1-ldap:amd64.\nPreparing to unpack .../libaprutil1-ldap_1.5.3-1_amd64.deb ...\nUnpacking libaprutil1-ldap:amd64 (1.5.3-1) ...\nSelecting previously unselected package apache2-bin.\nPreparing to unpack .../apache2-bin_2.4.7-1ubuntu4.4_amd64.deb ...\nUnpacking apache2-bin (2.4.7-1ubuntu4.4) ...\nSelecting previously unselected package apache2-data.\nPreparing to unpack .../apache2-data_2.4.7-1ubuntu4.4_all.deb ...\nUnpacking apache2-data (2.4.7-1ubuntu4.4) ...\nSelecting previously unselected package apache2.\nPreparing to unpack .../apache2_2.4.7-1ubuntu4.4_amd64.deb ...\nUnpacking apache2 (2.4.7-1ubuntu4.4) ...\nSelecting previously unselected package ssl-cert.\nPreparing to unpack .../ssl-cert_1.0.33_all.deb ...\nUnpacking ssl-cert (1.0.33) ...\nProcessing triggers for man-db (2.6.7.1-1) ...\nProcessing triggers for ureadahead (0.100.0-16) ...\nureadahead will be reprofiled on next reboot\nProcessing triggers for ufw (0.34~rc-0ubuntu2) ...\nSetting up libapr1:amd64 (1.5.0-1) ...\nSetting up libaprutil1:amd64 (1.5.3-1) ...\nSetting up libaprutil1-dbd-sqlite3:amd64 (1.5.3-1) ...\nSetting up libaprutil1-ldap:amd64 (1.5.3-1) ...\nSetting up apache2-bin (2.4.7-1ubuntu4.4) ...\nSetting up apache2-data (2.4.7-1ubuntu4.4) ...\nSetting up apache2 (2.4.7-1ubuntu4.4) ...\nEnabling module mpm_event.\nEnabling module authz_core.\nEnabling module authz_host.\nEnabling module authn_core.\nEnabling module auth_basic.\nEnabling module access_compat.\nEnabling module authn_file.\nEnabling module authz_user.\nEnabling module alias.\nEnabling module dir.\nEnabling module autoindex.\nEnabling module env.\nEnabling module mime.\nEnabling module negotiation.\nEnabling module setenvif.\nEnabling module filter.\nEnabling module deflate.\nEnabling module status.\nEnabling conf charset.\nEnabling conf localized-error-pages.\nEnabling conf other-vhosts-access-log.\nEnabling conf security.\nEnabling conf serve-cgi-bin.\nEnabling site 000-default.\n * Starting web server apache2\n * \nSetting up ssl-cert (1.0.33) ...\nProcessing triggers for libc-bin (2.19-0ubuntu6) ...\nProcessing triggers for ureadahead (0.100.0-16) ...\nProcessing triggers for ufw (0.34~rc-0ubuntu2) ...\n"}

TASK: [start apache2] ********************************************************* 
changed: [192.168.33.10] => {"changed": true, "name": "apache2", "state": "started"}

PLAY RECAP ******************************************************************** 
192.168.33.10              : ok=3    changed=2    unreachable=0    failed=0   


In [3]:
!ansible -i inventory 192.168.33.10 --sudo -a 'service apache2 status'


192.168.33.10 | success | rc=0 >>
 * apache2 is running

stop and uninstall apache2


In [9]:
import yaml

with open('ansible-playbook-uninstall-apache2.yml') as f:
    y = yaml.load(f)
y


Out[9]:
[{'hosts': '192.168.33.10',
  'sudo': True,
  'tasks': [{'name': 'stop apache2', 'service': 'name=apache2 state=stopped'},
   {'apt': 'name=apache2 state=absent', 'name': 'uninstall apache2'}],
  'user': 'vagrant'}]

In [10]:
!ansible-playbook -v -i inventory ansible-playbook-uninstall-apache2.yml


PLAY [192.168.33.10] ********************************************************** 

GATHERING FACTS *************************************************************** 
ok: [192.168.33.10]

TASK: [stop apache2] ********************************************************** 
ok: [192.168.33.10] => {"changed": false, "name": "apache2", "state": "stopped"}

TASK: [uninstall apache2] ***************************************************** 
ok: [192.168.33.10] => {"changed": false}

PLAY RECAP ******************************************************************** 
192.168.33.10              : ok=3    changed=0    unreachable=0    failed=0   


In [11]:
!ansible -i inventory 192.168.33.10 --sudo -a 'service apache2 status'


192.168.33.10 | FAILED | rc=3 >>
 * apache2 is not running


In [ ]: