Ansible v2.3用户:修改es_config后重跑剧本Elasticsearch角色被跳过问题
Hey there! As a new Ansible v2.3 user, let’s walk through how to fix this issue where your elasticsearch role gets skipped when you update es_config parameters—plus we’ll clarify idempotency along the way.
First, Let’s Demystify Idempotency
Idempotency is Ansible’s superpower: it lets you run a playbook as many times as you want without breaking things. If your system is already in the state you defined, Ansible does nothing (marks tasks as "ok" or skipped). That’s why your role is being skipped right now—Ansible thinks all the role’s work was already completed on the first run.
Fixes to Update elasticsearch.yml and Restart Elasticsearch
1. Verify the Config File Task in the Elasticsearch Role
The key here is making sure the task that manages elasticsearch.yml detects changes to your es_config variables. Head to roles/elasticsearch/tasks/main.yml and look for a task using the template module (this is how most roles generate config files from variables). It should look something like this:
- name: Generate elasticsearch.yml config file template: src: elasticsearch.yml.j2 dest: /etc/elasticsearch/elasticsearch.yml owner: elasticsearch group: elasticsearch mode: '0644' notify: Restart Elasticsearch
- The
templatemodule automatically compares the rendered content (using your updatedes_configvariables) to the existing file on the server. If they differ, the task will mark itself as "changed" and trigger theRestart Elasticsearchhandler. - If this task has a
whenclause that’s preventing it from running (likewhen: elasticsearch_installed | default(false) == false), you’ll need to remove or adjust that condition so it runs every time the config needs updating.
2. Check for Role-Level Skip Conditions
Take a look at how you’re including the role in your elk.yml playbook. If you have something like this:
- hosts: elk_servers tasks: - include_roles: name: elasticsearch when: elasticsearch_not_installed | default(true)
That when condition will skip the entire role after the first install. Remove the when clause so the role runs every time—Ansible will still skip individual tasks that don’t need changes, but run the ones that do (like the config update).
3. Force Execution (Quick Temporary Fix)
If you need to push changes right away, you can target just the config-related tasks using tags (if the role uses them):
ansible-playbook --ask-become-pass -vv elk.yml --tags=elasticsearch-config
If the role doesn’t have tags, you can add them to the template task in the role, or use --force-handlers to ensure the restart happens even if Ansible thinks no changes were made (though this is less ideal than fixing the root cause).
4. Check Handlers for Restart
Make sure the Elasticsearch role has a handler defined to restart the service. Look in roles/elasticsearch/handlers/main.yml:
- name: Restart Elasticsearch service: name: elasticsearch state: restarted enabled: true
Handlers only run when a task marks itself as "changed", so if your config task works correctly, this will kick off automatically after the config file is updated.
Final Notes
Remember, Ansible’s skip behavior is intentional (thanks to idempotency), but we just need to make sure the right tasks trigger when your es_config variables change. By verifying the template task, removing unnecessary skip conditions, and checking handlers, you’ll get the config updates and service restart you want on subsequent playbook runs.
内容的提问来源于stack exchange,提问作者Tim

