簡體   English   中英

如何用循環重寫這個 Ansible 劇本?

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

我制作了一本劇本,以便在我的 Wordpress 服務器上提供所有更新。

它有效,但我想用循環重寫它以尊重“不要重復自己”

這不是一本劇本。 只是角色中的一些任務> 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

當您在 playbook 執行期間查看標准輸出時會有點吵,但工作已經完成:

編輯: 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