Skip to content

Examples of Ansible use cases to share specific examples I have ran across

License

Notifications You must be signed in to change notification settings

MichaelChristopherson/ansible-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 

Repository files navigation

ansible-examples

Examples of Ansible. Notes taken while studying or specific use cases I have ran across in my uses of ansible.

Debugging

This example a task is done with the output of the task saved in the Register line. The variable output is created. You can then write that output out or take action based upon what the variable returns.

- Name: 
  File: 
    Path: /tmp/newfile 
    State: touch 
  Register: output 

- Debug: msg="Register output is {{ output }}" 

- name:
  lineinfile:
    path: /tmp/newfile
    line: "{{ output.uid }}"

Conditionals

  • Handlers
  • With_items
  • When
  • With_files

Handlers

Handlers are a way to have a task execute on a given host only if another task actually had to take action. If the task returns without any action taken then the handler does not get notified to execute - however if the task does make change then the handler gets notified and runs at the end of playbook only 1 time no matter how many times the handler was called.

  • Task that notifies Handler
- name: change config
  replace:
    path: /etc/somefile
    regexp: '^someregex.*'
    replace 'changetotext'
    backup: yes
  notify: "restart web"
  • Handler
- name: restart apache
  service:
    name: httpd
    state: restarted
  listen: "restart web"
  • Handlers can be called before the task section of a playbook if not set in the handlers folder of a created role.
- hosts: somehosts
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted
      listen: "restart web"
     
   tasks:
     - name: change config
      replace:
        path: /etc/somefile
        regexp: '^someregex.*'
        replace 'changetotext'
        backup: yes
      notify: "restart web" 

With_items

When

With_files

Error Handling

  • Ignoring acceptable errors
  • Blocks
  • Defining failure conditions

Ignoring acceptable errors

This is to ignore errors and continue - while not especially advised I would attempt to find a better way to run the particular task.

- name: do something
  service:
    name: httpd
    state: started
  ignore_errors: yes

Blocks

Blocks are basically try/catch in normal programming languages. The rescue portion only runs in the block fails. Always is not required but can be used as needed.

- name: get file
  block:
    - get_url:
      url: "http://url.com/index.html"
      dest: "/tmp/outfile"
  rescue:
    - debug msg="The file does not Exist - letting playbook continue!"
  always:
    - debug: msg="Play done!"

Defining failure conditions

Variables

Special Variables can be found here: https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html

You can also use variables to Determine another variable to use. In this example the variable 'appenvironment' is used in conjunction with the string '_some_url' to have the variable 'some_url' point towards different URL's based upon the environment. This is useful so you can set an app environment once and have that flow to all other variables.

some_url: "{{ vars[appenvironment|lower + '_some_url'] }}"
dev_some_url: "dev-url.mydomain.net"
prod_some_url: "url.mydomain.net"
qa_some_url: "url.mydomain.net"

Templates

Jinja2

Good article explaining jinja2. Long Read: http://jinja.palletsprojects.com/en/2.10.x/templates/

Examples of variable usage: You can use the magic variables and reference items within:

{{ hostvars['node1']['ansible_distribution'] }}
{{ groups['webservers'] }}

Filters

You can use built in filters as well in Jinja2. Good article on built in Jinja2 filters: https://jinja.palletsprojects.com/en/2.10.x/templates/#builtin-filters

A good example is creating a list of the servers in the 'webservers' group:

{{ groups['webservers']join(' ') }}

About

Examples of Ansible use cases to share specific examples I have ran across

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published