I used the below code in my playbook to remove the rpm
- name: query rpm
shell: "rpm -qa | grep -i rscd"
args:
warn: false
register: rpmblade
- debug: msg="{{ rpmblade.stdout}}"
- name: remove bladelogic rpm
shell: "rpm -e {{ rpmblade.stdout }}"
when: rpmblade.stdout == "BladeLogic_RSCD_Agent-8.9.04-292.x86_64" or rpmblade.stdout == "BladeLogic_RSCD_Agent-21.02.01-211.x86_64"
but what I required was irrespective of whatever the version as long as I see the "BladeLogic_RSCD_Agent" I want to uninstall that package. Is there a way to do it?
Related
I am having an issue learning Ansible and getting feedback from it.
Here is the code I am running
clear && sudo ansible-playbook patching.yml -u stackoverflow -k --ask-become-pass -v
Here is my patching.yml file
---
- hosts: all
gather_facts: no
become: yes
tasks:
- name: Patching
apt: update_cache=yes force_apt_get=yes
- name: Proof of Patching
shell: apt -q -y --ignore-hold --allow-change-held-packages --allow-unauthenticated -s dist-upgrade | /bin/grep ^Inst | wc -l
register: output
- debug: var=output
What I am trying to accomplish is one to learn Ansible and two to patch my various Linux machines with some sort of feedback that they have patched I am getting this feedback after reading stack overflow for some time. It is long and un-human-friendly I would like it to be just a # or even better hostname: #.
Thank You
-Jared
I want to install MongoDB via Ansible playbook, I am following the instruction in:
https://www.howtoforge.com/tutorial/install-mongodb-on-ubuntu-16.04/
For the step about "Step 2 - Create source list file MongoDB"
I should use:
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
while it get the ubuntu version with the following command:
$(lsb_release -sc)
How can I do it via yml file and run it via ansible palybook?
I used the below yml command but it is not working and gives me error since I use shell command "$(lsb_release -sc)" inside my script
- name: Create source list file MongoDB
sudo: yes
lineinfile: >
line="deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse"
dest=/etc/apt/sources.list.d/mongodb-org-3.2.list
state=present
create=yes
There is apt_repository module in Ansible:
- apt_repository:
repo: deb http://repo.mongodb.org/apt/ubuntu {{ ansible_distribution_release | lower }}/mongodb-org/3.2 multiverse
state: present
You can register the result of one task, including its stdout, as a variable then use it in later tasks:
- name: Work out the distribution
command: lsb_release -sc
register: result
- name: Create source list file MongoDB
sudo: yes
lineinfile: >
line="deb http://repo.mongodb.org/apt/ubuntu {{ result.stdout }}/mongodb-org/3.2 multiverse"
dest=/etc/apt/sources.list.d/mongodb-org-3.2.list
state=present
create=yes
I've been getting my feet wet with Ansible (2.0.0.2) on CentOS 7. I'm trying to obtain a version from an installed rpm/yum package, but ran into a warning message when running the script.
Ansible script:
---
- name: Get version of RPM
shell: yum list installed custom-rpm | grep custom-rpm | awk '{print $2}' | cut -d'-' -f1
register: version
changed_when: False
- name: Update some file with version
lineinfile:
dest: /opt/version.xml
regexp: "<version>"
line: " <version>{{ version.stdout }}</version>"
Running this works fine and does what it's supposed to, but it's returning a warning after it executes:
ok: [default] => {"changed": false, "cmd": "yum list installed custom-rpm | grep custom-rpm | awk '{print $2}' | cut -d'-' -f1", "delta": "0:00:00.255406", "end": "2016-05-17 23:11:54.998838", "rc": 0, "start": "2016-05-17 23:11:54.743432", "stderr": "", "stdout": "3.10.2", "stdout_lines": ["3.10.2"], "warnings": ["Consider using yum module rather than running yum"]}
[WARNING]: Consider using yum module rather than running yum
I looked up information for the yum module on the Ansible site, but I don't really want to install/update/delete anything.
I could simply ignore it or suppress it, but I was curious if there was a better way?
I just want to update this old discussion to point out that there is now a package module that makes this more straightforward
- name: get the rpm or apt package facts
package_facts:
manager: "auto"
- name: show apache2 version
debug: var=ansible_facts.packages.apache2[0].version
I think more native ansible way would be:
- name: get package version
yum:
list: package_name
register: package_name_version
- name: set package version
set_fact:
package_name_version: "{{ package_name_version.results|selectattr('yumstate','equalto','installed')|map(attribute='version')|list|first }}"
It's okay to suppress this warning in your case. Use args like:
---
- name: Get version of RPM
shell: yum list installed custom-rpm | grep custom-rpm | awk '{print $2}' | cut -d'-' -f1
register: version
changed_when: False
args:
warn: no
Equivalent to warn=no on the shell: line but tidier.
I didn't like any of these answers.
- name: use command to pull version
command: '/usr/bin/rpm -qa custom-rpm --queryformat %{VERSION}'
register: version
ignore_warnings: True
changed_when: False
How about you use RPM to retrieve the version directly in stead of going trough various pipes:
rpm -q --qf "%{VERSION}" custom-rpm
The way you do it is perfectly fine. The check which is causing the warning is very simply and just checks the first word against a pre-defined list. It ignores further options and often results in warnings which can not be solved with the corresponding module, like in the yum case.
To get rid of the warning you can simply do a which:
shell: `which yum` list installed custom-rpm | grep custom-rpm | awk '{print $2}' | cut -d'-' -f1
which looks up the the complete path of yum, which then is executed. It's the exact same thing, but from viewpoint of Ansible it calls which, not yum which avoids the warning.
If you want to deactivate this kind of warnings globally you can set command_warnings = False in your ansible.cfg. (See docs)
According to the docs you can also add warn=no at the end of your command but this really looks strange to me as it appears to be part of the command.
use the YUM module as suggested. This really helps. You need not do any installation/update/delete.
More over this gives you more options like if the package is already installed it would just ignore it.
Varaint of peaxol answer setting a fact with the installed package version number for futher tests
- name: Find if custom_rpm is installed
yum:
list: custom_rpm
register: custom_rpm_yum_packages
when: ansible_os_family == "RedHat"
- name: Extract custom_rpm actual installed version
set_fact:
actual_custom_rpm_version: "{{custom_rpm_yum_packages|json_query(jsonquery)}}"
vars:
jsonquery: "results[?yumstate=='installed'].version"
when: ansible_os_family == "RedHat"
- debug:
var: actual_custom_rpm_version
As mentioned by others, you can use the shell command.
I found this answer useful for using the ansible yum module instead of the shell command as recommended by the ansible warning:
How can I get the installed YUM packages with Ansible?
Summarizing it here for easy reference:
You can use yum list from the native yum module in ansible.
- hosts: localhost
tasks:
- name: Get installed packages
sudo: yes
yum: list=installed
register: yum_packages
changed_when: False
To view the contents of yum_packages:
- debug:
var: yum_packages
yum_packages contains a list of all installed packages.
You can then get the version of package of interest by:
- debug: var=item
with_items: "{{yum_packages|json_query(jsonquery)}}"
vars:
jsonquery: "results[?name=='tar'].version"
Please note that jsonquery is only available from ansible 2.2 onwards.
Try adding the ignore_errors
option to handle this. See the Error Handing page for details and examples
I always wonder what is the good way to replace the following shell tasks using the "ansible way" (with get_url, etc.):
- name: Install oh-my-zsh
shell: wget -qO - https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash -
or
- name: Install nodesource repo
shell: curl -sL https://deb.nodesource.com/setup_5.x | bash -
This worked for me:
- name: Download zsh installer
get_url:
url: https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh dest=/tmp/zsh-installer.sh
- name: Execute the zsh-installer.sh
shell: /tmp/zsh-installer.sh
- name: Remove the zsh-installer.sh
file:
path: /tmp/zsh-installer.sh
state: absent
#RaviTezu solution doesn't work because the file/script that you wish to execute must be on the machine where you execute your play/role.
As per the documentation here
The local script at path will be transferred to the remote node and then executed.
So one way to do it is by downloading the file locally and using a task like below:
- name: execute the script.sh
script: /local/path/to/script.sh
Or you can do this:
- name: download setup_5.x file to tmp dir
get_url:
url: https://deb.nodesource.com/setup_5.x
dest: /tmp/
mode: 0755
- name: execute setup_5.x script
shell: setup_5.x
args:
chdir: /tmp/
I would go for the first method if you are uploading your own script, the second method is more useful in your case because the script might gets updated in time so you are sure each time you execute it it uses the latest script.
For me, the following statement worked:
- name: "Execute Script"
shell: curl -sL https://rpm.nodesource.com/setup_6.x | bash -
Consider using the get_url or uri module rather than running curl.
For example:
- name: Download setup_8.x script
get_url: url=https://deb.nodesource.com/setup_8.x dest=/opt mode=755
- name: Setup Node.js
command: /opt/setup_8.x
- name: Install Node.js (JavaScript run-time environment)
apt: name=nodejs state=present
This playbook is what I've come up with. So far, it's as close to an idiomatic solution as I have come, and seems to be idempotent.
It will check for the existence of a command (in this case, starship) and if/when the test fails it will download the script.
---
- name: Check for Starship command
command: command -v starship >/dev/null 2>&1
register: installed
no_log: true
ignore_errors: yes
- name: Download Starship installer
get_url:
url: https://starship.rs/install.sh
dest: /tmp/starship-installer.sh
mode: 'u+rwx'
when: installed.rc != 0
register: download
- name: Run the install script
shell: /tmp/starship-installer.sh
when: download.changed
- name: Remove the starship-installer.sh
file:
path: /tmp/starship-installer.sh
state: absent
May be this basic example can help you to start:
---
- name: Installing Zsh and git
apt: pkg=zsh,git state=latest
register: installation
- name: Backing up existing ~/.zshrc
shell: if [ -f ~/.zshrc ]; then mv ~/.zshrc{,.orig}; fi
when: installation|success
sudo: no
- name: Cloning oh-my-zsh
git:
repo=https://github.com/robbyrussell/oh-my-zsh
dest=~/.oh-my-zsh
when: installation|success
register: cloning
sudo: no
- name: Creating new ~/.zshrc
copy:
src=~/.oh-my-zsh/templates/zshrc.zsh-template
dest=~/.zshrc
when: cloning|success
sudo: no
Note the: "force=yes", which will always download the script, overriding the old one.
Also note the "changed_when", which you can refine per your case.
- name: 'Download {{ helm.install_script_url }}'
environment:
http_proxy: '{{proxy_env.http_proxy | default ("") }}'
https_proxy: '{{proxy_env.https_proxy | default ("") }}'
no_proxy: '{{proxy_env.no_proxy | default ("") }}'
get_url: url={{ helm.install_script_url | default ("") }} dest=/tmp/helm_install_script force=yes mode="0755"
when: helm.install_script_url is defined
tags:
- helm_x
- name: Run {{ helm.install_script_url }}
environment:
http_proxy: '{{proxy_env.http_proxy | default ("") }}'
https_proxy: '{{proxy_env.https_proxy | default ("") }}'
no_proxy: '{{proxy_env.no_proxy | default ("") }}'
command: "/tmp/helm_install_script"
register: command_result
changed_when: "'is up-to-date' not in command_result.stdout"
when: helm.install_script_url is defined
args:
chdir: /tmp/
tags:
- helm_x
I have a package I need to install from a remote URL as in:
- get-url: url=http://foo.com/foo.deb dest=/tmp
- command: dpkg --skip-same-version -i /tmp/foo.deb
- apt: update_cache=yes
- apt: pkg=foo state=present
I'd only like to run the first 3 if pkg=foo isn't already present. What's the best way to achieve this?
You have to register a variable with the result, and then use when statement.
tasks:
- shell: /usr/bin/foo
register: result
ignore_errors: True
- debug: msg="it failed"
when: result|failed