【问题标题】:Ansible: Failed to restart apache2.service: Connection timed outAnsible:无法重新启动apache2.service:连接超时
【发布时间】:2021-03-31 16:33:39
【问题描述】:

我正在使用 Ansible AWX 发出重启命令来重启主机上的 apache2 服务。 restart 命令包含在剧本中。

---
- name: Manage Linux Services
  hosts: all
  tasks:
  - name: Restart a linux service
    command: systemctl restart '{{ service_name }}'
    register: result
    ignore_errors: yes

  - name: Show result of task
    debug:
     var: result

---
- name: Manage Linux Services
  hosts: all
  tasks:
  - name: Restart a linux service
    ansible.builtin.service:
      name: '{{ service_name }}'
      state: restarted
    register: result
    ignore_errors: yes

  - name: Show result of task
    debug:
     var: result

但是,当我运行命令时,我收到以下错误:

"Failed to restart apache2.service: Connection timed out",
"See system logs and 'systemctl status apache2.service' for details."

我试图找出问题所在,但还没有运气。

【问题讨论】:

    标签: ansible ansible-awx


    【解决方案1】:

    我后来找到了问题的原因。

    这是我修复它的方法

    重启命令需要sudo 访问权限才能运行,而我的命令中缺少此权限。

    我所要做的就是添加become: true 命令,以便我可以以root 权限执行该命令。

    所以我的剧本此后看起来像这样:

    ---
    - name: Manage Linux Services
      hosts: all
      tasks:
      - name: Restart a linux service
        command: systemctl restart '{{ service_name }}'
        become: true
        register: result
        ignore_errors: yes
    
      - name: Show result of task
        debug:
         var: result
    

    ---
    - name: Manage Linux Services
      hosts: all
      tasks:
      - name: Restart a linux service
        ansible.builtin.service:
          name: '{{ service_name }}'
          state: restarted
        become: true
        register: result
        ignore_errors: yes
    
      - name: Show result of task
        debug:
         var: result
    

    如果您想在 Ansible AWX 上实现此目的,另一种方法是勾选作业模板中的 Privilege Escalation 选项。

    如果启用,这将以管理员身份在作业模板中运行选定的剧本。

    就是这样。

    我希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 2019-08-20
      • 2014-12-12
      • 2016-10-24
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 2018-03-14
      • 2014-02-17
      相关资源
      最近更新 更多