I'm trying to make a playbook to install halyard on a remote host using ansible-playbook. One of the tasks is to execute the "InstallHalyard.sh" wrapper scripts to the actual installer (reference: https://www.spinnaker.io/setup/install/halyard/).
No error / failure present, but also nothing has changed, as if the InstallHalyard.sh script is not executed. Though, it's fine if I do it by typing manual command. The other similar task works perfectly.
You can see the InstallHalyard.sh script implementation: here
Any idea about what is happening?
Here is the task in my playbook:
name: Run InstallHalyard.sh
become: yes
become_user: sudo
shell: "{{ ansible_env.HOME }}/.spinnaker/InstallHalyard.sh"
Any help would be appreciated, thank you very much :)
EDIT:
Already tried using script, command, and shell module.
FYI, the InstallHalyard.sh script will call itself by passing an env variable and needs to do curl -O
I'm suspecting the "export" operation inside the script doesn't work as Ansible has a different understandings for environment vars. (For example, ansible will not recognize "$HOME" instead it uses "{{ ansible_env.HOME }}".)
EDIT:
I found the script does an operation that makes a fork, does ansible handle this kind of operation?
Tested on localhost, got the same result.
SOLUTION
It is because of the different interpretation of environment vars of ansible. If I executed the script manually, the wrapper sets the environment variable and pass it to the next script calls, which ansible not able to do. So what I do is setting the environment var manually with ansible module before executing the script (just adding 2 lines)
Here is my revised task:
name: Run InstallHalyard.sh
become: yes
become_user: sudo
environment:
HAL_USER: "{{ ansible_env.USER }}"
shell: "{{ ansible_env.HOME }}/.spinnaker/InstallHalyard.sh"
The question is really how do I debug the output of a failing command. I'm guessing the command is failing with a useful error message but that message is not being displayed anywhere.
Here is an example to help debug this failing script:
-name: Run InstallHalyard.sh
become: yes
become_user: sudo
shell: "{{ ansible_env.HOME }}/.spinnaker/InstallHalyard.sh"
register: output
- debug: msg="{{ output.stdout }}"
- debug: msg="{{ output.stderr }}"
Try that is see what error message it was giving you.
Related
I've got some trouble defining a job. I'm in an early stage of defining my pipeline and I'm trying to solve my problems with variables :). I've got a few subfolder under in gcp/live/development and I'm trying to cd them and then run terraform stuff:
.plan-development:
variables:
ENVIRONMENT: "development"
TERRAFORM_DEVELOPMENT_PATH: "gcp/live/development"
TF_ROOT: ${TERRAFORM_DEVELOPMENT_PATH}/${CI_JOB_NAME%-plan-development}
stage: plan-development
script:
- cd ${TF_ROOT}
- gitlab-terraform plan
- gitlab-terraform plan-json
artifacts:
name: plan-${ENVIRONMENT}-${CI_JOB_NAME%-plan-development}
paths:
- ${TF_ROOT}/plan.cache
reports:
terraform: ${TF_ROOT}/plan.json
But according to my failing pipeline, the first script part cd ${TF_ROOT} doesn't enter the expected folder at gcp/live/development/cloud-nat.
I've got some debug outout, which tells me following:
$ cd ${TF_ROOT}
$ pwd && ls
/builds/b79ya_1j/0/devops/terraform/gcp/live/development
cloud-nat
firewall
gke
vpc
$ echo "${CI_JOB_NAME%-plan-development}"
cloud-nat
Am I missing something combing two variables (TF_ROOT: ${TERRAFORM_DEVELOPMENT_PATH}/${CI_JOB_NAME%-plan-development})?
Hope you can help me out.
Am I missing something combing two variables (TF_ROOT:
${TERRAFORM_DEVELOPMENT_PATH}/${CI_JOB_NAME%-plan-development})?
The issue is that Gitlab in variables section, doesn't perform bash operations, like you attempt here
${CI_JOB_NAME%-plan-development}
Gitlab literally searches for the variable named CI_JOB_NAME%-plan-development
That's why when it creates the TF_ROOT variable its value is gcp/live/development/ since it could not find the variable named CI_JOB_NAME%-plan-development
I am using Ansible to automate the installation, configuration and deployment of an application server which uses JBOSS, therefore I need to use the in-built jboss-cli to deploy packages.
This Ansible task is literally the last stage to run, it simply needs to check if a deployment already exists, if it does, undeploy it and redeploy it (to be idempotent).
Running the below commands manually on the server and checking the return code after each command works as expected, something, somewhere in Ansible refuses to read the return codes correctly!
# BLAZE RMA DEPLOYMENT
- name: Check if Blaze RMA has been assigned to dm-server-group from a previous Ansible run
shell: "./jboss-cli.sh --connect '/server-group=dm-server-group/deployment={{ blaze_deployment_version }}:read-resource()' | grep -q success"
args:
chdir: "{{ jboss_home_dir }}/bin"
register: blaze_deployment_status
failed_when: blaze_deployment_status.rc == 2
tags: # We always need to check this as the output determines whether or not we need to undeploy an existing deployment.
- skip_ansible_lint
- name: Undeploy Blaze RMA if it has already been assigned to dm-server-group from a previous Ansible run
command: "./jboss-cli.sh --connect 'undeploy {{ blaze_deployment_version }} --all-relevant-server-groups'"
args:
chdir: "{{ jboss_home_dir }}/bin"
when: blaze_deployment_status.rc == 0
register: blaze_undeployment_status
- name: Deploy Blaze RMA once successfully undeployed
command: "./jboss-cli.sh --connect 'deploy {{ jboss_deployment_dir }}/{{ blaze_deployment_version }} --server-groups=dm-server-group'"
args:
chdir: "{{ jboss_home_dir }}/bin"
when: blaze_undeployment_status.rc == 0 or blaze_deployment_status.rc == 1
Any advice would be appreciated.
Your second command contains a when clause. If it is skipped, ansible still registers the variable but there is no rc attribute in the data.
You need to take this into consideration when using the var in the next task. The following condition on last task should fix your issue.
when: blaze_undeployment_status.rc | default('') == 0 or blaze_deployment_status.rc == 1
Also there is maybe the same as author situation, when you run ansible --check
Using Ansible I would like to be able to write the sysout of a task running a command to a local(i.e. on the managed server) log file.
For the moment I can only do this using a task like this:
- name: Run my command
shell: <command> <arg1> <arg3> ... |tee -a <local log file>
The reason to do this is that the takes a long time to complete(i.e. we cannot wait until it finishes to get its output) and would like to collect the output during its execution.
Is there any "Ansible" way to redirect to sysout of the command to a local log file during its execution without using the tee pipe?
Not 100% answers your question as you wont get a constantly updating file in your manager server but you could use async commands
# Requires ansible 1.8+
- name: 'YUM - async task'
yum:
name: docker-io
state: installed
async: 1000
poll: 0
register: yum_sleeper
- name: 'YUM - check on async task'
async_status:
jid: "{{ yum_sleeper.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: 30
and then dump the contents of yum_sleeper to a file with
- name: save log locally
copy:
content: '{{ yum_sleeper.stdout }}'
dest: file.log
delegate_to: localhost
Am running ansible play-book but getting below error, -using ansible 2.7.6, ubuntu 16.04.
in playbook am mentioned
(<unknown>): did not find expected key while parsing a block mapping at line 6 column 3
I tried without become-yes,ubuntu,sudo that also getting the same issue and ansible saying:
The offending line appears to be:
- name: build npm
^ here
- hosts: all
vars:
app_dir: /home/ubuntu/app/backend-app-name
tasks:
- name: build npm
command: "chdir={{ app_dir }} {{ item }}"
with_items:
- /usr/bin/npm run build
become: yes
become_user: ubuntu
become_method: sudo
Indentation is wrong. Correct syntax is
tasks:
- name: build npm
command: ...
with_items:
- /usr/bin/npm run build
become: yes
become_user: ubuntu
become_method: sudo
I received this same error when there was an extra single-quote in the YAML task.
While parsing a block mapping, did not find expected key.
- task: DotNetCoreCLI#2
inputs:
command: 'pack'
packagesToPack: '**/DoesNotMatter/OL.csproj'
#...
versionEnvVar: 'PACKAGEVERSION''
See last (extra ') character of code sample.
Removed Trailing Whitespace
I had a similar issue when rubocop parsed the yaml file.
› ruby_koans (mark) rubocop --auto-gen-config
(.rubocop.yml): did not find expected key while parsing a block mapping at line 1 column 1
Removed trailing white space. (in VSCode using "Trim Trailing Whitespace" setting.
› ruby_koans (mark) rubocop --auto-gen-config
Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`.
Phase 1 of 2: run Layout/LineLength cop
Inspecting 42 files
I've come across a problem with Anisble hanging when trying to start a forever process on an Ansible node. I have a very simple API server I'm creating in vagrant and provisioning with Ansible like so:
---
- hosts: all
sudo: yes
roles:
- Stouts.nodejs
- Stouts.mongodb
tasks:
- name: Install Make Dependencies
apt: name={{ item }} state=present
with_items:
- gcc
- make
- build-essential
- name: Run NPM Update
shell: /usr/bin/npm update
- name: Create MongoDB Database Folder
shell: /bin/mkdir -p /data/db
notify:
- mongodb restart
- name: Generate Dummy Data
command: /usr/bin/node /vagrant/dataGen.js
- name: "Install forever (to run Node.js app)."
npm: name=forever global=yes state=latest
- name: "Check list of Node.js apps running."
command: /usr/bin/forever list
register: forever_list
changed_when: false
- name: "Start example Node.js app."
command: /usr/bin/forever start /vagrant/server.js
when: "forever_list.stdout.find('/vagrant/server.js') == -1"
But even though Ansible acts like everything is fine, no forever process is started. When I change a few lines to remove the when: statement and force it to run, Ansible just hands, possibly running the forever process (forever, I presume) but not launching the VM to where I can interact with it.
I've referenced essentially two points online; the only sources I can find.
as stated in the comments the variable content needs to be included in your question for anyone to provide a correct answer but to overcome this I suggest you can do it like so:
- name: "Check list of Node.js apps running."
command: /usr/bin/forever list|grep '/vagrant/server.js'|wc -l
register: forever_list
changed_when: false
- name: "Start example Node.js app."
command: /usr/bin/forever start /vagrant/server.js
when: forever_list.stdout == "0"
which should prevent ansible from starting the JS app if it's already running.