【问题标题】:Can I execute an ansible playbook inside a playbook?我可以在剧本中执行 ansible 剧本吗?
【发布时间】:2019-07-22 16:40:17
【问题描述】:

我正在运行一个剧本来配置我的主节点,之后我想配置只能从主节点访问的节点。

如果我尝试从主 playbook 的任务中执行 ansible-playbook,连接会被拒绝,但如果我在 master 中 ssh 登录并运行相同的 ansible-playbook 命令,一切正常

这是主要的ansible-playbook,所有任务都执行到最后一个

    - name: "Copy private key to Workstation"
      copy:
        src: ~/.ssh/private.pem
        dest: /home/ubuntu/.ssh/id_rsa
        mode: 0400

    - name: "Executing playbookNodes on Workstation"
      shell: "sudo ansible-playbook -i /home/ubuntu/inventory.yaml playbookNodes.yaml"
      tags:
        - remote_playbook

如果我运行上面的代码,我会得到:

fatal: [workstation]: FAILED! => {
    "changed": true,
    "cmd": "ssh ubuntu@12.0.11.10",
    "delta": "0:00:00.065269",
    "end": "2019-07-22 16:05:31.183599",
    "invocation": {
        "module_args": {
            "_raw_params": "ssh ubuntu@12.0.11.10",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "warn": true
        }
    },
    "msg": "non-zero return code",
    "rc": 255,
    "start": "2019-07-22 16:05:31.118330",
    "stderr": "Warning: Permanently added '12.0.11.10' (ECDSA) to the list of known hosts.\r\nubuntu@12.0.11.10: Permission denied (publickey).",
    "stderr_lines": [
        "Warning: Permanently added '12.0.11.10' (ECDSA) to the list of known hosts.",
        "ubuntu@12.0.11.10: Permission denied (publickey)."
    ],
    "stdout": "",
    "stdout_lines": []
}

但当我运行 ssh -i private.pem ubuntu@master 并在主控 ansible-playbook -i inventory.yaml playbookNodes.yaml 内运行时有效

【问题讨论】:

  • 您可能应该考虑将您的第一本剧本转换为 Python。

标签: ansible


【解决方案1】:

我在 AWS 的单个剧本中做了一些非常相似的事情,如下所示,您可以更改它,但是您正在配置机器:

- name: Provision machine
  hosts: localhost
  pre_tasks:
    - name: Provision ec2 instance
      ec2:
        // Do AWS ec2 provisioning stuff <snip>
      register: new_ec2_instances

  - name: Add new instance to host group
    add_host:
      groupname: newboxes
      hostname: "{{ item.private_ip }}"
    with_flattened: "{{ new_ec2_instances }}"

  - name: wait for ssh
    wait_for:
      host: "{{ item.private_ip }}"
      port: 22
      delay: 10
      timeout: 180
      state: started
    with_flattened: "{{ ec2.instances }}"

- name: Do stuff to new boxes
  hosts: newboxes
  roles:
    - yourplaybookNodesyamlhere

【讨论】:

    猜你喜欢
    • 2017-12-22
    • 2017-01-04
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    相关资源
    最近更新 更多