我可以在剧本中执行ansible剧本吗?

时间:2019-07-22 16:40:17

标签: ansible

我正在运行一本手册来配置我的主服务器,然后我要配置只能从主服务器访问的节点。

如果我尝试从主剧本的任务执行ansible-playbook,则连接被拒绝,但是如果我在主控台中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内部运行时有效

1 个答案:

答案 0 :(得分:0)

我在一个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