Ansible 将2个剧本制作成1个剧本

Ansible 将2个剧本制作成1个剧本,ansible,Ansible,因此,我使用ansible playbook在100多台服务器上修复我的logrotate,每台服务器都有5-7个logrotate.d项需要更改。我知道如何用一个项目来修复它,但我似乎不知道如何将多个项目放入剧本中进行更改,逻辑告诉我只需将它们列在彼此下面,但随后它开始跳过除第一个项目之外的所有内容。所以我会在这里包括两个单独的剧本,有人能告诉我如何把它们变成一个剧本吗 我试着列出-name部分,但这不起作用 --- - hosts: '{{ server }}' remote_user:

因此,我使用ansible playbook在100多台服务器上修复我的logrotate,每台服务器都有5-7个logrotate.d项需要更改。我知道如何用一个项目来修复它,但我似乎不知道如何将多个项目放入剧本中进行更改,逻辑告诉我只需将它们列在彼此下面,但随后它开始跳过除第一个项目之外的所有内容。所以我会在这里包括两个单独的剧本,有人能告诉我如何把它们变成一个剧本吗

我试着列出-name部分,但这不起作用

---
- hosts: '{{ server }}'
  remote_user: ansip
  become: yes
  become_method: sudo

  vars_prompt:
    - name: "server"
      prompt: "Enter server name or all"
      private: no

  tasks:
  roles:
    - role: ansible-role-logrotate
      logrotate:
        compress: true
        create: true
        weekly: true
        dateext: true
        include: /etc/logrotate.d
        rotate: 3
        shred: true
      logrotate_d:
        - name: samba
          files:
            - /var/log/samba/*
          options:
            compress: true
            dateext: true
            rotate: 3

- hosts: '{{ server }}'
  remote_user: ansip
  become: yes
  become_method: sudo

  tasks:
  roles:
    - role: ansible-role-logrotate
      logrotate:
        compress: true
        create: true
        weekly: true
        dateext: true
        include: /etc/logrotate.d
        rotate: 3
        shred: true
      logrotate_d:
        - name: httpd
          files:
            - /var/log/httpd/*log
          options:
            compress: true
            dateext: true
            rotate: 3
...
在Appu的帮助下,我做到了:

  logrotate_d:
    - name: "{{ item.name }}"
      files:
        - "{{ item.logpath }}" / "{{ item.name }}"/*log
      options:
        compress: true
        dateext: true
        rotate: 3
      with_items:
        - { name: 'samba', logpath: '/log/var' }
        - { name: 'httpd', logpath: '/log/var' }

但是,它似乎不喜欢logpath和name之间的“/”。我认为它也缺少引用,如果我添加配额,它仍然认为相同。我只在两边添加了空格,否则,引号会变得疯狂,甚至不会停止,使整个剧本变成红色。

我还没有测试过它,但您可以通过在下面指定必填字段列表来使用“with_items”

  tasks:
  roles:
    - role: ansible-role-logrotate
      logrotate:
        compress: true
        create: true
        weekly: true
        dateext: true
        include: /etc/logrotate.d
        rotate: 3
        shred: true
      logrotate_d:
        - name: "{{ item.o }}"
          file: /"{{ item.l }}"/"{{ item.o }}"/*
          options:
            compress: true
            dateext: true
            rotate: 3
      with_items:
        - { o: 'samba', l: 'var/log' }
        - { o: 'httpd', l: '/tmp' }
...

好的,我做到了,谢谢大家的帮助。我无法让with_项目工作,所以我回到了列出所有东西的最初想法,不知何故,现在它开始工作,而早些时候我无法让它工作

- hosts: '{{ server }}'
  remote_user: ansip
  become: yes
  become_method: sudo

  vars_prompt:
- name: "server"
  prompt: "Enter server name or all"
  private: no

  tasks:
  roles:
    - role: ansible-role-logrotate
      logrotate:
        compress: true
        create: true
        weekly: true
        dateext: true
        include: /etc/logrotate.d
        rotate: 3
        shred: true
      logrotate_d:
        - name: samba
          files:
            - /var/log/samba/*
          options:
            compress: true
            dateext: true
            rotate: 3


          - name: httpd
           files:
             - /var/log/httpd/*
           options:
             compress: true
             dateext: true
             rotate: 3


         - name: syslog
           files:
             - /var/log/cron/*
             - /var/log/maillog/*
             - /var/log/messages/*
             - /var/log/secure/*
             - /var/log/spooler/'
           options:
             compress: true
             dateext: true
             rotate: 3

哇,这看起来很合法,我会马上试一试,谢谢:Dkudos。。如果答案满足您的要求,请接受。啊,遗憾的是,不,这个答案会立即产生错误。Playbook认为名字行缺少quoates。可能是我遗漏了双引号,请检查更新的引号。我建议您粘贴输出错误。另外,并非所有日志都在/var/log中,这也会使事情变得复杂。我需要分别定义所有5个,只是不知道如何在剧本中做到这一点。最好注意的是,我只使用ansible 2.7。我还安装了早期版本,它提到缺少一个插件,因此无法工作!