This is the puppet code managing a service:
class fred::service {
service { 'bob':
enable => true,
ensure => 'running',
require => Package['bob-5.4']
}
}
My translation in Ansible role
---
- name: check bob
service:
name: bob
enabled: true
state: running
package:
name: bob-5.4
state: present
My question is:
Is the translation correct?
I am told package comes before service?
Do I require package:
The order is incorrect. You should first attempt to install the package. It will automatically skip this (resulting in ok) if the package is already present.
When checking if the service is running, state: running is not valid in Ansible, it should be state: started.
- name: Install package
apt:
name: bob-5.4
state: present
- name: Check if service is running
service:
name: bob
state: started
enabled: yes
Depending on what you install the package with, this may require a little modification (package or yum instead of apt for example).
Reference for apt in Ansible
Reference for package in Ansible
Reference for yum in Ansible
Related
I have the following script that attempts to install a package on a node only when not already installed.
- name: check if linux-modules-extra-raspi is installed # Task 1
package:
name: linux-modules-extra-raspi
state: present
check_mode: true
register: check_raspi_module
- name: install linux-modules-extra-raspi if not installed # Task 2
shell: |
sudo dpkg --configure -a
apt install linux-modules-extra-raspi
when: not check_raspi_module.changed
But the problem here is that if I have a set of hosts, the Task 1 runs for node n1 and registeres check_raspi_module to false and then Task 1 runs for node n2 and then sets it to true because that package is already available in node n2. So how can I throttle this and have the check_raspi_module local to a task and not global like it is now?
If you need to install package, you have just to use the first bloc like below. You haven't need to use block of check and install separatly.
Even if your package is installed, Ansible will detect it and not reinstall it. It’s the principe of Ansible
The documentation: here
(definition) state: present (mean install if not present)
- name: install if not present if linux-modules-extra-raspi
ansible.builtin.package:
name: linux-modules-extra-raspi
state: present
Hi i am new to ansible i have to deploy nodejs12.8.4, SSL and ngnix latest to Ubuntu 20.04 server can someone guide me how to do it thank you.
this is my yml file:
hosts: all
become: true
tasks:
- name: install nodejs prerequisites
apt:
name:
- apt-transport-https
- gcc
- g++
- make
state: present
- name: add nodejs apt key
apt_key:
url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
state: present
- name: add nodejs repository
apt_repository:
repo: deb https://deb.nodesource.com/node_12.x {{ ansible_lsb.codename }} main
state: present
update_cache: yes
- name: install nodejs
apt:
name: nodejs
state: present
it install nodejs 12 now i want to install nginx in same file how i add new task.
Try Ansible NGINX Role. See details at Github.
Q: "I want to install Nginx in the same file how I add a new task?"
A: Include the role
- include_role:
name: nginx
Download the role. See Roles and Using Roles in particular.
I've been trying to install NewRelic agent for PHP on Amazon Linux 2 the "ansible way", but I cannot get it to work with either rpm_key or yum_repository. I've also tried just copying the repo file to /etc/yum.repos.d/newrelic.repo, but it's supposed to use a GPG key and the only one I found is 548C16BF.gpg and at that point I felt this was getting to hacky.
My current setup is:
- name: add the new relic repository
# noqa 303
command: rpm -Uvh http://yum.newrelic.com/pub/newrelic/el5/x86_64/newrelic-repo-5-3.noarch.rpm
but that doesn't sit well with ansible-lint (hence the rule exception).
Am I missing something here or maybe my preconception of what the "ansible-way" would be is incorrect. Asking for a friend (with a lot of Ansible experience).
To add the GPG key:
- name: Adding RPM key
rpm_key:
state: present
key: https://download.newrelic.com/548C16BF.gpg
and Add the repository:
- name: Add repository
yum_repository:
name: rewrelic
description: Newrelic YUM repo
baseurl: http://yum.newrelic.com/pub/newrelic/el5/x86_64/newrelic-repo-5-3.noarch.rpm
Finally install the yum:
- name: install Rewrelic
yum:
name: rewrelic
state: present
I got some trouble with automating an installation using ansible.
I use this role (https://github.com/elastic/ansible-elasticsearch) to install elasticsearch on my ubuntu 16.04 server.
The role depends on the package python-jmespath, as mentioned in the documentation.
The role DOES NOT install the package itsself, so i try to install it before role execution.
- hosts: elasticsearch_master_servers
become: yes
tasks:
- name: preinstall jmespath
command: "apt-get install python-jmespath"
- name: Run the equivalent of "apt-get update" as a separate step
apt:
update_cache: yes
- hosts: elasticsearch_master_servers
become: yes
roles:
- role: elastic.elasticsearch
vars:
...
When running the playbook i expect the python-jmespath package to be installed before execuction of role takes place, but role execution fails with
You need to install \"jmespath\" prior to running json_query filter"
When i check if the package is installed manually using dpkg -s python-jmespath i can see the package is installed correctly.
A second run of the playbook (with the package already installed) doesnt fail.
Do I miss an ansible configuration, that updates the list of installed packages during the playbook run ?
Am I doing something wrong in general ?
Thanks in advance
FWIW. It's possible to tag installation tasks and install the packages in the first step. For example
- name: install packages
package:
name: "{{ item.name }}"
state: "{{ item.state|default('present') }}"
state: present
loop: "{{ packages_needed_by_this_role }}"
tags: manage_packages
Install packages first
shell> ansible_playbook my-playbook.yml -t manage_packages
and then run the playbook
shell> ansible_playbook my-playbook.yml
Notes
This approach makes checking of the playbooks with "--check" much easier.
Checking idempotency is also easier.
With tags: [manage_packages, never] the package task will be skipped when not explicitly selected. This will speed up the playbook.
I'm trying to install kubelet with Ansible, but I'm struggling to do it.
This is my playbook:
---
- hosts: all
become: yes
tasks:
- name: install docker
yum:
name: docker
state: present
update_cache: true
- name: add repo
yum_repository:
name: kuberepo
description: kubernetes-repo
baseurl: "https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64"
enabled: yes
gpgcheck: yes
repo_gpgcheck: yes
gpgkey:
- "https://packages.cloud.google.com/yum/doc/yum-key.gpg"
- "https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg"
- name: setenforce
shell: setenforce 0
- name: install kubelet
yum:
name: kubelet
state: present
update_cache: true
This is the error when i run the playbook (ansible-playbook -i hosts kube-dependencies.yml)
The "ok" from [master] node is because I manually executed "sudo yum install kubelet" on that machine, and it worked.
Conclusions:
So basically, manually executing the yum install command it works, but I can't make it work with ansible.
Any clues? Thanks in advance.
I had the same issue, I fix it adding version of kubelet
apt:
name: kubelet=1.20.0-00
state: present
update_cache: true
That worked for me
same issue with kubeletadm
- name: install kubeadm
apt:
name: kubeadm=1.14.0-00
state: present
Error
solution
- name: install kubeadm
apt:
name: kubeadm=1.20.0-00
state: present
Fix the problem
I hope I've helped
The best solution is to upgrade the version to install.
in every new version k8s changed many configs so some components become npt compatible (kubelet vs kubernetes-cni (= 0.7.5) are not compatible.
version up, if not works then find another solution.
hope that this clarification help.