I’ve been developing Ansible playbooks to help manage our Ubuntu servers. I wanted to install a list of package on a server using Ansible:
The old syntax:
- name: Install list of packages action: apt pkg={{item}} state=installed with_items: - package1 - package2 - package3 - etc
The new syntax:
- name: Install list of packages apt: name={{item}} state=installed with_items: - package1 - package2 - package3 - etc
The syntax has changed, so now the recommended way to call a variable is:
action: apt pkg={{item}} state=installed
Yes. Good point. I’ve updated the post.
The syntax has probably changed again, and the recommended way is:
apt: pkg={{ item }} state=installed
(instead of action:)
In fact, I’m wrong, it’s
apt: name={{ item }} state=installed
Great. Thanks for your feedback. I’ll update the post.
Hi,
You made a mistake here:
apt: apt pkg={{item}} state=installed
It is just:
apt: name={{item}} state=installed
Thanks. I’ll correct that 🙂
You didn’t correct it yet.
Thanks for the reminder!
?
There’s a slight change as “installed” is now “present” according to latest Ansible docs:
http://docs.ansible.com/ansible/apt_module.html
– name: Install list of packages
apt: name={{item}} state=present
with_items:
– package1
– package2
– package3
– etc
Great. Thanks for letting me know. I will update the examples.
Why haven’t you updated this yet? Google searched point to your site and this is misleading. You said you’d update it more than 2 years ago.
Loop with_items is not really necessary:
– name: Install list of packages
apt:
name:
– package1
– package2
– package3
Example of this syntax – ‘|’ denotes the left margin, to show spacing
|- name: Install virtualbox dependencies
| apt:
| name:
| – dkms
| – build-essential
| – linux-headers-generic
| state: present
I’m really confused how you made this work with 2 ‘name’ fields in a single action. Especially when the method in the original post is the one in the Ansible docs.
the – name: is just naming the “play” or action that ansible will take.
|- name: Install virtualbox dependencies # you can name this whatever you want
| apt: # the ansible module to run
| name: # a required definition of the apt module
| – dkms #
| – build-essential # it can take list like that
| – linux-headers-generic #
| state: present # the state of the packages
Now, lots of modules support lists as an input for name. Do you don’t need to create subtasks manually via with_* for those anymore.
I try to do:
—
– hosts: php
sudo: yes
tasks:
– name: install packages
apt: name={{ item }} update_cache=yes state=latest
with_items:
– nginx
– php5-fpm
But i have a problem:
ERROR! Syntax Error while loading YAML.
The error appears to have been in ‘/home/ansible-apache/php.yml’: line 7, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
– name: install packages
apt: name={{item}} update_cache=yes state=latest
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
– {{ foo }}
Should be written as:
with_items:
– “{{ foo }}”
am getting a similar error and can you please share the solution if you found please
You must indent ‘apt’ so that it starts below ‘name’ not below ‘- name’
Example:
– name: apt install initial packages
become: yes
apt:
name:
– findutils
– fortune
– pwgen
– sudo
– vim
update_cache: yes
Sorry trailing blanks are removed.
|- name: apt install initial packages
| become: yes
| apt:
| name:
| – findutils
| – fortune
| – pwgen
| – sudo
| – vim
| update_cache: yes
Hi ALL,
I want to check multiple rpms and install if not present. (rhel6)
vars:
– is_gfs2: rpm -q gfs2-utils
– is_lvm2: rpm -q lvm2-cluster
– is_unzip: rpm -q unzip
– gfs2_rpm: gfs2-utils
– lvm2_rpm: lvm2-cluster
tasks:
### UNZIP RPM Check ####
– name: Check if unzip.rpm is installed
# command: “{{ is_unzip }}”
raw: “{{ item.pkg }}”
with_items:
– { pkg: “{{ is_gfs2 }}” }
– { pkg: “{{is_lvm2 }}” }
ignore_errors: True
register: rpm_check
– name: Installing Unzip
# yum: name= “{{ unzip }}” state=latest
yum: name= “{{ item.pkg }}” state=latest
with_items:
– { pkg: “{{ gfs2_rpm }} ” }
– { pkg: “{{ lvm2_rpm }} ” }
when: rpm_check.stdout.find(‘is not installed’) != -1
register: yum_result
changed_when: yum_result.rc == 0 or yum_result == 1 or yum_result.rc == 2
failed_when: yum_result.rc != 0 and yum_result != 1 and yum_result.rc != 2
Getting below error:
TASK: [Installing Unzip] ******************************************************
fatal: [xxxxxxx] => error while evaluating conditional: rpm_check.stdout.find(‘is not installed’) != -1
fatal: [yyyyyyy] => error while evaluating conditional: rpm_check.stdout.find(‘is not installed’) != -1
fatal: [zzzzzzz] => error while evaluating conditional: rpm_check.stdout.find(‘is not installed’) != -1
FATAL: all hosts have already failed — aborting
This code works in my version of ubuntu/ansible thanks!
Great. That’s good to know.
minor update… “with_items” is now “loop”
Create a playbook and install below softwares in ubuntu …Please help me tried all above options and not working
apache2
sqlite3
git