From 7c9347f8ed19be4c6cb17d903dc6d12d0678b4d3 Mon Sep 17 00:00:00 2001 From: Claude Becker <becker@phys.ethz.ch> Date: Fri, 7 Mar 2025 12:48:56 +0100 Subject: [PATCH] ansible: snippet cleanup --- .../ansible/ansible_task_snippets.markdown | 67 +++++++++++++------ 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/documentation/ansible/ansible_task_snippets.markdown b/documentation/ansible/ansible_task_snippets.markdown index c99625d0..750f7f22 100644 --- a/documentation/ansible/ansible_task_snippets.markdown +++ b/documentation/ansible/ansible_task_snippets.markdown @@ -1,12 +1,13 @@ Ansible Task Snippets ===================== - ### Package Management ```yaml - name: update apt cache - apt: update_cache_yes cache_valid_time=3600 + apt: + update_cache: yes + cache_valid_time: 3600 - name: install common tools package: pkg: @@ -14,7 +15,6 @@ Ansible Task Snippets - htop ``` - ### File Management Copy a file with given owner and permissions @@ -25,14 +25,17 @@ Copy a file with given owner and permissions src: authorized_keys dest: /root/.ssh/authorized_keys owner: root - mode: 0600 + group: root + mode: "0600" ``` Copy a host-specific file if it exists, the default otherwise ```yaml - name: copy proper config - copy: src={{ item }} dest=/etc/foo.conf + copy: + src: "{{ item }}" + dest: /etc/foo.conf with_first_found: - "foo.conf_{{ inventory_hostname}}" - foo.conf_default @@ -42,14 +45,18 @@ Copy and unpack a compressed file to a given directory ```yaml - name: copy and extract archive - unarchive: src=archive.tar.gz dest=/tmp + unarchive: + src: archive.tar.gz + dest: /tmp ``` Create a directory ```yaml - name: create ~root/.ssh directory - file: path=/root/.ssh state=directory + file: + path: /root/.ssh + state: directory ``` Create a symlink @@ -66,21 +73,23 @@ Delete a file ```yaml - name: disable apache2 default config - file: path=/etc/apache2/sites-enabled/default state=absent + file: + path: /etc/apache2/sites-enabled/default + state: absent ``` Use `stat` for instance to check the existence of a file ```yaml - name: check if somefile exists - stat: path=/path/to/somefile + stat: + path: /path/to/somefile register: somefile - name: run boostrap script (only if somefile does not exist) script: bootstrap.sh when: somefile.stat.exists == false ``` - ### Handlers Handlers can be notified to restart services or trigger other actions. @@ -89,7 +98,9 @@ Example `handlers/services.yml` to restart ssh service: ```yaml - name: restart ssh - service: name=ssh state=restarted + service: + name: ssh + state: restarted ``` Include handler in `site.yml`: @@ -107,7 +118,9 @@ Use `notify` to trigger a service restart in a playbook: ```yaml - name: sshd_config file - copy: src=sshd_config dest=/etc/ssh/sshd_config + copy: + src: sshd_config + dest: /etc/ssh/sshd_config notify: restart ssh ``` @@ -120,7 +133,6 @@ A meta module can be used to trigger the processing of all handlers at a specifi meta: flush_handlers ``` - ### Tags You may add tags to selected items or roles @@ -138,7 +150,6 @@ ansible-playbook site.yml --tags "webserver,dbserver" ansible-playbook site.yml --skip-tags "slowtask" ``` - ### Prompt for variable values to be entered ```yaml @@ -148,7 +159,6 @@ vars_prompt: private: yes # don't show what is being typed ``` - ### Wait for a condition to be met Use `wait_for` to not continue until a port accepts a connection @@ -171,6 +181,16 @@ or use `until` loops delay: 3 ``` +### Interact with HTTP API + +Use the `uri` module to interact with web services. + +```yaml +- name: Queue build of a project in Jenkins + uri: + url: https://example.com + method: GET +``` ### Register @@ -194,7 +214,6 @@ Use `when` for conditionals when: motd_contents.stdout.find('hi') != -1 ``` - ### When conditionals ```yaml @@ -214,17 +233,22 @@ block: - name: failing task command: /bin/false - name: never executed because the of the previous error - debug: msg="never" + debug: + msg: "never" rescue: - name: catch task to run if there was an error - debug: msg="catch task" + debug: + msg: "catch task" - name: info about the failed task - debug: var=ansible_failed_task + debug: + var: ansible_failed_task - name: info about failed result via automatic register - debug: var=ansible_failed_result + debug: + var: ansible_failed_result always: - name: task that is always run, independently of any errors - debug: msg="always" + debug: + msg: "always" ``` ### Become to run shell command with sudo @@ -290,7 +314,6 @@ This can also be set for a whole playbook to continue executing tasks after the when: ping_cmd.rc == 0 ``` - ### Yaml multi-line values Use `|` to preserve newlines: -- GitLab