How to auto accept terms while installing packages with Ansible? - ansible

While installing pkgs Ansible fails, because there is a need to accept licensing terms.
How to auto accept terms through ansible-playbook?
---
- hosts: client1
remote_user: ansible
become: True
tasks:
- name: testing
apt_repository: repo=ppa:webupd8team/java state=present
- name: updating
apt: update_cache=yes
- name: installaing oracle pkg
apt: pkg=oracle-java8-installer state=present update_cache=yes

There is no universal method for "packages".
For Oracle Java add a task before calling apt:
- debconf:
name: oracle-java8-installer
question: shared/accepted-oracle-license-v1-1
value: true
vtype: select

For virtualbox-ext-pack
- debconf:
name: virtualbox-ext-pack
question: virtualbox-ext-pack/license
value: "true"
vtype: select
before apt install command.

Related

How do I correct ansible playbook syntax error? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am creating an ansible playbook in centos7. I get the syntax error every-time I adjust to the syntax error (I even checked spacing and still getting same error):
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: No JSON object could be decoded
Syntax Error while loading YAML.
could not find expected ':'
The error appears to be in '/etc/ansible/playbook.yml': line 121, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Install MySQL repo
^ here
hosts: local host
connection: local
vars: digital_ocean_token: ****
droplets:
- wordpress1
- wordpress2
tasks:
- name: Create SSH Key
user:
name: "{{ansible_user_id}}"
generate_ssh_key_type: rsa
ssh_key_type 4096
ssh_key_file .ssh/id_rsa
- name: Create Digital Ocean Key
community.digitalocean.digital_ocean:
state: present
command: droplet
name: "" "{{ item }}"
unique_name: yes
size_id: s-1vcpu-1gb
region_id: nyc
image_id: centos-7-x64
ssh_key_ids: "{{ my_ssh.ssh_key.id }}"
api_token: "{{ digital_ocean_token }}"
with_items: "{{droplets}}"
register: droplet_details
- name: Add doplets to /etc/ansible/hosts
add_host:
path: /etc/ansible/hosts
groups: droplets
name: "{{ item.droplet.ip_address }}"
with_items:: "{{ droplet_details.results }}"
- pause:
seconds: 45
- hosts: droplets
tasks:
- name: Disable SeLinux
selinux:
state: disabled
#Epel-Release
- name: Install epel-release
yum:
name: epel-release
state: present
- name: update
yum:
name: "*"
state: present
#Install Apache
- name: Install Apache
yum:
name: httpd
state: latest
- name: enable service to start on boot up
service:
name: httpd
state: started
#Install MariaDB
- name: Install MariaDB
yum:
name: mariadb
state: latest
- name: Install MariaDB Server
yum:
name: mariadb-server
state: latest
- name: Install Python2
yum:
name: python2
state: latest
- name: Install Python3
yum:
name: python3
state: latest
- name: Install pip
pip:
name: pip
extra_args: --upgrade
executable: pip3
#Install PHP
- name: Install Remi Repository
yum:
name: https://rpms.remirepo.net/enterprise/remi-release-7.rpm
state: present
- name: Install PHP
yum:
enablerepo: "remiremi-php80"
name:
- php
- php-common
- php-cli
- php-gd
- php-curl
- php-mysqlnd
- php-fpm
- php-mysqli
- php-json
state:latest
- name: Install MySQL repo
yum:
name: http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
state: present
- name: Install PyMySQL
yum:
pkg: ['mysql-community-server', 'mysql-community-client', 'MySQL-python']
# Start MySQL
- name: Start MySQL
service:
name: mysqld
state: started
enabled: yes
# Create MariaDB Database
- name: MySQL login
mysql_user:
user: ***
login_user: ***
login_password: "****"
state: present
- name: Create MariaDB username & password
mysql_user:
login_user: ****
login_password: "****!"
name: wordpressuser
password: ****
priv: "*.*;ALL,GRANT"
host: "localhost"
state: present
- name: Vreate MariaDB Database
mysql_db:
login_user: ***
login_password: "****"
state: present
- name: Restart MariaDB Database
service:
name: mysqld
state: restarted
# Install Wordpress
- name: Download Wordpress
get_url:
url=http://wordpress.org/latest.tar.gz
dest=/tmp/wordpress.latest.tar.gz
validate_certs=no
- name: unzip Wordpress
unarchive:
src=/tmp/wordpress.latest.tar.gz
dest=/var/www
copy=no
- name: Copy sample config file
command: mv /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php
- name: Update Wordpress config file
lineinfile:
path: /var/www/wordpress/wp-config.php
regexp: "{{item.regexp}}"
line: "{{item.line}}"
with_items:
- {'regexp': "define\\( 'DB_NAME', '(.)+' \\);", 'line': "define( 'DB_NAME'', 'wordpress');"}
- {'regexp': "define\\( 'DB_USER', '(.)+' \\);", 'line': "define( 'DB_USER', 'wordpressuser' );"}
- {'regexp': "define\\( 'DB_PASSWORD', '(.)+' \\);", 'line': "define( 'DB_PASSWORD', '***' );"}
- name: Give Ownership to Apache user
file:
path: /var/www/wordpress
state" directory
recurse: yes
owner: apache
group: apache
- name: Set correct permissions on Wordpress directories
command: find /var/www/wordpress/ -type d -exec chomd 755 {} \;
- name: Set correct permissions for Wordpress files
command: find /var/www/wordpress/ -type f -exec chomd 755 {} \;
- name: Restart Apache
service:
name: httpd
state: restarted
It looks from what you provided that you don't have a valid yaml format playbook.
You can do quick syntax checks using:
ansible-playbook playbook.yaml --syntax-check
The plays need to be one or more elements of a yaml list, under tasks.
I'd expect your playbook to look something like:
---
# YAML documents begin with the document separator ---
- hosts: hosta,hostb ...etc
tasks:
- name: install MySQL repo
yum:
name: http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
state: present
# Three dots indicate the end of a YAML document
...
There are other sections you might have in a playbook, but I've omitted those. The sections like vars, handlers or roles, would be list items at the same indention level as the hosts: and tasks: if you were to need those.
It looks like your issue is here:
name: Install PHP
yum:
enablerepo: "remiremi-php80"
name:
- php
- php-common
- php-cli
- php-gd
- php-curl
- php-mysqlnd
- php-fpm
- php-mysqli
- php-json
state: latest
Notice that state: has to be indented exactly at the same level as enablerepo and name. Make sure you have a space there between state: and latest.

How to fix the ansible playbook error "Unsupported parameters for (systemd) module: enable Supported parameters?

I am trying to install Apache 2, PHP on Ubuntu machine using the ansible-playbook.
I am getting the following error
Error after executing playbook
fatal: [18.220.215.181]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (systemd) module: enable Supported parameters include: daemon_reexec, daemon_reload, enabled, force, masked, name, no_block, scope, state, user"}The ansible playbook is as follows---
- hosts: all
become: yes
tasks:
- name: Chenking ping
ping:
- name: Update packages
apt:
name: apache2
update_cache: yes
state: present
- name: restart apache2 server
service:
name: apache2
enable: yes
state: restarted
- name: install php module
apt:
name: "{{ item }}"
state: present
with_items:
- php
- libapache2-mod-php5
- php-mcrypt
- php-mysql
- name: restart apache2 afetr restart
service:
name: apache2
enable: yes
state: restarted
`
The right parameter is enabled (not enable) in your service tasks.
- name: restart apache2 afetr restart
service:
name: apache2
enabled: yes
state: restarted
Change "enabled" task containing service module.
- hosts: all
become: yes
tasks:
- name: Chenking ping
ping:
- name: Update packages
apt:
name: apache2
update_cache: yes
state: present
- name: restart apache2 server
service:
name: apache2
enabled: yes
state: restarted
- name: install php module
apt:
name: "{{ item }}"
state: present
with_items:
- php
- libapache2-mod-php5
- php-mcrypt
- php-mysql
I think last task is not required.

Apply module_defaults attribute to a role

I have the following file structure:
webserver.yml
emailserver.yml
roles/
common/
tasks/
main.yml
emailserver.yml and webserver.yml look like:
- hosts: all
roles:
- common
And main.yml:
- name: apt-get update
apt:
update_cache: yes
force_apt_get: yes
- name: apt-get upgrade
apt:
name: "*"
state: latest
force_apt_get: yes
In order to avoid repeating force_apt_get: yes, I would like to use the module_defaults attribute for this role:
module_defaults:
apt:
force_apt_get: yes
I've tried to include that at the top of main.yml but I'm getting a syntax error.
How can I apply a module_defaults attribute to a role's tasks?
(an alternative is to add module_defaults to both webserver.yml and emailserver.yml, but it means code duplication and having the common role configuration spread over a number of files).
One solution is to use a block in the role task file, main.yml:
- block:
- name: apt-get update
apt:
update_cache: yes
- name: apt-get upgrade
apt:
name: "*"
state: latest
become: yes
module_defaults:
apt:
force_apt_get: yes
How can I apply a module_defaults attribute to a role?
It is not possible as of current Ansible version (2.7). module_defaults is a Play attribute only.

YAML syntax error (Ansible playboook)

I am trying to write a playbook that installs Apache, but I get the below error:
The offending line appears to be:
tasks:
- name: command to install apache
^ here
Here is my YAML code:
---
- hosts: all
tasks:
- name: command to install apache
sudo: yes
yum: name=httpd state=latest
service: name=httpd state=running
What could be wrong here?
You cannot add two actions (modules) to a single task in Ansible.
You need to split yum and service into two tasks.
Also sudo declaration was deprecated long time ago and now become should be used:
---
- hosts: all
tasks:
- name: Ensure apache is installed
become: yes
yum: name=httpd state=latest
- name: Ensure httpd service is running
become: yes
service: name=httpd state=running

How to do best organization of install and delete in the same ansible playbook?

I try to figure how I must to construct an ansible playbooks where I can do some action and undo them (I can install or remove same packages; place file or remove this file).
If I create two ansible playbooks: delete.yml and install.yml. There are may be problem's like:
I added to installation someting, but don't change deletion
Example:
install.yml:
---
- name: Add x2go repository
apt_repository: repo='deb http://ppa.launchpad.net/x2go/stable/ubuntu precise main' state=present
apt_repository: repo='deb-src http://ppa.launchpad.net/x2go/stable/ubuntu precise main' state=present
when: ansible_os_family == "Debian"
tags:
- remote-access-x2go
- name: Install x2go application
apt: name=x2goserver update_cache=yes state=present
apt: name=x2goserver-xsession update_cache=no state=present
when: ansible_os_family == "Debian"
tags:
- remote-access-x2go
delete.yml:
---
- name: Add x2go repository
apt_repository: repo='deb http://ppa.launchpad.net/x2go/stable/ubuntu precise main' state=absent
apt_repository: repo='deb-src http://ppa.launchpad.net/x2go/stable/ubuntu precise main' state=present
when: ansible_os_family == "Debian"
tags:
- remote-access-x2go
- name: Install x2go application
apt: name=x2goserver update_cache=yes state=absent
apt: name=x2goserver-xsession update_cache=no state=absent
when: ansible_os_family == "Debian"
tags:
- remote-access-x2go
This is a very interesting idea. I have personally never tried the 'undoing' workflow, but I can see the nice things about this idea and would like to use it sometime. Here is what I would do.
In my ansible-role/defaults/main.yml I would define a variable flag
# defaults file for ansible-role
flag_undo: false
In my ansible-role/tasks/main.yml I would have
- name: task foo bar
command: falana dhimaka
- name: undoing task foo bar
command: undo falana dhimaka
when: flag_undo=true
So by default our flag is always false. So when installing things I would us the first command below to run my plays. And to uninstall I would use the second command.
ansible-playbook foo-play.yml
ansible-playbook foo-play.yml --extra-vars "flag_undo=true"
One approach that I use in some cases is to simply have lists of packages that you want installed and lists you want removed, then iterate over each list. I use this basic method not only for packages but other things as well, like users, groups, etc. For example, I have a "packages" role that has the following files in it:
vars/main.yml:
---
installed_system_packages:
- telnet
- screen
- postfix
latest_system_packages:
- glibc
removed_packages:
- sendmail
tasks/main.yml:
---
- name: Install system packages (latest)
yum: pkg={{ item }} state=latest
with_items: latest_system_packages
- name: Install system packages
action: yum pkg={{ item }} state=installed
with_items: installed_system_packages
- name: Remove unwanted packages
action: yum pkg={{ item }} state=removed
with_items: removed_packages
This way, if I decide that I no longer want a package like telnet installed I can just move it from installed_system_packages to removed_packages. Or if I want to ensure I'm running the latest version of screen I would simply move it to the latest_system_packages list. Then it's just a matter of re-running the role to have the changes applied.

Resources