首页 文章

ansible playbook:无法以root身份启动服务

提问于
浏览
1

在一天的大部分时间里,我一直在敲打这个,即使在我的系统管理员的帮助下,我已经尽力而为,但没有成功 . (请注意,我根本不是一名安全专家,我今天发现了这一点)

context:我尝试通过gitlab运行java服务的实现持续集成 . 管道将在推送时运行测试,打包jar,然后运行ancible playbook以停止现有服务,更换jar,再次启动服务 . 我们有这个用于谷歌 Cloud 的 生产环境 ,它工作正常 . 我想在此之前添加一个额外的步骤,在localhost上做同样的事情 .

我只是无法理解为什么ansible没有做“sudo service XXXX stop | start” . 我得到的只是

致命:[localhost]:失败! => {“已更改”:false,“failed”:true,“module_stderr”:“抱歉,请再试一次 . \ n [sudo via ansible,key = nbjplyhtvodoeqooejtlnhxhqubibbjy] password:\ nsudo:1次密码尝试不正确\ n”,“ module_stdout“:”“,”msg“:”MODULE FAILURE“,”rc“:1}

这是我调用的gitlab管道阶段:

indexer-integration:
stage: deploy integration
script:
   - ansible-playbook -i ~/git/ansible/inventory deploy_integration.yml --vault-password-file=/home/gitlab-runner/vault.txt
 when: on_success

vault.txt包含保险库加密密码 . 这是deploy_integration.yml

---
- name: deploy integration saleindexer
  hosts: localhost
  gather_facts: no
  user: test-ccc #this is the user that I created as a test
  connection: local
  vars_files:
     - /home/gitlab-runner/secret.txt  #holds the sudo password
  tasks:
    - name: Stop indexer
      service: name=indexer state=stopped
      become: true
      become_user: root
    - name: Clean JAR
      become: true
      become_user: root
      file:
         state: absent
         path: '/PATH/indexer-latest.jar'
    - name: Copy JAR
      become: true
      become_user: root
      copy:
          src: 'target/indexer-latest.jar'
         dest: '/PATH/indexer-latest.jar'
    - name: Start indexer
        service: name=indexer state=started
      become: true
      become_user: root

用户'test-ccc'是我创建的另一个用户(组根和sudoer文件的一部分),以确保它不是与gitlab-runner用户相关的问题(并且因为这里显然没有人可以记住该用户的sudo密码xD)

我尝试了很多东西,包括

shell: echo 'password' | sudo -S service indexer stop

在命令行中工作 . 但是如果由ansible执行,我得到的只是一条提示信息,要求我输入sudo密码

谢谢

编辑每个评论请求:secret.txt具有:

ansible_become_pass: password

在命令行中使用该用户(su user / sudo service start ....)并提示输入该密码时,它可以正常工作 . 我认为问题是ansible总是提示输入密码,或者密码没有正确传递给任务 . sshd_config有一行'PermitRootLogin yes'

1 回答

  • 0

    好的,感谢techraf的回复(现已删除),我注意到了这一行

    user: test-ccc
    

    实际上是没用的,一切都还是由'gitlab-runner'用户运行 . 所以我 :

    • 将我的所有操作都放在脚本postbuild.sh中

    • 将gitlab-runners添加到sudoers并为该脚本提供了nopassword

    gitlab-runner ALL =(ALL)NOPASSWD:/home/PATH/postbuild.sh

    • 删除了有关从ansible任务传递密码和秘密的事情,并改为使用:

    shell:sudo -S /home/PATH/postbuild.sh

    这样工作,脚本执行,服务停止/启动 . 我将此标记为已回答,即使使用service:name = indexer state = started并为用户提供NOPASSWD:ALL仍然会导致错误(我对问题的评论中的那个) . 如果有人能在评论中阐明这一点......

相关问题