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

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

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

Related

GitHub action stalls on setup-r-dependencies#v2

This is cross posted on the RStudio Community site. I am using r-lib/setup-r-dependencies#v2 for a bookdown project on GitHub action. However, the job is stalling when pak tries to create a lock file. Here is an example run , and here is the workflow.
As you can see, eventually the job just times out.
The issue seems similar to this post. The solution there was to use the most current RSPM, but I believe that is already what I am using (I don't specify anything different in the workflow).
Any ideas on what might be causing this?
Edit: Here is the full yaml code for the workflow (linked above)
on:
push:
branches: [main, master]
workflow_dispatch:
name: bookdown
jobs:
bookdown:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout#v2
- uses: r-lib/actions/setup-pandoc#v2
- uses: r-lib/actions/setup-r#v2
with:
use-public-rspm: true
- uses: r-lib/actions/setup-r-dependencies#v2
with:
cache-version: 1
- name: Cache bookdown results
uses: actions/cache#v2
with:
path: _bookdown_files
key: bookdown-${{ hashFiles('**/*Rmd') }}
restore-keys: bookdown-
- name: Build site
run: bookdown::render_book("index.Rmd", quiet = TRUE)
shell: Rscript {0}
- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action#4.1.4
with:
branch: gh-pages
folder: _book

Deploy ASP.NET Core on Server with Github Action

I'm using github actions to deploy my projects on my windows server using FTP
name: .NET
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout#v3
- name: Setup .NET
uses: actions/setup-dotnet#v2
with:
dotnet-version: 6.0.x
- name: Init Utilities
run: git submodule update --init
- name: Get Utilities
run: git submodule update --remote -f
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Publish
run: dotnet publish
- name: Deploy
uses: SamKirkland/FTP-Deploy-Action#4.0.0
with:
local-dir: LOCAL_DIR
server: SERVER
username: USERNAME
password: PASSWORD
dangerous-clean-slate: true
the problem is that I have to manually stop the server so the files could be uploaded.
but I don't want to stop the server manually. is there any way so I can force update the project without stoping the server or stop the server automatically?
You can add app_offline.htm to make your site offline. When publishing is complete, delete the app_offline.htm file.
Official doc: Taking Web Applications Offline with Web Deploy
You can refer the link to add app_offline.htm file when you delpoy the app.

Symfony5 workflow with githubaction issue on database and migrations

Currently, I'm trying to set up a workflow with GitHub action.
I took example from https://github.com/shivammathur/setup-php/blob/master/examples/symfony-mysql.yml
My ci.yml is:
name: phpunit
on: [push]
jobs:
tests :
name: Running functional and unit test
runs-on: ubuntu-20.04
services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: false
MYSQL_ROOT_PASSWORD: symfony
MYSQL_DATABASE: symfony
ports:
- 3306/tcp
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
strategy:
fail-fast: true
matrix:
php-versions: ['7.4-apache']
steps:
# —— Setup Github actions 🐙 —————————————————————————————————————————————
# https://github.com/actions/checkout (official)
- name: Checkout
uses: actions/checkout#v2
# https://github.com/shivammathur/setup-php (community)
- name: Setup PHP, extensions and composer with shivammathur/setup-php
uses: shivammathur/setup-php#v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, xml, ctype, iconv, intl, pdo, pdo_mysql, dom, filter, gd, iconv, json, mbstring, mysqli
env:
update: true
- name: Check PHP Version
run: php -v
# —— Composer 🧙‍️ —————————————————————————————————————————————————————————
- name: Validate composer.json and composer.lock
run: composer validate
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache#v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Composer dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
- run: composer require symfony/runtime
- name: Run Migration && Load Fixtures
run: |
composer require --dev symfony/orm-pack
php bin/console doctrine:database:drop --if-exists --force --env=test
php bin/console doctrine:database:create --if-not-exists --env=test
php bin/console doctrine:schema:update --env=test --force || echo "No migrations found or schema update failed"
php bin/console doctrine:migrations:migrate --env=test || echo "No migrations found or migration failed"
php bin/console doctrine:fixtures:load --no-interaction
env:
DATABASE_URL: mysql://root:symfony#127.0.0.1:${{ job.services.mysql.ports['3306'] }}/symfony
## —— NPM 🐱 ————————————————————————————————————————————————————————————
- name: npm install
uses: actions/setup-node#v2
with:
node-version: '14'
#registry-url: npm.fontawesome.com
- run: npm install
#env:
#NODE_AUTH_TOKEN: ${{ secrets.FONTAWESOME_NPM_AUTH_TOKEN }}
- run: npm run build
- run: php bin/phpunit
I'm getting issue in the step of Run migrations & load Fixtures':
ci error:
Ci error:
Error: Migration DoctrineMigrations\Version20220222101244 failed during Execution. Error: "An exception occurred while executing a query: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'user' already exists"
In ExceptionConverter.php line 47:
An exception occurred while executing a query: SQLSTATE[42S01]: Base table
or view already exists: 1050 Table 'user' already exists
I tried to remove schema update, this also lead into another error.
I also removed dropping database, then another error.
You create your schema twice:
First you do a doctrine:schema:update, that creates all tables like it is defined in your mapping.
Then you want to do the doctrine:migrations:migrate. I assume, that you have the CREATE TABLE definitions there, too. Thats why it complains.
If you remove this following line
php bin/console doctrine:schema:update --env=test --force || echo "No migrations found or schema update failed"
and have all setup in your migrations, it should work.

Run Sendria (MailTrap) in a Github workflow to test mail delivery in a WordPress plugin

I like to test the mail functionally from a WordPress plugin via a Github Workflow and Sendria (MailTrap)
This is my working workflow:
# Based on https://github.com/wp-cli/scaffold-command/blob/f9bad3dd7224d5684d950d31c486df70905e386f/templates/plugin-github.mustache
name: WordPress Test
on: [push]
env:
WP_TESTS_DIR: /github/home/wp-tests/wordpress-tests-lib
WP_CORE_DIR: /github/home/wp-tests/wordpress
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: [7.2]
wordpress-version: [latest]
container:
image: junaidbhura/wp-tests:php-${{ matrix.php-version }}
services:
mysql:
image: mysql:5.7.27
env:
MYSQL_ROOT_PASSWORD: root
steps:
- name: Checkout repository
uses: actions/checkout#v1
- name: Install Composer dependencies
run: |
composer install --no-dev
composer global require "phpunit/phpunit=6.1.0"
composer global require "yoast/phpunit-polyfills=1.0.3"
- name: Install WordPress test suite
run: bash bin/install-wp-tests.sh wordpress_test root root mysql ${{ matrix.wordpress-version }}
- name: Tests
run: $HOME/.composer/vendor/bin/phpunit --config=phpunit-integration.xml
This workflow
checkout my plugin
install composer dependencies
install the WordPress test suite
run the Tests (which should contain mail tests)
Now I like to add Sendria to this flow by adding this action right above the first step:
- name: Start Sendira
uses: upgundecha/start-sendria-github-action#v1.0.0
with:
smtp-port: 1025
http-port: 1080
To test that Sendria is running I check the endpoint via curl with an additional step:
- name: Test
run: |
curl -LI http://127.0.0.1:1080
And this "Test" fails:
I tried the workflow locally with act where this step worked but have no idea how to get this working on Github.
I've also created a test repo on Github here.

Error on ansible playbook installing WordPress with wp-cli

I'm using an ansible playbook (ansible ver. 2.9) to install WordPress using wp-cli tool.
Here's the playbook:
- name: Create WordPress database
mysql_db: name="{{ db_name }}"
state=present
login_user=root
login_password="{{ mysql_root_password }}"
- name: Create WordPress DB user and grant permissions to WordPress DB
mysql_user: name="{{ db_user }}"
password="{{ db_pwd }}"
priv="{{ db_name }}.*:ALL"
state=present
login_user="root"
login_password="{{ mysql_root_password }}"
- name: Is WordPress downloaded?
stat: path="/var/www/{{ domain_name }}/html/index.php"
register: wp_dir
- name: Download WordPress
command: wp core download
args:
chdir: "/var/www/{{ domain_name }}/html/"
remote_user: "{{ web_user }}"
when: wp_dir.stat.isdir is not defined
- name: Configure WordPress
command: wp core config
--path="/var/www/{{ domain_name }}/html"
--dbname="{{ db_name }}"
--dbuser="{{ db_user }}"
--dbpass="{{ db_pwd }}"
--dbprefix="{{ db_prefix }}"
remote_user: "{{ web_user }}"
when: wp_dir.stat.isdir is not defined
- name: Is WordPress installed?
command: wp core is-installed
args:
chdir: "/var/www/{{ domain_name }}/html/"
register: wordpress_is_installed
ignore_errors: True
remote_user: "{{ web_user }}"
- name: Install WordPress tables
command: wp core install
--url="{{ wp_home_url }}"
--title="{{ wp_site_title }}"
--admin_user="{{ wp_admin_user }}"
--admin_password="{{ wp_admin_pwd }}"
--admin_email="{{ wp_admin_email }}"
args:
chdir: "/var/www/{{ domain_name }}/html/"
when: wordpress_is_installed|failed
remote_user: "{{ web_user }}"
At the "Download WordPress" task, a fatal error shows up:
"Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress installation exists under."
I run the playbook as a sudo user ("ansible_user" in hosts file). And I have setup an additional user to manage WordPress setup (remote_user: "{{ web_user }}").
Any help would be much appreciated!
In the tasks you need to use become and become_user instead remote_user as below
- name: Download WordPress
command: wp core download
args:
chdir: "/var/www/{{ domain_name }}/html/"
become: yes
become_user: "{{ web_user }}"
when: wp_dir.stat.isdir is not defined
Now a different error is showing up when running the same code:
FAILED! => {"msg": "Failed to set permissions on the temporary files Ansible needs to create when becoming an unprivileged user (rc: 1, err: chown: changing ownership of '/var/tmp/ansible-tmp-1613648876.307028-8235-221563540981220/': Operation not permitted\nchown: changing ownership of '/var/tmp/ansible-tmp-1613648876.307028-8235-221563540981220/AnsiballZ_command.py': Operation not permitted\n}). For information on working around this, see https://docs.ansible.com/ansible/become.html#becoming-an-unprivileged-user"}
I updated Ansible to the last version available (2.10).
The only solution I've found so far is adding allow_world_readable_tmpfiles = Yes to ansible.cfg file...
Any ideas?
Thanks

Resources