Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
800 views
in Technique[技术] by (71.8m points)

yaml - Ansible run a task based on something in output

I'm new in Ansible and I try do some practice playbook. I write a playbook to deploy a docker container as follow. I write a task in block section if an error happened, run a task in rescue section based on Failed message content. For example I want to run a specific task to delete existing container if the Failed message is something like this:

failed: [192.168.1.140] (item=stderr_lines) => {"changed": true, "cmd": ["docker", "run", "-itd", "--name", "h1", "-p", "80:80", "httpd"], "delta": "0:00:00.016385", "end": "2021-01-04 03:00:55.403364", "failed_when_result": true, "item": "stderr_lines", "msg": "non-zero return code", "rc": 125, "start": "2021-01-04 03:00:55.386979", "stderr": "/usr/bin/docker-current: Error response from daemon: Conflict. The container name "/h1" is already in use by container bc5cc803a5f4321358992d06097ce271f3a63b8eba19900cfc0d23e321a4e243. You have to remove (or rename) that container to be able to reuse that name.. See '/usr/bin/docker-current run --help'.", "stderr_lines": ["/usr/bin/docker-current: Error response from daemon: Conflict. The container name "/h1" is already in use by container bc5cc803a5f4321358992d06097ce271f3a63b8eba19900cfc0d23e321a4e243. You have to remove (or rename) that container to be able to reuse that name..", "See '/usr/bin/docker-current run --help'."], "stdout": "", "stdout_lines": []}"


My playbook is as follow but it didn't work correctly. Sometimes error in reading dictionary or "Unexpected templating type error occurred on". Can somebody guide me what should I write??

    - name: run a container
      vars:
       - run_container: docker run -itd --name h1 -p 80:80 httpd
       - rm_container: docker stop h1 && docker rm h1
    
      hosts: 192.168.x.x
      tasks:
      - name: check docker container
        block:
        - name: run a container httpd
          command: "{{run_container}}"
          register: rss
          with_items:
            - "stderr_lines"
          failed_when: "'Error' in rss.stderr"
    
        rescue:
         - name: iterate over list
           debug:
             msg: "{{item.value}}"
           loop: "{{rss | dict2items}}"
    
         - name: remove the exited container
           command: "{{rm_container}}"
           register: rs
           with_items:
            - "{{rss | dict2items}}"
           when: item.value is search("The container name .* is already in use")
question from:https://stackoverflow.com/questions/65559990/ansible-run-a-task-based-on-something-in-output

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...