Hello team I hope this message finds you all well. I am reaching out to seek you . . .

Syed Khalandar:
Hello team
I hope this message finds you all well. I am reaching out to seek your assistance with a project related to the course “Ansible for Absolute Beginners.”
The project entails developing an Ansible playbook to automate the deployment of the KodeKloud E-Commerce Application. While I have made progress with the playbook, I encountered an issue related to executing MySQL queries. As a result, I am currently facing the following error:
FYI: i am running ansible from my local censtos machine for amazon linux ec2 machine in aws t2.micro

fatal: [server1]: FAILED! => {"changed": false, "msg": "A MySQL module is required: for Python 2.7 either PyMySQL, or MySQL-python, or for Python 3.X mysqlclient or PyMySQL. Consider setting ansible_python_interpreter to use the intended Python version."}

the below is the playbook code

---
- name: Install Apache HTTP Server
  hosts: server1
  become: true
  tasks:
    - name: Install firewalld Server
      package:
        name: firewalld
        state: present
      
    - name: Start and enable firewalld Server
      service:
        name: firewalld
        state: started
        enabled: yes
    
    - name: Install mariadb Server
      package:
        name: mariadb-server
        state: present
      
    - name: Start and enable mariadb Server
      service:
        name: mariadb
        state: started
        enabled: yes
    
    - name: Allow port 3306/tcp in firewall
      shell: "firewall-cmd --permanent --zone=public --add-port=3306/tcp"
    
    - name: Reload firewall rules
      shell: "firewall-cmd --reload"
    



    # - name: Create the database
    #   mysql_db:
    #     name: ecomdb
    #     state: present
    #     login_user: root
    #     login_password: ''

    # - name: Create the user
    #   mysql_user:
    #     name: ecomuser
    #     password: ecompassword
    #     priv: "*.*:ALL"
    #     state: present
    #     host: localhost
    #     login_user: root
    #     login_password: ''

    # - name: Flush privileges
    #   mysql_db:
    #     name: '*.*'
    #     state: flush_privileges
    #     login_user: root
    #     login_password: ""
  


#   mysql -u root -p
# CREATE DATABASE ecomdb;
# CREATE USER 'ecomuser'@'localhost' IDENTIFIED BY 'ecompassword';
# GRANT ALL PRIVILEGES ON *.* TO 'ecomuser'@'localhost';
# FLUSH PRIVILEGES;

    - name: Install Apache HTTP Server
      package:
        name: httpd
        state: present
    
    - name: Start and enable Apache HTTP Server
      service:
        name: httpd
        state: started
        enabled: yes
    
    - name: Install php package
      yum:
        name: php
        state: present


    - name: Install php-mysql package
      yum:
        name: php-mysql
        state: present

    - name: Allow port 80/tcp in firewall
      shell: "firewall-cmd --permanent --zone=public --add-port=80/tcp"

    - name: Reload firewall rules
      shell: "firewall-cmd --reload"

    - name: Replace string in httpd.conf
      shell: "sed -i 's/index.html/index.php/g' /etc/httpd/conf/httpd.conf"
    
    - name: Restart httpd service
      service:
        name: httpd
        state: restarted
    
    - name: Install Git package
      yum:
        name: git
        state: present

    - name: Clone the repository
      git:
        repo: <https://github.com/kodekloudhub/learning-app-ecommerce.git>
        dest: /var/www/html/
    
    - name: Replace IP address in index.php
      shell: "sudo sed -i 's/172.20.1.101/localhost/g' /var/www/html/index.php"
      

  

As a temporary solution, I have commented out the problematic lines and am running the MySQL queries manually. However, I would greatly appreciate your guidance to identify the root cause of the issue and find a permanent solution.
To address the problem, I have already attempted installing PyMySQL on my Amazon Linux 2 EC2 server, but unfortunately, this did not resolve the error.
If anyone has encountered a similar issue before or has expertise in Ansible and MySQL integration, I would be grateful for any insights or suggestions you may have.
Thank you all in advance for your support and valuable input. Your assistance will undoubtedly aid in resolving this challenge and further enhance our understanding of Ansible.

Ly Quoc Bao:
Hey, please give it a try with this post https://medium.com/@randima.somathilaka/ansible-throws-pymysql-module-required-error-b93e81661c6a

Syed Khalandar:
Thanks for the reply @Ly Quoc Bao

but i am getting /usr/local/Cellar/ansible/8.2.0_2/bin/pip3
zsh: no such file or directory: /usr/local/Cellar/ansible/8.2.0_2/bin/pip3

Ly Quoc Bao:
Hey @Syed Khalandar,
Can you install pymysql in your client ansible and try again?
For example: pip3 install pymysql

Syed Khalandar:

  1. The initial issue was related to the deployment of an application on an EC2 instance where a database was created. It was discovered that the pymysql library needed to be installed on the EC2 instance for the application to work properly.
  2. Despite attempting to manually install the pymysql library and then rerunning the Ansible code, the same error persisted, indicating that the issue wasn’t resolved.
  3. To address this, a decision was made to include the installation of the pymysql library directly in the Ansible code, right before performing the database operations. This approach was implemented to ensure that the required library was installed within the context of Ansible’s operations.
  4. The specific Ansible task that was added to install pymysql looked like this:
- name: Install pymysql
  pip:
    name: pymysql
    executable: pip3
  1. The realization was that Ansible operates within its own python virtual environment, including its own set of libraries, which can sometimes lead to library-related issues.
  2. Adding the installation task for pymysql within the Ansible code proved effective. This approach ensured that the required library was properly installed within Ansible’s context, leading to the successful execution of the application deployment and database operations.
    In summary, addressing the library dependency issue by including the installation of pymysql directly within the Ansible playbook allowed for the seamless deployment and operation of the application on the EC2 instance.