Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...