How can I use same tasks yum with CentOS and dnf with Fedora on Ansible?

I use CentOS 7 on work and Fedora 28 on hobby, and I’m making an ansible’s playbook that installs some pakcage for them.

But CentOS uses yum and Fedora uses dnf. I know there exists yum module and dnf module, but separeted.

I’d like to write simple, how can I solve it? Could you please tell me.

Solution:

You could use the package module. It’ll sort out dnf vs yum behind the scenes. https://docs.ansible.com/ansible/latest/modules/package_module.html#package-module

Advertisements

Disable RPCbind

If your systemd listens on port 111 but you don’t want that, please do the following steps:

  • verify it’s listening on port 111 with netstat or ss:
# ss -tpna|grep 111
LISTEN     0      128          *:111                      *:*                   users:(("systemd",pid=1,fd=39))
LISTEN     0      128         :::111                     :::*                   users:(("systemd",pid=1,fd=38))
  • disable rpcbind:
# systemctl stop rpcbind

# systemctl disable rpcbind

# systemctl mask rpcbind

# systemctl stop rpcbind.socket

# systemctl disable rpcbind.socket

# systemctl status rpcbind
● rpcbind.service
   Loaded: masked (/dev/null; bad)
   Active: inactive (dead) since Sun 2017-12-17 15:31:11 CET; 3min 51s ago
 Main PID: 10920 (code=exited, status=0/SUCCESS)

Dec 13 13:28:35 sys.example.com systemd[1]: Starting RPC bind service...
Dec 13 13:28:35 sys.example.com systemd[1]: Started RPC bind service.
Dec 17 15:31:11 sys.example.com systemd[1]: Stopping RPC bind service...
Dec 17 15:31:11 sys.example.com systemd[1]: Stopped RPC bind service.
  • verify it’s no longer listening:
# ss -tpna|grep 111
#

CentOS inserting new line of text after another using sed not working

I’m trying to insert a line of text which is;

include /etc/nginx/sites-enabled/*;

On the next line after the words;

for more information.

in my /etc/nginx/nginx.conf using the following command

sudo sed '/for more information.'/a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf

But each time i run it, it just returns

>

Im not very good with this sort of stuff so i was hoping someone could help me out.

Cheers,

Solution:

Remove the second single-quote. It is superfluous:

sudo sed '/for more information./a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf

Also, Because . matches any character and you probably want it to match only a period, escape it like this:

sudo sed '/for more information\./a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf

Or this:

sudo sed '/for more information[.]/a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf

Search and replace the following in vi

Hi I want to replace the occurrence of [x] with [] in vi editor

I have used the following command

:%s/[x]/[]

and it gives me [[]], how to update my command so that I get desired result.

Solution:

You can replace:

  • It once (the first occurrence on the current line) with: :s/\[x\]/[]/
  • It everywhere on the current line with :s/\[x\]/[]/g
  • The first occurence on every line with :%s/\[x\]/[]/
  • It everywhere :%s/\[x\]/[]/g

(Obviously you must make sure you are in command mode, use the esc-key for this)

iterate over a map and export vars

this script prints out the right values but it looks like my export statement isn’t correct:

declare -A ENVMAP=( [foo]=bar [baz]=quux [corge]=grault )
for i in "${!ENVMAP[@]}"
do
  echo "key  : $i"
  echo "value: ${ENVMAP[$i]}"
  export "$i"="${ENVMAP[$i]}"
done

The value gets printed correctly so the map works but the vars aren’t getting exported. Whats wrong with my export statement?

Solution:

In case you want to have these vars permanently in your user’s system you could add them into your current user’s DOT/BASH profile too, few steps are as follows:

I– Go to current user’s directory by doing normal cd.

II– Then list all files including hidden ones by doing either ls -la or ls -lhtr.

III– Now either add these export commands in your BASH profile(in case you are on Linux/bash) or you could create your own .profile too(only one will be picked up at a time).

IV– Once you saved it then use source .profile or source .bash_profile or . ./.profile or . /.bash_profile.

Please try these and let me know if these helps you.

Adding a shell command inside/inline of a systemd service file

I am running the gunicorn server as a service via systemd, Here is the sample service file:

[Unit]
Description=Gunicorn NGINX
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/test
ExecStart=/usr/local/bin/gunicorn --workers 8 --threads 8 --backlog 100 --bind 10.0.0.20:5000 -m 777 abc:app
Restart=always

[Install]
WantedBy=multi-user.target

I want now to replace the number near --workers and --threads by number of cores using the shell command so that it will dynamically pick the number of cores

nproc --all

Can someone help me how to do this

Solution:

You can explicitly invoke a shell to get shell parsing.

ExecStart=/bin/bash -c '/usr/local/bin/gunicorn --workers "$(nproc --all)" --threads "$(nproc --all)" --backlog 100 --bind 10.0.0.20:5000 -m 777 abc:app'

ansible: execute a role with condition

You can execute a role in ansible with condition as follows:

---

# Installation and configuration of nodejs

- hosts: http
  user: "test"
  become: yes
  become_method: sudo
  connection: ssh
  roles:
     - { role: http, when: "ansible_distribution == 'CentOS' and ansible_distribution_major_version == '7'" }