这是什么意思,以及如何解决Ansible错误“'NoneType'对象没有属性'read'”?

时间:2019-07-15 10:07:22

标签: ansible ansible-2.x ansible-inventory ansible-template

我正在尝试通过Ansible安装MySql 5.7版本,但这是说

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'"}

要解决此问题,我尝试在环境部分中设置代理,但仍然失败。 下面是我尝试通过其安装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

我希望通过Ansible脚本将MySQL安装在我的RHEL 7.4上。

1 个答案:

答案 0 :(得分:0)

前言

  • 帮个忙,在您的任务中为ansible模块参数采用仅Yaml的现代语法。它更易于阅读,不易出错,并且可以让短毛猫(例如yamllint ...)更早地捕获更多错误。
  • documentation中所述,请勿在{{1​​}}模块中使用循环。这将单独处理每个包裹。而是直接将软件包列表发送到yum参数。

我下面的示例遵循这两个规则。

实际答案

您仅为处理程序任务设置了代理环境以重新启动mysql

如果您有几个需要代理的任务,并且可以在任何地方设置它(例如,即使不需要的任务也可以),则可以在游戏级别进行

name

如果这样做不行,则需要在每个需要代理的任务上定义代理(在您的情况下为yum任务)。但是,您可以使用play(或广告资源)变量来使文字更简洁:

---
- 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...