Ansible code snippets in YAML
Tags: ansible, automation, shortposts, beginners
Published: 2023-06-10 21:37:26

I have been learning and writing Ansible for Cloud automation. Created this [code repository](https://github.com/VidyasagarMSC/ansible-yaml-snippets) 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.

> :construction: 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, :star: this repository :blush:

## Run locally 

1. Install and setup [Ansible](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html)
2. Command to run locally on your terminal or command prompt
   ```shell
    ansible-playbook list_files.yaml -i hosts.ini --flush-cache  
   ```
   > For verbose output, use `-vvvv` flag in the ansible command
   
   Expected output:
   ```shell
    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   
   ```
3. cURL requests: add `--ask-become-pass` flag prompting for a root user password
   ```shell
    ansible-playbook curl_requests.yaml -i hosts.ini --flush-cache --ask-become-pass
   ```
   Expected output:
   ```shell
    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   

   ```

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

[Tweet](https://twitter.com/VidyasagarMSC/status/1667645736857501698?s=20)