从gitlabci运行Ansible playbook命令

时间:2018-10-06 06:17:17

标签: ansible gitlab-ci gitlab-ci-runner

我最近在ansible中学习了cloudformation模块,并希望在gitlab CI中使用它。

下面是我的剧本:

- name: provision stack
  hosts: localhost
  connection: local
  gather_facts: false

  # Launch the cloudformation-example.yml template.  Register the output.

  tasks:
  - name: launch ansible cloudformation example
    cloudformation: >
      stack_name="ansible-cloudformation" state=present
      region=us-east-1 disable_rollback=true
      template=files/simple-rds.yml
    args:
      template_parameters:
        vpcid: vpc-0123456
        application: abc
        appSubnetCidr1: 201.0.0.0/20
        appSubnetCidr2: 201.0.0.0/22
        dbCreateNewParamsGroup: true
        dbInstanceType: db.t2.micro
        dbName: testdb
        dbSubnetId1: subnet-87654321
        dbSubnetId2: subnet-12345678
        dbUsername: master_user
        environment: development
        product: ""
        dbPassword: ""
        techContact: ""
    register: stack
  - name: show stack events
    debug: msg="My stack events are {{stack.events}}"

现在,我想使用gitlab CI运行此剧本。为此,我创建了一个通用的ansible图像,以便使用同一图像运行不同的剧本。

Ansible图片Docker文件:

FROM ubuntu:18.04
# File Author / Maintainer

# Install ansible
RUN apt-get update
RUN apt-get install software-properties-common -y
RUN apt-get update
#RUN apt-add-repository -y ppa:ansible/ansible
#RUN apt-get update
RUN apt-get -y install ansible

# Install Pip
RUN apt install python-pip -y

# Install boto3
RUN pip install boto3

# Install boto
RUN pip install boto 

现在在.gitlab-ci.yml中。

stages:
  - build

build:master:
  image: vrathore/ansibleimage:latest
  script:
    - touch  ~/.boto
    - echo "[Credentials]" > ~/.boto
    - echo "aws_access_key_id = AKIAIACZXXXXXXXXXXXX" >> ~/.boto
    - echo "aws_secret_access_key = P5lO8H9tXXXXXXXXXXXXX" >> ~/.boto
    - cp simple-rds.yml /etc/ansible/playbook/files/simple-rds.yml
    - cp eb_playbook.yml /etc/ansible/playbook/eb_playbook.yml
    - cd /etc/ansible/playbook && ansible-playbook eb_playbook.yml -vvv
  stage: build
  only:
    - master

在上面的脚本中,我在Boto文件中添加了凭据,并复制了剧本脚本(eb_playbook.yml)和cloudformation脚本(simple-rds.yml),并运行了playbook run命令,但我得到了

cp: cannot stat 'simple-rds.yml': No such file or directory 

如何复制两个文件(eb_playbook.yml和simple-rds.yml)。这两个文件都存在gitlab项目中,并运行ansible playbook命令。

0 个答案:

没有答案