对于Ansible,我有一个angular色,设置时区并填充(Ubuntu)基本系统的设置,
- name: set timezone copy: content='Europe/Berlin' dest=/etc/timezone owner=root group=root mode=0644 backup=yes - name: update timezone command: dpkg-reconfigure --frontend noninteractive tzdata
无论如何,这两个命令都被执行。 这意味着当Ansible针对同一个目标运行两次时,结果汇总中仍会得到一个changed=2 ,
default : ok=41 changed=2 unreachable=0 failed=0
理想情况下,第二次运行应该都可以。
虽然我猜测update timezone应该对set timezone有一定的依赖关系,但我不太确定如何最好地实现这一点。
你可以通过使用register when changed 。
通过使用寄存器 , copy命令的结果被保存到variables中。 然后可以使用此variables在更新时区任务中创buildwhen条件。
此外,请确保在时区内容的末尾添加换行符\n , 否则Ansible将始终执行该副本 。
- name: set timezone copy: content='Europe/Berlin\n' dest=/etc/timezone owner=root group=root mode=0644 backup=yes register: timezone - name: update timezone command: dpkg-reconfigure --frontend noninteractive tzdata when: timezone.changed
但是,您也可以通过为dpkg-reconfigure命令创build一个handler来解决此问题, 如下所述 :
tasks: - name: Set timezone variables copy: content='Europe/Berlin\n' dest=/etc/timezone owner=root group=root mode=0644 backup=yes notify: - update timezone handlers: - name: update timezone command: dpkg-reconfigure --frontend noninteractive tzdata
您只需要为copy播放注册一个variables,然后检查它是否已经changed 。
例如:
- name: make a file copy: ... register: whatever - name: run a command command: ... when: whatever.changed
根据所使用的Ubuntu版本,使用systemdfunction也可能是有用的:
- name: Set timezone command: timedatectl set-timezone Europe/Berlin changed_when: false
要在Ubuntu上使用Ansible(> = 1.6)设置时区,请使用locale_gen命令。
- name: set timezone locale_gen: name=Europe/Berlin state=present
注意: locale_gen是locale_gen当前附带的额外模块。 它可能会在未来的版本中删除。