0

I am trying to install MySql 5.7 version through Ansible, but it is saying

fatal: [192.168.1.45]: FAILED! => {"changed": false, "failed": true, "msg": "Failure downloading http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm, 'NoneType' object has no attribute 'read'"}

To fix the issue i tried setting up proxy in the environment section, but still it is failing. Below is the script, through which I am trying to install MySQL

---
- hosts: dbservers
  remote_user: yabx
  become: true
  become_method: sudo
  become_user: root
  tasks:
    - name: Install MySQL 5.7 repo
      yum: name=http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm state=present

    - name: Install MySQL 5.7
      yum: pkg={{ item }}
      with_items:
      - mysql-community-server
      - mysql-community-client
      - MySQL-python

    - name: Start the MySQL service
      service: name=mysqld state=started enabled=true

    - name: Change mysql root password and keep track in
      shell: |
        password_match=`awk '/A temporary password is generated for/ {a=$0} END{ print a }' /var/log/mysqld.log | awk '{print $(NF)}'`
        echo $password_match
        mysql -uroot -p$password_match --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'PassW0rd'; flush privileges; "
        echo "[client]"
        user=root
        password=PassW0rd > /root/.my.cnf
      args:
        creates: /root/.my.cnf
      register: change_temp_pass
      notify: restart mysqld

    - meta: flush_handlers
    - debug:
        var: change_temp_pass

  handlers:
    - name: restart mysqld
      service:
        name: mysqld
        state: restarted
      environment:
        http_proxy: http://***:***@192.168.1.45:8080
        https_proxy: https://***:***@192.168.1.45:8080

I want MySQL to be installed on my RHEL 7.4 through Ansible script.

Asad Ali
  • 389
  • 1
  • 10
  • 28

1 Answers1

0

Foreword

  • Do yourself a favor and adopt the modern yaml only syntax for ansible module parameters in your tasks. It is easier to read, less error prone, and will let linters (e.g. yamllint...) capture more errors earlier.
  • As explained in the documentation, don't use loops with the yum module. This will process each package individually. Instead, send the list of packages directly to the name parameter.

My examples below are following those 2 rules.

Actual answer

You have set the proxy environment only for your handler task to restart mysql

If you have several tasks that need a proxy and that it is ok to set it everywhere (e.g. even for tasks that don't need it), you can do it at play level

---
- name: Install and configure mysql
  hosts: dbservers
  remote_user: yabx
  become: true

  environment:
    http_proxy: http://***:***@192.168.1.45:8080
    https_proxy: https://***:***@192.168.1.45:8080
    no_proxy: .my-company.com, .my-other-internal-site.com

  tasks: 
    # ... tasks all executed with above env...

If this is not ok, then you need to define the proxy on each task that needs it (the yum tasks in your case). You can however use a play (or iventory) var to make the writing more concise:

---
- name: Install and configure mysql
  hosts: dbservers
  remote_user: yabx
  become: true

  vars:
    my_proxy_env:
      http_proxy: http://***:***@192.168.1.45:8080
      https_proxy: https://***:***@192.168.1.45:8080
      no_proxy: .my-company.com, .my-other-internal-site.com

  tasks: 
    - name: Install MySQL 5.7 repo
      yum:
        name: http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
        state: present
      environment: "{{ my_proxy_env }}"

    - name: Install MySQL 5.7
      yum:
        name:
          - mysql-community-server
          - mysql-community-client
          - MySQL-python
      environment: "{{ my_proxy_env }}"

   # ... other tasks without proxy ...
Zeitounator
  • 38,476
  • 7
  • 53
  • 66