Manage environment variables with ansible - environment-variables

i need set the environment variable to build some app at startup, i use vars in the playbook file:
---
- hosts: all
sudo: yes
vars:
build_env:
VAR1: value1
VAR2: value2
VAR3: value3
And set with environment into task to build app
- name: Build app
environment: "{{build_env}}"
command: "{{ item }} chdir={{app1}}"
with_items:
- npm install
- bower
- gulp
Also i need add the same variables to /etc/environment but i would not replicate the same variables in another task, any idea?

ansible-playbook -c local -i 127.0.0.1, test.yml
test.yml:
---
- hosts: all
sudo: yes
vars:
build_env:
VAR1: value1
VAR2: value2
VAR3: value3
tasks:
- name: Build app
environment: "{{build_env}}"
shell: source /etc/environment; echo $VAR1 > ~/111
- name: Build app
environment: "{{build_env}}"
shell: source /etc/environment;env >> /root/111
Tested vars:
# cat /etc/environment
VAR1=2
export VALERY=x
export VAR2=ss
CCCCCCCCCC=xx
The result:
# cat ~/111
value1
HOSTNAME=38b07f75ca28
SHLVL=1
HOME=/root
VALERY=x
_=/usr/local/bin/ansible-playbook
TERM=xterm
VAR1=value1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
VAR2=value2
VAR3=value3
LANG=C
LC_MESSAGES=C
PWD=/
LC_ALL=C
Conclusiosn:
CCCCCCCCCC=xx - wasn't exported
The environment: override the variables, so you should export all
variables in /etc/environment

Related

Ansible playbook execute only specific tasks per environment

I have a playbook with a bunch of tasks:
vars:
params_ENV_SERVER: "{{ lookup('env', 'ENV_SERVER') }}"
params_UML_SUFFIX: "{{ lookup('env', 'UML_SUFFIX') }}"
tasks:
- name: delete previous files
shell: ssh deploy#{{ params_ENV_SERVER }} sudo rm -rf /opt/jenkins-files/*
become: true
become_user: deploy
- name: create build dir
shell: ssh deploy#{{ params_ENV_SERVER }} sudo mkdir -p /opt/jenkins-files/build
become: true
become_user: deploy
- name: chown build dir
shell: ssh deploy#{{ params_ENV_SERVER }} sudo chown -R deploy:deploy /opt/jenkins-files
become: true
become_user: deploy
Which I calling from Jenkinsfile for PROD and QA env-s:
withEnv(["ENV_SERVER=192.168.1.30","UML_SUFFIX=stage-QA"]) {
sh "ansible-playbook nginx-depl.yml --limit 127.0.0.1"
}
withEnv(["ENV_SERVER=192.168.1.130","UML_SUFFIX=stage-PROD"]) {
sh "ansible-playbook nginx-depl.yml --limit 127.0.0.1"
Is it possible to modify playbook somehow, to execute on QA all tasks and on PROD only 2-nd and 3-rd?
Is this what are you looking for?
- name: delete previous files
shell: ssh deploy#{{ params_ENV_SERVER }} sudo rm -rf /opt/jenkins-files/*
become: true
become_user: deploy
when: "params_UML_SUFFIX == 'stage-QA'"
- name: create build dir
shell: ssh deploy#{{ params_ENV_SERVER }} sudo mkdir -p /opt/jenkins-files/build
become: true
become_user: deploy
when: "params_UML_SUFFIX == 'stage-QA'" or
"params_UML_SUFFIX == 'stage-PROD'"
- name: chown build dir
shell: ssh deploy#{{ params_ENV_SERVER }} sudo chown -R deploy:deploy /opt/jenkins-files
become: true
become_user: deploy
when: "params_UML_SUFFIX == 'stage-QA'" or
"params_UML_SUFFIX == 'stage-PROD'"
Optionally, "Ansible-way" would be creating the inventory
shell> cat hosts
[prod]
192.168.1.130
[qa]
192.168.1.30
and declare all hosts in the playbook
shell> cat playbook.yml
- hosts: all
tasks:
- debug:
msg: "Delete previous files.
Execute module file on {{ inventory_hostname }}"
when: inventory_hostname in groups.qa
- debug:
msg: "Create build dir.
Execute module file on {{ inventory_hostname }}"
when: inventory_hostname in groups.qa or
inventory_hostname in groups.prod
- debug:
msg: "Chown build dir.
Execute module file on {{ inventory_hostname }}"
when: inventory_hostname in groups.qa or
inventory_hostname in groups.prod
You can omit "become: true" and "become_user: deploy" and declare the remote user on the command-line. For example
shell> ansible-playbook -u deploy -i hosts playbook.yml
gives (abridged)
TASK [debug] ****
skipping: [192.168.1.130]
ok: [192.168.1.30] =>
msg: Delete previous files. Execute module file on 192.168.1.30
TASK [debug] ****
ok: [192.168.1.130] =>
msg: Create build dir. Execute module file on 192.168.1.130
ok: [192.168.1.30] =>
msg: Create build dir. Execute module file on 192.168.1.30
TASK [debug] ****
ok: [192.168.1.30] =>
msg: Chown build dir. Execute module file on 192.168.1.30
ok: [192.168.1.130] =>
msg: Chown build dir. Execute module file on 192.168.1.130
You can limit the execution to particular hosts or groups. For example, the command below would execute on prod group only
shell> ansible-playbook -u deploy -i hosts playbook.yml --limit prod
gives (abridged)
TASK [debug] ****
skipping: [192.168.1.130]
TASK [debug] ****
ok: [192.168.1.130] =>
msg: Create build dir. Execute module file on 192.168.1.130
TASK [debug] ****
ok: [192.168.1.130] =>
msg: Chown build dir. Execute module file on 192.168.1.130
Notes
"Ansible-way" is to execute modules on the remote hosts.
Replace the debug tasks with file
Integrate into one tasks "create build dir" and "chown build dir"
If you run the playbook as user deploy you can omit the parameter "-u deploy"

CircleCI environmental variables for HEROKU not being set properly causing GIT to fail

I am a CircleCI user, and I am setting up an integration with Heroku.
I want to do the following, and setup security with integrations with dockerHub and also to Heroku from the CircleCI portal page, using this config.yml file.
The problem is that CircleCI doesn't seem to know what these variables should be set to, and instead just echos.
${HEROKU_API_KEY} ${HEROKU_APP}
config.yml
version: 2
jobs:
build:
working_directory: ~/springboot_swagger_example-master-cassandra
docker:
- image: circleci/openjdk:8-jdk-browsers
steps:
- checkout
- restore_cache:
key: springboot_swagger_example-master-cassandra-{{ checksum "pom.xml" }}
- run: mvn dependency:go-offline
- save_cache:
paths:
- ~/.m2
key: springboot_swagger_example-master-cassandra-{{ checksum "pom.xml" }}
- type: add-ssh-keys
- type: deploy
name: "Deploy to Heroku"
command: |
if [ "${CIRCLE_BRANCH}" == "master" ]; then
# Install Heroku fingerprint (this is heroku's own key, NOT any of your private or public keys)
echo 'heroku.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAu8erSx6jh+8ztsfHwkNeFr/SZaSOcvoa8AyMpaerGIPZDB2TKNgNkMSYTLYGDK2ivsqXopo2W7dpQRBIVF80q9mNXy5tbt1WE04gbOBB26Wn2hF4bk3Tu+BNMFbvMjPbkVlC2hcFuQJdH4T2i/dtauyTpJbD/6ExHR9XYVhdhdMs0JsjP/Q5FNoWh2ff9YbZVpDQSTPvusUp4liLjPfa/i0t+2LpNCeWy8Y+V9gUlDWiyYwrfMVI0UwNCZZKHs1Unpc11/4HLitQRtvuk0Ot5qwwBxbmtvCDKZvj1aFBid71/mYdGRPYZMIxq1zgP1acePC1zfTG/lvuQ7d0Pe0kaw==' >> ~/.ssh/known_hosts
# git push git#heroku.com:yourproject.git $CIRCLE_SHA1:refs/heads/master
# Optional post-deploy commands
# heroku run python manage.py migrate --app=my-heroku-project
fi
- run: mvn package
- run:
name: Install Docker client
command: |
set -x
VER="17.03.0-ce"
curl -L -o /tmp/docker-$VER.tgz https://get.docker.com/builds/Linux/x86_64/docker-$VER.tgz
tar -xz -C /tmp -f /tmp/docker-$VER.tgz
mv /tmp/docker/* /usr/bin
- run:
name: Build Docker image
command: docker build -t joethecoder2/spring-boot-web:$CIRCLE_SHA1 .
- run:
name: Push to DockerHub
command: |
docker login -u$DOCKERHUB_LOGIN -p$DOCKERHUB_PASSWORD
docker push joethecoder2/spring-boot-web:$CIRCLE_SHA1
- run:
name: Setup Heroku
command: |
curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
chmod +x .circleci/setup-heroku.sh
.circleci/setup-heroku.sh
- run:
name: Deploy to Heroku
command: |
mkdir app
cd app/
heroku create
# git push https://heroku:$HEROKU_API_KEY#git.heroku.com/$HEROKU_APP.git master
echo ${HEROKU_API_KEY}
echo ${HEROKU_APP}
git push https://heroku:${HEROKU_API_KEY}#git.heroku.com/${HEROKU_APP}.git master
- store_test_results:
path: target/surefire-reports
- store_artifacts:
path: target/spring-boot-web-0.0.1-SNAPSHOT.jar
The problem is that CircleCI doesn't seem to know what these variables should be set to, and instead just echos.
${HEROKU_API_KEY}
${HEROKU_APP}
The question, and problem is why aren't these settings being detected automatically?
You need to set the value for the variables: https://circleci.com/docs/2.0/env-vars/
They are being echo'd because you're echoing them.
echo ${HEROKU_API_KEY}
echo ${HEROKU_APP}

Jobs not executed in circle ci workflow

I'm trying to cascade a series of tasking using the workflow syntax with circle ci. For some reason, only the build job seems to run - but my other jobs do not.
version: 2
jobs:
build:
docker:
- image: circleci/node:latest
steps:
- checkout
- restore_cache:
keys:
- sfdx-version-41-local
- run:
name: Install SFDX
command: pwd
- save_cache:
key: sfdx-version-41-local
paths:
- node_modules
auth:
steps:
- run:
name: Authenticate
command: ls -a
validate:
steps:
- run:
name: Validate
command: mkdir whocares
clean:
steps:
- run:
name: Remove Server Key
when: always
command: pwd
workflows:
version: 2
authenticate-and-deploy:
jobs:
- build
- auth
- validate
- clean
Ideally, I want to make sure each step finishes with a non zero exit code before moving to next step. But I'm not sure the the subsequent steps after build are not being executed.
Thanks,
If you really wants all of these jobs for some reason, the Workflows config is missing requirements and each job is missing its executor. I also fixed indenting. You can do this:
version: 2
jobs:
build:
docker:
- image: circleci/node:latest
steps:
- checkout
- restore_cache:
keys:
- sfdx-version-41-local
- run:
name: Install SFDX
command: pwd
- save_cache:
key: sfdx-version-41-local
paths:
- node_modules
auth:
docker:
- image: circleci/node:latest
steps:
- run:
name: Authenticate
command: ls -a
validate:
docker:
- image: circleci/node:latest
steps:
- run:
name: Validate
command: mkdir whocares
clean:
docker:
- image: circleci/node:latest
steps:
- run:
name: Remove Server Key
when: always
command: pwd
workflows:
version: 2
authenticate-and-deploy:
jobs:
- build
- auth:
requires: build
- validate:
requires: auth
- clean:
requires: validate
Here is what I ended up doing:
job-definition: &jobdef
docker:
- image: circleci/openjdk:latest-node-browsers
steps:
- checkout
- restore_cache:
keys:
- sfdx-6.8.2-local
- run:
name: Print branch
command: |
echo $CIRCLE_BRANCH
if [[$CIRCLE_BRANCH == 'master']];
then
echo "master";
else
echo "notMaster";
fi
- save_cache:
key: sfdx-6.8.2-local
paths:
- node_modules
- run:
name: "Validate Build"
command: |
#node_modules/sfdx-cli/bin/run force:mdapi:deploy -d src/ -u $USERNAME -c --testlevel RunLocalTests
echo CIRCLE_BRANCH
- run:
name: Push to Codecov.io
command: |
cp ~/tests/apex/test-result-codecoverage.json .
bash <(curl -s https://codecov.io/bash)
- store_artifacts:
path: ~/tests
- store_test_results:
path: ~/tests
version: 2
jobs:
static-analysis:
docker:
- image: circleci/openjdk:latest
steps:
- checkout
- restore_cache:
keys:
- pmd-v6.0.1
- run:
name: Install PMD
command: |
if [ ! -d pmd-bin-6.0.1 ]; then
curl -L "https://github.com/pmd/pmd/releases/download/pmd_releases/6.0.1/pmd-bin-6.0.1.zip" -o pmd-bin-6.0.1.zip
unzip pmd-bin-6.0.1.zip
rm pmd-bin-6.0.1.zip
fi
- save_cache:
key: pmd-v6.0.1
paths:
- pmd-bin-6.0.1
- run:
name: Run Static Analysis
command: |
pmd-bin-6.0.1/bin/run.sh pmd -d ./src/ -R $RULESET -f text -l apex -r static-analysis.txt -no-cache
- store_artifacts:
path: static-analysis.txt
build-enterprise:
<<: *jobdef
environment:
SCRATCH_DEF: workspace-scratch-def.json
URL: https://login.salesforce.com
NAME: $PROD_USERNAME
build-developer:
<<: *jobdef
environment:
SCRATCH_DEF: developer.json
URL: https://test.salesforce.com
NAME: $EVANDEV_USERNAME
workflows:
version: 2
test_and_static:
jobs:
- build-enterprise
#- build-developer
#- static-analysis
Two other thing I'm trying to do:
1. paramterize the build
2. run build on a PR

Are Instrumentation tests for Android Espresso available on CircleCi 2.0?

Are Instrumentation tests for Android Espresso available on CircleCI 2.0?
If yes, can anybody, please, help to configure config.yml file for me?
I’ve made thousand attempts and no luck. I can run unit tests, but not Instrumentation.
Thanks
The answer for this question is: yes. Instrumentation tests are possible for CircleCi. This is the configuration I have:
version: 2
jobs:
build:
working_directory: ~/code
docker:
- image: circleci/android:api-25-alpha
environment:
JVM_OPTS: -Xmx3200m
steps:
- checkout
- restore_cache:
key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
- run:
name: Chmod permissions #if permission for Gradlew Dependencies fail, use this.
command: sudo chmod +x ./gradlew
- run:
name: Download Dependencies
command: ./gradlew androidDependencies
- save_cache:
paths:
- ~/.gradle
key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
- run:
name: Setup emulator
command: sdkmanager "system-images;android-25;google_apis;armeabi-v7a" && echo "no" | avdmanager create avd -n test -k "system-images;android-25;google_apis;armeabi-v7a"
- run:
name: Launch emulator
command: export LD_LIBRARY_PATH=${ANDROID_HOME}/emulator/lib64:${ANDROID_HOME}/emulator/lib64/qt/lib && emulator64-arm -avd test -noaudio -no-boot-anim -no-window -accel on
background: true
- run:
name: Wait emulator
command: |
# wait for it to have booted
circle-android wait-for-boot
# unlock the emulator screen
sleep 30
adb shell input keyevent 82
- run:
name: Run Tests
command: ./gradlew connectedAndroidTest
- store_artifacts:
path: app/build/reports
destination: reports
- store_test_results:
path: app/build/test-results
The only problem with this configuration that it doesn't lead to successfull build because of not enough memory error. If somebody has better configuration, please, share.
I am running Android UI tests on CircleCI MacOS executor.
Here is my configuration:
version: 2
reference:
## Constants
gradle_cache_path: &gradle_cache_path
gradle_cache-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
workspace: &workspace
~/src
## Configurations
android_config: &android_config
working_directory: *workspace
macos:
xcode: "9.4.0"
shell: /bin/bash --login -eo pipefail
environment:
TERM: dumb
JVM_OPTS: -Xmx3200m
## Cache
restore_gradle_cache: &restore_gradle_cache
restore_cache:
key: *gradle_cache_path
save_gradle_cache: &save_gradle_cache
save_cache:
key: *gradle_cache_path
paths:
- ~/.gradle
## Dependency Downloads
download_android_dependencies: &download_android_dependencies
run:
name: Download Android Dependencies
command: ./gradlew androidDependencies
jobs:
ui_test:
<<: *android_config
steps:
- checkout
- run:
name: Setup environment variables
command: |
echo 'export PATH="$PATH:/usr/local/opt/node#8/bin:${HOME}/.yarn/bin:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin:/usr/local/share/android-sdk/tools/bin"' >> $BASH_ENV
echo 'export ANDROID_HOME="/usr/local/share/android-sdk"' >> $BASH_ENV
echo 'export ANDROID_SDK_HOME="/usr/local/share/android-sdk"' >> $BASH_ENV
echo 'export ANDROID_SDK_ROOT="/usr/local/share/android-sdk"' >> $BASH_ENV
echo 'export QEMU_AUDIO_DRV=none' >> $BASH_ENV
echo 'export JAVA_HOME=/Library/Java/Home' >> $BASH_ENV
- run:
name: Install Android sdk
command: |
HOMEBREW_NO_AUTO_UPDATE=1 brew tap homebrew/cask
HOMEBREW_NO_AUTO_UPDATE=1 brew cask install android-sdk
- run:
name: Install emulator dependencies
command: (yes | sdkmanager "platform-tools" "platforms;android-26" "extras;intel;Hardware_Accelerated_Execution_Manager" "build-tools;26.0.0" "system-images;android-26;google_apis;x86" "emulator" --verbose) || true
- *restore_gradle_cache
- *download_android_dependencies
- *save_gradle_cache
- run: avdmanager create avd -n Pixel_2_API_26 -k "system-images;android-26;google_apis;x86" -g google_apis -d "Nexus 5"
- run:
name: Run emulator in background
command: /usr/local/share/android-sdk/tools/emulator #Pixel_2_API_26 -skin 1080x2066 -memory 2048 -noaudio
background: true
- run:
name: Run Tests
command: ./gradlew app:connectedAndroidTest
https://gist.github.com/DoguD/58b4b86a5d892130af84074078581b87
I hope it helps

Setting enviroment variable in CircleCI using a command

I'm trying to set an environment variable (SHORT_HASH) to shorter github hash by running it as a command ('echo $CIRCLE_SHA1 | cut -c -7').
So, I'd want the hash 'b1e5ef8acff51c9218ccbf7152fae1d2049d03c5' to be shortened to 'b1e5ef8'
Here's a stripped down version of my circle.yml
machine:
python:
version: 2.7.3
services:
- docker
environment:
SHORT_HASH: 'echo $CIRCLE_SHA1 | cut -c -7'
BUILD_TAG: $CIRCLE_BUILD_NUM-$SHORT_HASH
I looked at the circleci docs, but am not finding anything like this. https://circleci.com/docs/environment-variables
The code is executed in a shell, so you'll want to use backticks or the $() method around the phrase you want to evaluate. This this:
SHORT_HASH: $(echo $CIRCLE_SHA1 | cut -c -7)
One way of doing it is to append export statement to $BASH_ENV
Here is an example:
version: 2
jobs:
build:
docker:
- image: buildpack-deps:jessie
working_directory: ~/project
steps:
- checkout
- run: |
bar_var="foo-bar"
echo 'export FOO_ENV_VAR="${bar_var}"' >> $BASH_ENV
- run:
command: |
echo $FOO_ENV_VAR

Resources