Need help of Ansible lineinfile module with a variable based on hostname

67 views
Skip to first unread message

FRANK L

unread,
Feb 5, 2021, 1:46:40 PM2/5/21
to Ansible Project
Hi, 

I need help of Ansible lineinfile module with a variable based on hostname. Here is what I have for the task:

On server vccs7ap01:

    - name: ulimits - find sidadm on the server
      shell: hostname | sed 's/vc//' | sed 's/ap[0-9][0-9]//g' | sed 's/$/adm/'
      register: sidadm

    - name: ulimits - add sidadm soft value to limits file
      lineinfile:
        path: /etc/security/limits.conf
        regexp: '^sidadm.stdout\ soft'
        line: "@sidadm.stdout\ soft 65536"

    - name: ulimits - add sidadm hard value to limits file
      lineinfile:
        path: /etc/security/limits.conf
        regexp: '^sidadm.stdout\ hard'
        line: "@sidadm.stdout\ hard 65536"

After ran the playbook, change was made to /etc/security/limits.conf:
@sidadm.stdout soft 65536
@sidadm.stdout hard 65536

but I would like to have the entries:
cs7adm soft 65536
cs7adm hard 65536

Thanks in advance. 

Frank

Stefan Hornburg (Racke)

unread,
Feb 6, 2021, 7:16:36 AM2/6/21
to ansible...@googlegroups.com
On 2/5/21 7:46 PM, FRANK L wrote:
> Hi, 
>
> I need help of Ansible lineinfile module with a variable based on hostname. Here is what I have for the task:>
> On server vccs7ap01:
>
>     - name: ulimits - find sidadm on the server
>       shell: hostname | sed 's/vc//' | sed 's/ap[0-9][0-9]//g' | sed 's/$/adm/'
>       register: sidadm
>

Ansible has already the hostname present in the variable "ansible_hostname", so you can really save the roundtrip to
vccs7ap01:

- name:
set_fact:
sidadm: "{{ ansible_hostname | regex_replace('vc') | regex_replace('ap[0-9][0-9]') + 'adm' }}"


>     - name: ulimits - add sidadm soft value to limits file
>       lineinfile:
>         path: /etc/security/limits.conf
>         regexp: '^sidadm.stdout\ soft'
>         line: "@sidadm.stdout\ soft 65536"
>
>     - name: ulimits - add sidadm hard value to limits file
>       lineinfile:
>         path: /etc/security/limits.conf
>         regexp: '^sidadm.stdout\ hard'
>         line: "@sidadm.stdout\ hard 65536"

There is also a dedicated module for adjusting PAM limits, so let's use that:

- name: ulimits - add sidadm soft value to limits file
pam_limits:
domain: "{{ sidadm }}"
limit_type: soft
limit_item: nofile
value: "65536"

>
> After ran the playbook, change was made to /etc/security/limits.conf:
> @sidadm.stdout soft 65536
> @sidadm.stdout hard 65536
>
> but I would like to have the entries:
> cs7adm soft 65536
> cs7adm hard 65536
>

Shouldn't that be:

cs7adm soft nofile 65536
cs7adm hard nofile 65536

Regards
Racke

> Thanks in advance. 
>
> Frank
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/d3fca17a-e73e-40fe-8c3a-967017991a50n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/d3fca17a-e73e-40fe-8c3a-967017991a50n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

FRANK L

unread,
Feb 6, 2021, 10:04:17 PM2/6/21
to Ansible Project
Thanks a lot for the tip, Racke.   It really works. 

I really appreciate your help.

Regards

Frank

Reply all
Reply to author
Forward
0 new messages