Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

New level of automation with Ansible

June 7, 2017
muayyad alsadi
Related topics:
CI/CDDevOpsLinux
Related products:
Streams for Apache Kafka

    Ansible is a simple agent-less automation tool that has changed the world for the better. It has many use cases and wide adoption (used by many upstream projects like Kubernetes and there are thousands of rules submitted to Ansible Galaxy). In this article, we are going to demonstrate Ansible. The intention of this article is not to teach you the basics of Ansible, but to motivate you to learn it.

    Why is Bash scripting not automation?

    Shell has been the comfort zone of every single sysadmin I know. They think, dream, and curse in BASH. While BASH would continue to be one of the basic requirements of every sysadmin, one needs to leverage his/her skills into real automation beyond shell scripting. I believe Ansible should become part of the comfort zone of every sysadmin and developer.

    Imperative vs. declarative

    Besides being an interactive shell where we type commands on the command line, BASH serves as an interpreted programming language. And as any of such languages, it's "imperative" which means your code is telling the interpreter ordered steps to execute one by one, do this then do that, then check this and if so do this and that, then go over to elsewhere and do this and that. On the other hand, declarative domain-specific languages (DSL) do not specify ordered steps but they describe the desired state. An example of this is the nginx configuration below (which says pass the request to port 8000 and set those two headers and of course the headers would be set before the proxy_pass despite being written after it) that's the reason imperative logic like "if" does not give the expected results, they call it "if is evil".

    location / {
     proxy_pass http://localhost:8000;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
    }

    Because Ansible is declarative, it's very simple and to the point "Get me this thing done" and "Put this in that desired state".

    From anywhere to the desired state

    Given a server in state A, we write a script to put it in state B. If you run the script twice usually it won't work because in the second run it would be starting from state B which is not what it was written for. For example creating a user would fail because the user is already there. Similarly creating a run-time directory. If you have many assumptions for state A that your automation script should expect and handle, the more generic the assumptions the more logic you have to put to handle all kinds of possibilities.

    On the other hand, Ansible is all about the desired state, each task defined in ways regardless of context or initial state, for example, the following YAML task means "I want a user named Omar to be present, and the user named Yousef to be absent":

    - user: name=omar state=present
    - user: name=yousef state=absent

    You can refer to Ansible user module for more details, another example is from Ansible file module:

    - file: path=/home/omar/tmp state=directory owner=omar group=admin mode=0775
    

    No matter from which state you start, no matter what are the steps leading to that desired state you get what you want. Ansible reports if it was able to successfully put it in the desired state or not and it also reports if a change was made. For example, the user Omar might already exist, it would report that it was a success without change. Doing the second example in bash means that you have to check if the directory exists, check who is the owner user and owner group and what is the current permission of the file and report if any change is necessary and do it.

    Dry run and report changes

    Being able to go from the desired state of itself might sound ridiculous but it's used as "dry run" to report changes or deviation from the desired state. It can also be used periodically to enforce some policy. Below is an example of how to run an Ad-Hoc ansible command which creates a directory /tmp/mydir with given properties.

    $ ansible -m file -a 'path=/tmp/mydir state=directory owner=root group=wheel mode=0775' localhost

    If you run it, it would fail, because I don't have permission to do so, to fix this let's add  "--become --ask-become-pass" or "-b -K" for short which would successfully create that directory.

    $ ansible -b -K -m file -a 'path=/tmp/mydir state=directory owner=root group=wheel mode=0775' localhost
    SUDO password: 
     [WARNING]: provided hosts list is empty, only localhost is available
    
    localhost | SUCCESS => {
     "changed": true, 
     "gid": 10, 
     "group": "wheel", 
     "mode": "0775", 
     "owner": "root", 
     "path": "/tmp/mydir", 
     "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
     "size": 40, 
     "state": "directory", 
     "uid": 0
    }

    But the good part is that you can add "--check" (or "-C" for short) to just report if change is needed.

    $ ansible -C -m file -a 'path=/tmp/mydir state=directory owner=root group=wheel mode=0775' localhost
     [WARNING]: provided hosts list is empty, only localhost is available
    
    localhost | SUCCESS => {
     "changed": false, 
     "gid": 10, 
     "group": "wheel", 
     "mode": "0775", 
     "owner": "root", 
     "path": "/tmp/mydir", 
     "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
     "size": 40, 
     "state": "directory", 
     "uid": 0
    }

    Working on multiple servers at once

    If you were asked to report git branch that all servers are standing on. One way is to login into every server and type "git rev-parse --abbrev-ref HEAD" but if you have tens on hundreds of servers that won't be a funny task. In the past, I was in love with python fabric, but writing a python code for your day to day tasks is also not productive. For this task, a simple ansible command using shell module would do the job.

    $ ansible -i 10.0.0.2,10.0.0.3,10.0.0.4, -b -u myuser --become-user=proj -m shell -a "cd ~proj/repo/ && git rev-parse --abbrev-ref HEAD" all

    As you can see we specified the list of IPs we are going to work on using "-i" short for "Inventory" which is either a file name or a comma separated list of IPs ending in a comma. Instead of remembering the IPs we can place them in a file.

    10.0.0.2
    10.0.0.3
    [web]
    10.0.0.3
    10.0.0.4
    [db]
    10.0.0.5

    You might call those file hosts and pass it to "-i" and you can also pass "all" or "web" or "db" to run the command on.

    We have specified the remote user to be used to connect to those servers with -u and the user to sudo before running the command using "--become-user".

    Readable Playbooks

    You think BASH scripts are readable, think again as I mean those written by others. If you download Apache Solr or Apache Cassandra you see a bash script that just exports some environment variables then execute "java $OPTIONS -jar file.jar", despite being a simple task the script is not readable by all means. On the other hand, if you look at the Ansible playbooks that deploy a complex highly available cluster, you would see that it's very simple and readable. Let's save the file below as book1.yml.

    ---
    - hosts: all
      become: yes
      tasks:
      - user: name=omar state=present
      - user: name=yousef state=absent
      - file: path=/tmp/mydir state=directory owner=root group=wheel mode=0775

    To run the playbook above you type "ansible-playbook -i ./hosts book1.yml".

    When you become comfortable with Ad-Hoc commands, you should start writing playbooks instead of shell scripts, it's a simple YAML here is a fully functional playbook that installs Apache web server (called "httpd" on RedHat family and "apache2" on Debian family) using suitable package manager and make sure it's enabled and started and it places a simple index.html page.

    ---
    - hosts: all
      become: yes
      tasks:
      - set_fact: httpd_pkg=httpd
        when: ansible_os_family == "RedHat"
      - set_fact: httpd_pkg=apache2
        when: ansible_os_family == "Debian"
      - copy: dest=/var/www/html/index.html content="Hello, world!"
      - package: name={{httpd_pkg}} state=present
      - service: name={{httpd_pkg}} state=started enabled=yes

    Save the file above as book2.yml and run it on your local machine using local connection type using,

    $ ansible-playbook -K -c local -i localhost, book2.yml

    as before "-K" is to ask for sudo password and "-b" is not needed because it's part of the file.

    We have used the "ansible_os_family" provided by ansible to conditionally set another fact using when.

    Orchestration

    You can do parallel execution, serial execution, delegated execution, etc.

    For example, you can go to the load-balancer remove a server from it, do some change to the server, then put it back to the load-balancer. You can go all Django application servers, fetch the latest code from git on all servers then only on one of them run "python manage.py, migrate" then reload the wsgi service on all servers.

    Making Ansible your comfort zone

    Make sure you take a glance at Ansible's list of modules by category or the grand long list of all modules.

    Use Ansible Ad-Hoc commands just as you use interactive bash shell.

    Learn facts reported by Ansible (run "ansible -m setup localhost | less").

    Write Ansible playbooks instead of shell scripts.

    Check for a module before use any command ex. "yum install", "systemctl start httpd".


    Take advantage of your Red Hat Developers membership and download RHEL today at no cost.

    Last updated: March 22, 2023

    Recent Posts

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.