Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

wordpress - How to rewrite this Ansible playbook with a loop?

I make a playbook in order to make alls updates available on my Wordpress serveurs.

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

---
# 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
question from:https://stackoverflow.com/questions/65842706/how-to-rewrite-this-ansible-playbook-with-a-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is a little noisy when you look at the stdout during playbook execution but the job is done :

Edit: items.name are not necessary but I let them for better reading.

---
# 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...