Category Archives: ansible-playbook

Ansible Module "lineinfile" replace multiple lines in several files

our SSL certificate runs out in a couple of days. So I thought Ansible can put the new certs on the server und change the apache2 sites.

Serveral sites are running on this server.

I want to replace the following lines:

  • SSLCertificateChainFile
  • SSLCertificateKeyFile
  • SSLCertificateFile

I use this command to get a list of all sites in /etc/apache2 where the pattern "SSLCertificate" exists.

- name: Apache 2.2 list sites files and store it in register
  command: grep -lR --exclude default-ssl "SSLCertificate" /etc/apache2/
  register: apache22_sites

This is what I use, when only one file has to be changed:

- name: apache2.2.* | configure certs
  lineinfile: dest=/path/to/...  regexp={{ item.regexp }} line={{ item.line}} backrefs=yes
  with_items:
        - { regexp: "SSLCertificateChainFile", line: "    SSLCertificateChainFile = ..." }
        - { regexp: "SSLCertificateKeyFile ", line: "    SSLCertificateKeyFile = ..." }
        - { regexp: "SSLCertificateFile", line: "    SSLCertificateFile = ..."
  notify: reload apache2

How can i tell ansible to use this code with multiple files listed in variable "apache22_sites" and multiples lines?

I found a good hint here, bad sadly only for one line.

I appreciate any tipps, tricks, hints :)

Greetings Dennis

Vagrant – Ansible error installing Apache

I'm working on a project with Vagrant and Ansible and Virtualbox. When I try to install Apache on an ubuntu precise (14.04) box, Vagrant fails. I improved the answer after.

It seems a known bug, but even if I'm installing a newer version, the error shows up. I tried also as stated here, but with no luck.

How can I resolve this issue?

Thank you.


UPDATED ANSWER

This is the Ansible task. Version 1:

- name: Install Apache
  sudo: yes
  apt: pkg=apache2 state=latest
  register: apache2_apt

Output:

failed: [default] => {"failed": true}
stderr: E: Sub-process /usr/bin/dpkg returned an error code (1)

stdout: Reading package lists...
Building dependency tree...
Reading state information...
Suggested packages:
  www-browser apache2-doc apache2-suexec-pristine apache2-suexec-custom
The following NEW packages will be installed:
  apache2
0 upgraded, 1 newly installed, 0 to remove and 183 not upgraded.
Need to get 0 B/146 kB of archives.
After this operation, 460 kB of additional disk space will be used.
(Reading database ... 52932 files and directories currently installed.)
Unpacking apache2 (from .../apache2_2.4.12-1+deb.sury.org~precise+5_amd64.deb) ...
dpkg: error processing /var/cache/apt/archives/apache2_2.4.12-1+deb.sury.org~precise+5_amd64.deb (--unpack):
 error setting ownership of `/var/www/html.dpkg-new': Operation not permitted
Processing triggers for man-db ...
Processing triggers for ureadahead ...
Errors were encountered while processing:
 /var/cache/apt/archives/apache2_2.4.12-1+deb.sury.org~precise+5_amd64.deb

msg: '/usr/bin/apt-get -y -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold"   install 'apache2'' failed: E: Sub-process /usr/bin/dpkg returned an error code (1)


FATAL: all hosts have already failed -- aborting

Version 2:

- name: Install Apache
  command: "sudo apt-get install apache2"
  register: apache2_apt

Output:

failed: [default] => {"changed": true, "cmd": ["sudo", "apt-get", "install", "apache2"], "delta": "0:00:07.745095", "end": "2015-06-09 11:08:53.726031", "rc": 100, "start": "2015-06-09 11:08:45.980936", "warnings": []}
stderr: E: Sub-process /usr/bin/dpkg returned an error code (1)
stdout: Reading package lists...
Building dependency tree...
Reading state information...
Suggested packages:
  www-browser apache2-doc apache2-suexec-pristine apache2-suexec-custom
The following NEW packages will be installed:
  apache2
0 upgraded, 1 newly installed, 0 to remove and 183 not upgraded.
Need to get 0 B/146 kB of archives.
After this operation, 460 kB of additional disk space will be used.
(Reading database ... 52932 files and directories currently installed.)
Unpacking apache2 (from .../apache2_2.4.12-1+deb.sury.org~precise+5_amd64.deb) ...
dpkg: error processing /var/cache/apt/archives/apache2_2.4.12-1+deb.sury.org~precise+5_amd64.deb (--unpack):
 error setting ownership of `/var/www/html.dpkg-new': Operation not permitted
Processing triggers for man-db ...
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Errors were encountered while processing:
 /var/cache/apt/archives/apache2_2.4.12-1+deb.sury.org~precise+5_amd64.deb

FATAL: all hosts have already failed -- aborting