Skip to content

Instantly share code, notes, and snippets.

@bmwant
Last active July 9, 2021 09:53
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bmwant/21398ba124c6fd06d22d26e83a3f15ca to your computer and use it in GitHub Desktop.
Save bmwant/21398ba124c6fd06d22d26e83a3f15ca to your computer and use it in GitHub Desktop.
ansible quick tips for some modules
# Check existing
- name: Ansible check file exists.
stat:
path: /etc/filename
register: file_status
- debug:
msg: "File exists..."
when: file_status.stat.exists
- debug:
msg: "File not found"
when: not file_status.stat.exists
- name: check phantomjs executable is present
shell: "which phantomjs"
register: result
ignore_errors: true
- fail:
msg: "PhantomJS is not found! You need to install it first."
when: result.rc != 0
# Set owner for the directory recursively
- file:
path: "/path/to/directory"
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
recurse: true
become: true
# Trigger handlers always
- name: restart app with supervisor
command: "/bin/true"
notify:
- restart app
- debug: msg="trigger nginx-restart"
notify: nginx-restart
changed_when: true
# Check command output to match
- name: Check which python version is installed
shell: "python3 --version"
register: python_output
ignore_errors: true
- debug:
var: python_output
when: python_output.stdout != "Python {{ python_version }}"
# Define variable
- set_fact:
one_fact: something
other_fact: "{{ local_var * 2 }}"
another_fact: "{{ some_registered_var.results | map(attribute='ansible_facts.some_fact') | list }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment