【问题标题】:Ansible triggering a task for **specific host** from a roleAnsible 从角色触发**特定主机**的任务
【发布时间】:2018-10-06 20:44:52
【问题描述】:

我想将剧本中的特定任务运行到单个主机。有没有更好的方法来做到这一点?

自定义剧本

---
- hosts: swarm_manager
  roles:
  - custom_role

roles/custom_role/tasks/main.yml

---
- name: checking for ip
  shell: docker service ps service | grep Running | head -n1 | awk '{print $1}'
  register: ip

- name: killing the container
  **hosts: "{{ip.stdout}}"**
  shell: docker kill $(docker service ps | grep service | awk '{print $1}')

【问题讨论】:

    标签: linux automation ansible devops


    【解决方案1】:

    您可以限制执行以下操作:

    ---
    - name: checking for ip
      shell: docker service ps service | grep Running | head -n1 | awk '{print $1}'
      register: ip
    
    - name: killing the container
      run_once: true
      delegate_to: "{{ip.stdout}}"
      shell: docker kill $(docker service ps | grep service | awk '{print $1}')
    

    我是这样测试的:

    ---
    - hosts: linux
      roles:
      - test_role
    

    然后:

    - name: Set Fact
      set_fact:
        ip: 10.100.10.10
    
    - name: Debug
      debug:
        msg: "{{hostvars[groups['linux'][0]]['ansible_default_ipv4']['address']}}"
      run_once: true
      delegate_to: "{{ip}}"
    

    【讨论】:

    • 谢谢。但我还有一个担心。由于 ip 是动态生成的并且它不在主机文件中,所以 ansible 会抛出一个错误,说无法 ssh 并且权限被拒绝。我知道我们可以在 vars 中传递 ansible_ssh_private_key_file。但是有没有办法为 ansible ssh 连接设置默认密钥?
    • 嗨!你可以在你的 ansible.cfg 中使用它,甚至可以使用你的 .ssh/config 文件
    猜你喜欢
    • 2020-10-05
    • 2019-01-14
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2015-08-26
    • 2018-11-23
    相关资源
    最近更新 更多