DEV Community

Vidyasagar SC Machupalli
Vidyasagar SC Machupalli

Posted on

Ansible code snippets in YAML

I have been learning and writing Ansible for Cloud automation. Created this code repository to share my Ansible automation learnings as snippets.

As I explore and learn Ansible, I will be adding the simple and easy to use Ansible code snippets written in YAML.

🚧 The snippets include generic yaml code for you to understand the Ansible in-built modules and how to use them for your Ansible automation. More code snippets to come. So, ⭐ this repository 😊

Run locally

  1. Install and setup Ansible
  2. Command to run locally on your terminal or command prompt
    ansible-playbook list_files.yaml -i hosts.ini --flush-cache  
Enter fullscreen mode Exit fullscreen mode

For verbose output, use -vvvv flag in the ansible command

Expected output:

    PLAY [List files] *****************************************************************************************************************************************************************************

    TASK [List files in the current directory] ****************************************************************************************************************************************************
    changed: [localhost]

    TASK [debug] **********************************************************************************************************************************************************************************
    ok: [localhost] => {
        "output.stdout_lines": [
            "LICENSE",
            "README.md",
            "add_user_to_os.yaml",
            "ansible.cfg",
            "curl_requests.yaml",
            "hosts.ini",
            "list_files.yaml",
            "yum_install_dependencies.yaml"
        ]
    }

    PLAY RECAP ************************************************************************************************************************************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
Enter fullscreen mode Exit fullscreen mode
  1. cURL requests: add --ask-become-pass flag prompting for a root user password
    ansible-playbook curl_requests.yaml -i hosts.ini --flush-cache --ask-become-pass
Enter fullscreen mode Exit fullscreen mode

Expected output:

    BECOME password: 

    PLAY [CURL GET and POST requests] *************************************************************************************************************************************************************

    TASK [POST request with JSON and bearer token] ************************************************************************************************************************************************
    ok: [localhost]

    TASK [Print return information from the previous task] ****************************************************************************************************************************************
    ok: [localhost] => {
        "result.json": {
            "success": "true"
        }
    }

    TASK [GET request to download a file] *********************************************************************************************************************************************************
    ok: [localhost]

    TASK [debug] **********************************************************************************************************************************************************************************
    ok: [localhost] => {
        "result": {
            "changed": false,
            "dest": "./filename.txt",
            "elapsed": 0,
            "failed": false,
            "gid": 0,
            "group": "wheel",
            "mode": "0644",
            "msg": "HTTP Error 304: Not Modified",
            "owner": "root",
            "size": 10612,
            "state": "file",
            "status_code": 304,
            "uid": 0,
            "url": "https://reqbin.com/echo"
        }
    }

    PLAY RECAP ************************************************************************************************************************************************************************************
    localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Enter fullscreen mode Exit fullscreen mode

Simple and easy-to-use Ansible code snippets in YAML https://github.com/VidyasagarMSC/ansible-yaml-snippets @ansible @RedHat

Tweet

Top comments (0)