Useful shortcuts for vi editor

Showing posts with label ansible. Show all posts
Showing posts with label ansible. Show all posts

Tuesday, 24 May 2022

'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'stdout'

Action
ansible-playbook -i /etc/ansible/hosts /etc/ansible/example.yml -v

Error
'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'stdout'

Solution
Use the standard output of the variable before setting it as a fact
e.g.
- name: debug
  debug:
   msg: "Hello, {{ my_variable.stdout }}!"

- name: set fact the variable
  set_fact:
    fact_my_variable: "{{ my_variable.stdout }}"

Friday, 20 August 2021

"Attempting to decrypt but no vault secrets found" issue solution

Action
ansible-playbook -i /etc/ansible/hosts /etc/ansible/example.yml -v

Error
Attempting to decrypt but no vault secrets found

Solution
Add "--ask-vault-pass" OR "--vault-password-file <vault-file-path>" option to the end of the playbook command line
e.g.
ansible-playbook -i /etc/ansible/hosts /etc/ansible/example.yml -v --ask-vault-pass

Monday, 15 June 2020

How to resolve "[Errno 13] Permission denied"?

Action
ansible-playbook -i /etc/ansible/hosts /etc/ansible/example.yml -v

Error
ERROR! Unexpected Exception, this is probably a bug: [Errno 13] Permission denied

Analysis
ansible user does not have the owner permissions on "/dev/shm"

Possible Reason
Running "umount -a" or "umount /dev/shm"

Solution
$ sudo chown $USER /dev/shm

Tuesday, 27 August 2019

Ansible Commands Book

-> valid syntax of the playbook
$ ansible-playbook -i myhosts example.yml -v 
--syntax-check

-> show elapsed time of the playbook

$ time ansible-playbook -i myhosts example.yml -v

-> maintain output verbosity (-v, -vv, -vvv, -vvv)
$ time ansible-playbook -i myhosts example.yml -vvvv

-> list tags/tasks of the playbook
$ ansible-playbook -i myhosts example.yml --list-tags
$ ansible-playbook -i myhosts example.yml --list-tasks

-> run the playbook by getting vault password/sudo password from user
$ ansible-playbook -i myhosts example.yml --ask-vault-pass
$ ansible-playbook -i myhosts example.yml --ask-sudo-pass

-> run the playbook with sudo password 
$ ansible-playbook -i myhosts example.yml -e "ansible_sudo_pass=alper"
(-e, --extra-vars)

Thursday, 9 August 2018

How to check available yum packages?

e.g. install/update ansible by yum

-> Go to repo path

# cd /etc/yum.repos.d/

-> Add source paths into repo file to be able to install ansible

# vi epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]

name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch/debug
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

[epel-source]

name=Extra Packages for Enterprise Linux 7 - $basearch - Source
#baseurl=http://download.fedoraproject.org/pub/epel/7/SRPMS
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

-> Search yum package
yum search ansible

-> Check available yum package
# yum list available | grep ansible

-> Check available all yum packages with its duplicates
yum list available --showduplicates | grep ansible

-> Install ansible 
# yum install ansible

-> Download only yum package
yum install ansible --downloadonly --downloaddir=/root

-> Check available yum packages for upgrade
yum check-updates | grep ansible

-> Update ansible

yum update ansible

-> Check yum history
yum history list

How to install/configure/run ansible?

-> Set hostname
# hostnamectl set-hostname ansible_host

-> Add remote hosts into /etc/hosts file
# vi /etc/hosts
10.10.10.2 remote_host_2
10.10.10.3 remote_host_3

-> Generate ssh key without passphase
# ssh-keygen

-> Install ssh key into remote servers
# ssh-copy-id remote_host_2
# ssh-copy-id remote_host_3

-> Install ansible rpm packages
# yum install ansible

-> Add remote hosts as nodes into /etc/ansible/hosts file
# vi /etc/ansible/hosts
[remote_hosts]
remote_host_2
remote_host_3

-> Prepare an ansible playbook in /etc/ansible/
# vi my_very_first_playbook.yml
# example playbook
- name: Run the role for me 
  hosts: remote_hosts
  roles:
  - example-role

-> Prepare a role for the playbook in /etc/ansible/roles/example-role/
# vi main.yml
- name: checking Command Scheduler service
  service: 
   name=crond.service
   state=started

-> Run the playbook in /etc/ansible/
# ansible-playbook my_very_first_playbook.yml
or
# ansible-playbook -i hosts my_very_first_playbook.yml
(i -> inventory, its default location is /etc/ansible/hosts)

-> To check the ansible logs
# tail -f /var/log/ansible.log

Tuesday, 7 August 2018

usage of "run_once: true" option

If you want to run only once a specific task during all playbook execution, you may add "run_once: true" option into your task to do this.

e.g. example task
- name: prompt for directory name
  run_once: true
  pause:
   prompt: "Please enter directory name: "
  register: input_dir_name

- name: show input
  debug: 
   msg="{{ input_dir_name.user_input }}"

Friday, 13 July 2018

How to solve remote connection error cause of iptables/firewalld service?

Case
Machine A(192.168.1.2) wants to connect on Machine B(192.168.1.5) by database port.

Action
In Machine A;
# su - postgres
$ psql -h 192.168.1.5 -d alper -U alper -p 5433

Error
psql: could not connect to server: No route to host
        Is the server running on host "192.168.1.5" and accepting
        TCP/IP connections on port 5433?

Solution
In Machine B;
For iptables service:
# iptables -I INPUT -p tcp --dport 5433 -j ACCEPT
# iptables-save

For firewalld service:
# firewall-cmd --zone=public --add-port=5433/tcp --permanent
# firewall-cmd --reload

Check
In Machine A;
nc -vz 192.168.1.5 5433
Ncat: Connected to 192.168.1.5:5433

How to solve database module error?

Action
# ansible-playbook -i myhosts example.yml -v

Error
TASK [example role : example task] *******************
fatal: [local]: FAILED! => {"changed": false, "msg": "The MySQL-python module is required."} 

Solution
# yum install MySQL-python

The other solutions related to database tasks:
# yum install python-redis (Redisdb)
# yum install python-psycopg2 (PostgreSQL)

Check it for up to date list of database modules:
https://docs.ansible.com/ansible/latest/modules/list_of_database_modules.html

Friday, 29 June 2018

How to run a SQL query/statement in a task?

You are able to run a SQL query/statement with "echo" command by shell module for the ansible task.

# an example task for sql query/statement
- name: set default schema for user
  shell: echo "ALTER ROLE alper SET SEARCH_PATH TO myschema" | psql
  become: yes
  become_user: postgres

benefit of "gather_facts: no" setting

If you do not need any facts for your variables, you can run your playbook more fast via gather_facts setting.

For this, just add following setting in your playbook:

# example playbook
- name: Run the role for me 
  hosts: my_nodes
  gather_facts: no
  roles:
  - example-role