我在 ubuntu 14.04 中使用 ansible logrotate 定义,其定义如下
---
- 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
这会导致生成具有下面奇怪语法的文件。
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
'
]
}
由于这个语法错误,我每天早上都会收到很多电子邮件。以下是有关服务器的更多详细信息。
$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
答案1
如果我从 github 克隆该模块并创建如下剧本:
- hosts: localhost
roles:
- logrotate
tasks:
- logrotate: name=myapp path=/tmp/myapp.log
args:
options:
- daily
- rotate 8
- postrotate
- exec script
- endscript
它运行正常,并生成如下 logrotate 配置文件:
# Generated by Ansible.
# Local modifications will be overwritten.
/tmp/myapp.log {
daily
rotate 8
postrotate
exec script
endscript
}
如果您看到使用相同剧本的不同行为,您是否会更新您的问题以指出您正在使用哪个版本的 ansible(以及您在哪个平台上运行它)?
顺便说一句,您使用的语法有点奇怪;您将传统的 key=value 语法与首选的 YAML 字典语法混合在一起。它不会对操作产生影响(无论哪种方式,剧本的工作方式都相同),但一般来说,您会这样写:
- hosts: localhost
roles:
- logrotate
tasks:
- logrotate:
name: myapp
path: /tmp/myapp.log
options:
- daily
- rotate 8
- postrotate
- exec script
- endscript