-1

I am using ansible logrotate definition for logrotate in ubuntu 14.04 which is having definition as below

---

- name: dependencies
  apt: pkg={{item}} state=latest
  with_items:
    - unzip
    - jq

- name: check if already downloaded
  stat: path={{nomad_download_folder}}/{{nomad_archive}}
  register: nomad_archive_stat

- name: download
  get_url: >
    url={{nomad_download}}
    dest={{nomad_download_folder}}
    sha256sum={{nomad_checksum}}
  register: nomad_downloaded
  when: nomad_archive_stat.stat.exists == false

- name: group
  group: >
    name={{nomad_group}}
    state=present
  register: nomad_group_created

# On Nomad schedulers
- name: user
  user: >
    home={{nomad_home}}
    name={{nomad_user}}
    system=yes
    groups={{nomad_group}}
    append=yes
  when: (nomad_group_created | changed) and (nomad_is_server == true)

# On Nomad runners
- name: user
  user: >
    home={{nomad_home}}
    name={{nomad_user}}
    system=yes
    groups={{nomad_group}},docker
    append=yes
  when: (nomad_group_created | changed) and (nomad_is_server == false)

- name: directories
  file: >
    state=directory
    path={{item}}
    owner={{nomad_user}}
    group={{nomad_group}}
  with_items:
    - "{{nomad_home}}"
    - "{{nomad_home}}/bin"
    - "{{nomad_config_dir}}"

- name: check for log directory
  stat: path={{nomad_log_file | dirname}}
  register: nomad_log_directory_stat

- name: create log directory
  file: >
    state=directory
    path={{nomad_log_file | dirname}}
    owner={{nomad_user}}
    group={{nomad_group}}
  when: not nomad_log_directory_stat.stat.exists

- name: touch log file
  file: >
    state=touch
    path={{nomad_log_file}}
    owner={{nomad_user}}
    group={{nomad_group}}
  changed_when: false

- name: install
  unarchive: >
    src={{nomad_download_folder}}/{{nomad_archive}}
    dest={{nomad_home}}/bin
    copy=no
  when: nomad_downloaded | changed

- name: link executable in PATH
  file: >
    state=link
    src={{nomad_home}}/bin/nomad
    dest=/usr/local/bin/nomad

- name: set ownership
  file: >
    state=directory
    path={{nomad_home}}
    owner={{nomad_user}}
    group={{nomad_group}}
    recurse=yes
  when: nomad_downloaded | changed

- name: nomad config file
  template: >
    src=nomad.conf.j2
    dest={{nomad_config_file}}
    owner={{nomad_user}}
    group={{nomad_group}}
    mode=0755
  notify:
    - restart nomad

- name: copy nomad upstart script
  template: >
    src=nomad.upstart.conf.j2
    dest=/etc/init/nomad.conf
    owner={{nomad_user}}
    group={{nomad_group}}
    mode=0755
  notify:
    - restart nomad

- name: rotate log file
  logrotate: name=nomad path={{nomad_log_file}}
  args:
    options:
      - daily
      - missingok
      - rotate 3
      - compress
      - delaycompress
      - copytruncate
      - notifempty

Which causes to generate file with weird syntax which is below.

sudo cat /etc/logrotate.d/nomad

# Generated by Ansible.
# Local modifications will be overwritten.

/var/log/nomad.log {
  [
  '
  d
  a
  i
  l
  y
  '
  ,

  '
  m
  i
  s
  s
  i
  n
  g
  o
  k
  '
  ,

  '
  r
  o
  t
  a
  t
  e

  3
  '
  ,

  '
  c
  o
  m
  p
  r
  e
  s
  s
  '
  ,

  '
  d
  e
  l
  a
  y
  c
  o
  m
  p
  r
  e
  s
  s
  '
  ,

  '
  c
  o
  p
  y
  t
  r
  u
  n
  c
  a
  t
  e
  '
  ,

  '
  n
  o
  t
  i
  f
  e
  m
  p
  t
  y
  '
  ]
}

Due to this syntax error I am getting so many emails every morning. Below are more details about server.

$sudo ansible --version
ansible 2.2.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
$lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.4 LTS
Release:    14.04
Codename:   trusty
Shailesh Sutar
  • 1,517
  • 5
  • 23
  • 41

1 Answers1

2

If I clone that module from github and create a playbook like this:

- hosts: localhost
  roles:
    - logrotate
  tasks:
    - logrotate: name=myapp path=/tmp/myapp.log
      args:
        options:
          - daily
          - rotate 8
          - postrotate
          - exec script
          - endscript

It runs just fine, and results in a logrotate configuration file that looks like:

# Generated by Ansible.
# Local modifications will be overwritten.
/tmp/myapp.log {
  daily
  rotate 8
  postrotate
  exec script
  endscript
}

If you're seeing different behavior using an identical playbook, would you update your question to indicate which version of ansible you're using (and on what platform you're running it)?

Incidentally, the syntax you're using is a little odd; you're mixing the legacy key=value syntax with the preferred YAML dictionary syntax. It doesn't have an operational impact (the playbook works the same either way), but in general you would write it like this:

- hosts: localhost
  roles:
    - logrotate
  tasks:
    - logrotate:
        name: myapp
        path: /tmp/myapp.log
        options:
          - daily
          - rotate 8
          - postrotate
          - exec script
          - endscript
larsks
  • 43,623
  • 14
  • 121
  • 180
  • What is the `logrotate` role from your example? Why did you include it? – techraf Feb 09 '17 at 06:28
  • That is the logrotate role referenced by Shailesh Sutar in the question, and I included it because that's how you use a role. In this case, that ensures that logrotate is installed via the role's `tasks/main.yml` and also activates the roles `library` directory. – larsks Feb 09 '17 at 12:23
  • @larsks It works for me when I run ansible-playbook the way you did however It doesn't work for me in production where it is deployed. – Shailesh Sutar Feb 10 '17 at 11:33
  • 1
    @ShaileshSutar you will need to update your question with an example that clearly reproduces the problem. Trying to put that together may actually lead you to the source of your problem. – larsks Feb 10 '17 at 17:08