简体   繁体   English

如何用循环重写这个 Ansible 剧本?

[英]How to rewrite this Ansible playbook with a loop?

I make a playbook in order to make alls updates available on my Wordpress serveurs.我制作了一本剧本,以便在我的 Wordpress 服务器上提供所有更新。

It works but, I want to rewrite it with a loop to respect "Don't Repeat Yourself"它有效,但我想用循环重写它以尊重“不要重复自己”

It is not a playbook.这不是一本剧本。 Just some tasks in roles > Intranet > tasks > main.yaml只是角色中的一些任务> Intranet > 任务> main.yaml

---
# Main tasks for wordpress serveurs

# Updates
- name: Update WP command line tool
  command: wp cli update
  register: wpcli_result

- name: Update Wordpress Core
  command: wp core update --allow-root --path=/var/www/html
  register: update_core

- name: Update Wordpress Core Data Base
  command: wp core update-db --allow-root --path=/var/www/html
  register: update_core_db

- name: Update Plugins
  command: wp plugin update --all --allow-root --path=/var/www/html
  register: update_plugins

- name: Update Themes
  command: wp theme update --all --allow-root --path=/var/www/html
  register: update_themes
...

# Debug
- name: Debug wp cli update
  ansible.builtin.debug:
    var: wpcli_result.stdout

- name: Debug wp Core update
  ansible.builtin.debug:
    var: update_core.stdout

- name: Debug wp Core update data base
  ansible.builtin.debug:
    var: update_core_db.stdout

- name: Debug wp plugins update
  ansible.builtin.debug:
    var: update_plugins.stdout

- name: Debug wp Themes update
  ansible.builtin.debug:
    var: update_themes.stdout
...

# Call to Zabbix tasks
- include: zabbix.yml

It is a little noisy when you look at the stdout during playbook execution but the job is done:当您在 playbook 执行期间查看标准输出时会有点吵,但工作已经完成:

Edit: items.name are not necessary but I let them for better reading.编辑: items.name 不是必需的,但我让他们更好地阅读。

---
# Main tasks for wordpress serveurs
- name: Loop through Wordpress Updates items
  command: "{{ item.command }}"
  register: var_cmd
  with_items:
    - { name: Update WP command line tool,
        command: wp cli update}
    - { name: Update Wordpress Core,
        command: "wp core update {{wp_allow}} {{wp_path}}"}
    - { name: Update Wordpress Core Data Base,
        command: "wp core update-db {{wp_allow}} {{wp_path}}"}
    - { name: Update Plugins,
        command: "wp plugin update --all {{wp_allow}} {{wp_path}}"}
    - { name: Update Themes,
        command: "wp theme update --all {{wp_allow}} {{wp_path}}"}
    - { name: Update Core Translations,
        command: "wp language core update {{wp_allow}} {{wp_path}}"}
    - { name: Update Plugins Translations,
        command: "wp language plugin update --all {{wp_allow}} {{wp_path}}"}
    - { name: Update Themes Translations,
        command: "wp language theme update --all {{wp_allow}} {{wp_path}}"}

- name: Debug Wordpress Updates
  debug:
    msg: "{{ item.stdout_lines }}"
    verbosity: 0
  with_items: "{{ var_cmd['results'] }}"

# Call to Zabbix tasks
- include: zabbix.yml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM