Git Product home page Git Product logo

ansible-snippets's Introduction

ansible-snippets

---
- name: debug module demo
  hosts: all
  vars:
    fruit: "Apple"
    
  tasks:
    - name: Debug Message Prints Hello World 
      ansible.builtin.debug:

    - name: Customized Debug Message
      ansible.builtin.debug:
        msg: "Example Text Message"
    
    - name: Display fruit Variable
      ansible.builtin.debug:
        var: fruit
    
    - name: Using jinja Template to display fruit variable
      ansible.builtin.debug:
        msg: "The Fruit is {{ fruit }}" 

    - name: Task with Verbosity 2
      ansible.builtin.debug:
        verbosity: 2

Verbosity Skipped

image

Verbosity not skipped

image
---
- name: pause module demo
  hosts: all
  vars:
    wait_seconds: 10
  tasks:
    - name: pause for {{ wait_seconds | int }} second(s)
      ansible.builtin.pause:
        seconds: "{{ wait_seconds | int }}"

    - name: message
      ansible.builtin.debug:
        msg: "The end"
image
---
- name: extra variable demo
  hosts: all
  gather_facts: no
  vars:
    fruit: "banana"
  tasks:
    - name: print message
      ansible.builtin.debug:
        msg: "fruit is {{ fruit }}"
image

The Basics: | and > Operators

In Ansible, breaking a string over multiple lines is accomplished using two main operators:

  • | (Literal Block Scalar): This operator instructs Ansible to treat the string as a literal block scalar, preserving the newlines within the string.
  • > (Folded Block Scalar): This operator tells Ansible to treat the string as a folded block scalar, collapsing all newlines into a single space.

https://ansiblepilot.medium.com/how-to-break-a-string-over-multiple-lines-with-ansible-and-yaml-73cd5909fae8#:~:text=The%20Basics%3A%20%7C%20and%20%3E%20Operators,the%20newlines%20within%20the%20string.

- name: debug module demo
  hosts: all
  gather_facts: no
  vars:
    variable1: |
      exactly as you see
      will appear these three
      lines of poetry
    variable2: >
      this is really a
      single line of text
      despite appearances
    variable3: |-
      exactly as you see
      will appear these three
      lines of poetry
    variable4: >-
      this is really a
      single line of text
      despite appearances
  tasks:
    - name: print variable1
      ansible.builtin.debug:
        var: variable1
    - name: print variable2
      ansible.builtin.debug:
        var: variable2
    - name: print variable3 with no newline at the end- Literal Block Scalar
      ansible.builtin.debug:
        var: variable3
    - name: print variable3 with no newline between Strings- Literal Block Scalar
      ansible.builtin.debug:
        msg: "{{ variable3.split('\n') }}"
    - name: print variable4 with no new line - Folded Block Scalar
      ansible.builtin.debug:
        var: variable4
image

Ansible terminology — ansible_hostname vs inventory_hostname vs ansible_fqdn

https://ansiblepilot.medium.com/ansible-terminology-ansible-hostname-vs-inventory-hostname-vs-ansible-fqdn-ae44d2acc484 https://www.middlewareinventory.com/blog/ansible-inventory_hostname-ansible_hostname-variables/

Set remote environment per task or play - Ansible environment statement

https://www.ansiblepilot.com/articles/set-remote-environment-per-task-or-play-ansible-environment-statement/

---
- name: remote environment demo
  hosts: all
  gather_facts: false
  environment:
    EXAMPLE: test1

  tasks:
    - name: diplay EXAMPLE Playbook level
      ansible.builtin.command: "echo $EXAMPLE"

    - name: diplay EXAMPLE Task Level
      ansible.builtin.command: "echo $EXAMPLE"
      environment:
        EXAMPLE: test2
image

Execute command on the Ansible host — Ansible localhost

https://ansiblepilot.medium.com/execute-command-on-the-ansible-host-ansible-localhost-71fa92459fe1

https://www.shellhacks.com/ansible-run-shell-command-on-remote-host/

https://medium.com/margarytachepiga/ansible-beggining-b64d578ed7d5

---
- name: localhost demo
  hosts: localhost
  vars:
    ansible_connection: local
    ansible_python_interpreter: "{{ ansible_playbook_python }}"
  tasks:
    - name: print hostname
      ansible.builtin.debug:
        msg: "{{ inventory_hostname }} -  {{ ansible_python_interpreter }}"
image

Three options to Safely Limit Ansible Playbooks Execution to a Single Machine

https://ansiblepilot.medium.com/three-options-to-safely-limit-ansible-playbooks-execution-to-a-single-machine-f4abc107246f

Ansible modules - command vs shell

https://www.ansiblepilot.com/articles/ansible-modules-command-vs-shell/ to be watched again

Write a Variable to File — Ansible module copy vs template

https://ansiblepilot.medium.com/write-a-variable-to-file-ansible-module-copy-vs-template-2ef51c67ebdc

How to Run Only One Task in Ansible Playbook? — Ansible tags statement

https://ansiblepilot.medium.com/how-to-run-only-one-task-in-ansible-playbook-ansible-tags-statement-1c5d007370bb

Filter A List By Its Attributes — Ansible selectattr filter

https://www.educative.io/answers/how-to-filter-a-list-by-its-attributes-in-ansible

https://ansiblepilot.medium.com/filter-a-list-by-its-attributes-ansible-selectattr-filter-66881fe8a825

Using Date, Time and Timestamp in Ansible Playbook - Ansible Tip and Tricks

https://www.ansiblepilot.com/articles/using-date-time-and-timestamp-in-ansible-playbook-ansible-tip-and-tricks/

Using Date, Time and Timestamp without Facts in Ansible Playbook - Ansible date and lookup plugin

https://www.ansiblepilot.com/articles/using-date-time-and-timestamp-without-facts-in-ansible-playbook-ansible-date-and-lookup-plugin/

https://ansiblepilot.medium.com/using-date-time-and-timestamp-in-ansible-playbook-ansible-tip-and-tricks-5ee6cb3d6310

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.