Ansible task for run Docker container from private registry - ansible

I have a private Docker registry runnning in 172.20.20.20 in port 5000. And I have pushed the image pruebasjeje to it.
Now, I need to pull and run the image using Ansible task:
- name: run container
docker:
registry: 172.20.20.20:5000
insecure_registry: true
image: pruebasjeje
state: reloaded
pull: always
ports:
- "8080:8080"
But when I execute it, I get this error:
fatal: [localhost]: FAILED! => {"changed": false, "changes":
["{\"status\":\"Pulling repository
docker.io/library/pruebasjeje\"}\r\n",
"{\"errorDetail\":{\"message\":\"Error: image
library/pruebasjeje:latest not found\"},\"error\":\"Error: image
library/pruebasjeje:latest not found\"}\r\n"], "failed": true, "msg":
"Unrecognized status from pull.", "status": ""}
It seems that is looking for the image in docker.io. How should I write my task in order to do what I want?
Thanks.

Not sure if it is how it should be done, but this is how I do it:
- name: run container
docker:
insecure_registry: true
image: 172.20.20.20:5000/pruebasjeje
state: reloaded
pull: always
ports:
- "8080:8080"
I just started with Docker myself. Here are the tasks I have for setting up Docker on my target host (CentOS 7):
- name: Install epel repo
yum:
name: "http://download.fedoraproject.org/pub/epel/{{ ansible_distribution_major_version }}/{{ ansible_userspace_architecture }}{{ '/' if ansible_distribution_major_version < '7' else '/e/' }}epel-release-{{ ansible_distribution_major_version }}-5.noarch.rpm"
state: present
become: yes
- name: Install Docker
yum:
name: "{{ item }}"
state: latest
enablerepo: epel
with_items:
- device-mapper
- docker-engine
- python-pip
become: yes
- name: Install docker-py
pip:
name: docker-py
become: yes
- name: Add registry cert
copy:
src: registry.crt
dest: /etc/docker/certs.d/{{ docker_registry_host }}/ca.crt
become: yes
- name: Allow access to insecure registry
lineinfile:
dest: /usr/lib/systemd/system/docker.service
regexp: ^ExecStart=
line: ExecStart=/usr/bin/docker daemon -H fd:// --insecure-registry {{ docker_registry_host }}:5000
become: yes
- name: Start Docker service
service:
name: docker
state: started
enabled: yes
become: yes

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.

ERROR! 'notify' is not a valid attribute for a Play

I am trying to install LAMP using Ansible-playbook. But I am getting the error[as shown in image]
The code that I had used for playbook is as follows:-
---
- hosts: all
become: yes
tasks:
- name: Install httpd
yum:
name: httpd
state: present
notify:
- restart apache
- name: starting httpd service
service:
name: httpd
enabled: yes
state: started
- name: Installing php packages
yum:
name: "{{ item }}"
state: present
with_items:
- php
- php-mysql
- php-pdo
- php-gd
- php-mbstring
notify:
-restart apache
handlers:
- name: restart apache
service:
name: httpd
state: restarted
Error in image format
Your indenting is incorrect. This should work:
---
- hosts: all
become: yes
tasks:
- name: Install httpd
yum:
name: httpd
state: present
notify:
- restart apache
- name: starting httpd service
service:
name: httpd
enabled: yes
state: started
- name: Installing php packages
yum:
name: "{{ item }}"
state: present
with_items:
- php
- php-mysql
- php-pdo
- php-gd
- php-mbstring
notify:
- restart apache
handlers:
- name: restart apache
service:
name: httpd
state: restarted
Please try as below. I think indention is the problem here.
---
- hosts: all
become: yes
tasks:
- name: Install httpd
yum:
name: httpd
state: present
notify: "restart apache"
The correct solution is as follows:
---
- hosts: all
become: yes
tasks:
- name: Install httpd
yum:
name: httpd
state: present
- name: starting httpd service
service:
name: httpd
enabled: yes
state: started
- name: Installing php packages
yum:
name: "{{ item }}"
state: present
with_items:
- php
- php-mysql
- php-pdo
- php-gd
- php-mbstring
notify:
- restart httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
Solution in image form

How to resolve ansible playbook error installing php "No package php-mysql available"?

I am trying to install Php, Apache in RHEL using the Ansible Playbook. But I am getting following error.
*failed: [18.191.65.251] (item=[u'php', u'php-mysql', u'php-pdo', u'php-gd', u'php-mbstring']) => {"ansible_loop_var": "item",
"changed": false, "failures": ["No package php-mysql available."],
"item": ["php", "php-mysql", "php-pdo", "php-gd", "php-mbstring"],
"msg": ["Failed to install some of the specified packages"], "rc": 1,
"results": []}
Screenshot of error
The code that I have used to write the playbook is as follows
---
- hosts: all
become: yes
tasks:
- name: Install httpd
yum:
name: httpd
state: present
- name: starting httpd service
service:
name: httpd
enabled: yes
state: started
- name: Installing php packages
yum:
name: "{{ item }}"
state: present
with_items:
- php
- php-mysql
- php-pdo
- php-gd
- php-mbstring
- name: restart Apache service
service:
name: httpd
state: restarted
Use Amazon Linux instead of RHEL, rest code will code work.
hosts: all
become: yes
tasks:
name: Install httpd
yum:
name: httpd
state: present
name: starting httpd service
service:
name: httpd
enabled: yes
state: started
name: Installing php packages
yum:
name: "{{ item }}"
state: present
with_items:
php
php-mysql
php-pdo
php-gd
php-mbstring
name: restart Apache service
service:
name: httpd
state: restarted

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.

How can I apply a tag to every command in an Ansible tasks file?

The Ansible best-practices documentation has this example code:
---
# file: roles/common/tasks/main.yml
- name: be sure ntp is installed
yum: name=ntp state=installed
tags: ntp
- name: be sure ntp is configured
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntpd
tags: ntp
- name: be sure ntpd is running and enabled
service: name=ntpd state=running enabled=yes
tags: ntp
I'm looking to avoid duplicating the tags: ntp line. Is it possible for each of these instructions to inherit a tag?
You could work with - block:
➜ ~ cat become.yml
---
- hosts: localhost
user: vagrant
tasks:
- block:
- shell: whoami
register: result
- debug: var=result.stdout
- name: become_root_user
become: true
become_user: root
shell: whoami
register: sudo_test_result
- debug: var=sudo_test_result.stdout
tags:
- block1
- block:
- name: creating_new_app_user
become: true
become_user: root
become_method: sudo
user: name=app_user password=Bzs310di86b6E groups="adm,sudo" system=yes state=present
- name: become_app_user
become: true
become_user: app_user
become_method: sudo
shell: whoami
register: app_user_test_result
- debug: var=app_user_test_result.stdout
tags:
- block2
~ ansible-playbook -i realtime-automation/hosts-slaves become.yml --tags "block1"
In your specific case:
---
- block:
- name: be sure ntp is installed
yum: name=ntp state=installed
- name: be sure ntp is configured
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntpd
- name: be sure ntpd is running and enabled
service: name=ntpd state=running enabled=yes
tags: ntp
Before v2 this could be achieved assigning a tag to an 'include'
Move this task to a different file, say ntp.yml
---
# file: roles/common/tasks/ntp.yml
- name: be sure ntp is installed
yum: name=ntp state=installed
- name: be sure ntp is configured
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntpd
- name: be sure ntpd is running and enabled
service: name=ntpd state=running enabled=yes
And then include it in main.yml
---
# file: roles/common/tasks/main.yml
- include: ntp.yml
tags: ntp

Resources