No one likes repetitive tasks. With Ansible, IT admins can begin automating away the drudgery from their daily tasks. Automation frees admins up to focus on efforts that help deliver more value to the business by speeding time to application delivery, and building on a culture of success. Ultimately, Ansible gives teams the one thing they can never get enough of time. Allowing smart people to focus on smart things.
Ansible is a simple automation language that can perfectly describe an IT application infrastructure. It’s easy-to-learn, self-documenting, and doesn’t require a grad-level computer science degree to read. Automation shouldn’t be more complex than the tasks it’s replacing.
When talk about ansible & pythin, We’re taking the automation into next level with limitless possibilities. You can imagine writing piece of python code which call Ansible to execute commands on servers.
While you can invoice commands directly from python code, But by using Ansible, You’re utilizing the benefits of simplicity, using Ansible modules, Idempotency and more…
Here we’ll move forward Ansible & python installation & running playbook from it.
Platform
- OS: Centos 7.9
- Ansible version: 2.9
- Python version: 3.6.8
Ansible installation
Ansible installation is pretty simple, You can follow procedures from Ansible website here
For centos, Ansible is part of EPEL-Release. Start with installation of EPEL-Release:
sudo yum install epel-release
Install Ansible:
sudo yum install ansible
Verify the installation:
ansible --version
Python installation
Install Python3:
yum install -y python3
Verify the installation:
python3
Install python modules using pip3 ( upgrade pip3 to latest version ):
yum install -y python3-pip export LC_ALL="en_US.utf8" pip install --upgrade pip pip3 install ansible_runner
Create simple playbook
Let’s create a very simple playbook to print Hello World on local machine and text executing the playbook :
--- - name: "Simple playbook" hosts: localhost connection: local tasks: - name: Print message debug: msg: Hello Ansible World
Execute the playbook:
ansible-playbook playbook.yml
Playbook execution output should be like this:
[root@localhost /]# ansible-playbook playbook.yaml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [Simple playbook] ********************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************** ok: [localhost] TASK [Print message] ************************************************************************************************************************************************ ok: [localhost] => { "msg": "Hello Ansible World" } PLAY RECAP ********************************************************************************************************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Running Ansible playbook from python
Running Ansible playbook from python, It has many ways to run it. The below is the simplest way to run it & will use ansible-runner python module.
For the below code, Replace the following variables:
- private_data_dir with the directory path that have the yml file
- playbook with the playbook file name
import ansible_runner r = ansible_runner.run(private_data_dir='/ansible', playbook='playbook.yml') print("{}: {}".format(r.status, r.rc))
Execute the python application:
python3 execute.py
Later on, We’ll discuss to fine-tune it to pass variables, execute on remote hosts,…
Using this approach, You’ll have endless possibilities of automation & integrations. You can use it in any solution like VMware, Cisco, Hardware,….