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

**URL:** <https://forum.ansible.com/t/need-help-of-ansible-lineinfile-module-with-a-variable-based-on-hostname/34580>\
**Category:** Ansible Project\
**Created:** [February 5, 2021, 6:46pm UTC](https://forum.ansible.com/t/need-help-of-ansible-lineinfile-module-with-a-variable-based-on-hostname/34580 "2021-02-05T18:46:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FRANK\_L](https://avatars.discourse-cdn.com/v4/letter/f/839c29/32.png) [@FRANK\_L](https://forum.ansible.com/u/FRANK_L)\
**Post date:** [February 5, 2021, 6:46pm UTC](https://forum.ansible.com/t/need-help-of-ansible-lineinfile-module-with-a-variable-based-on-hostname/34580/1 "2021-02-05T18:46:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 6, 2021, 12:16pm UTC](https://forum.ansible.com/t/need-help-of-ansible-lineinfile-module-with-a-variable-based-on-hostname/34580/2 "2021-02-06T12:16:10Z")

</div>

> 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 &#39;s/vc//&#39; | sed &#39;s/ap\[0\-9\]\[0\-9\]//g&#39; | sed &#39;s/$/adm/&#39;
> register: sidadm
> 
> ```

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

- name:  
&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;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: &#39;^sidadm\.stdout\\ soft&#39;
> line: &quot;@sidadm\.stdout\\ soft 65536&quot;
> 
> \- name: ulimits \- add sidadm hard value to limits file
> lineinfile:
> path: /etc/security/limits\.conf
> regexp: &#39;^sidadm\.stdout\\ hard&#39;
> line: &quot;@sidadm\.stdout\\ hard 65536&quot;
> 
> ```

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

- name: ulimits - add sidadm soft value to limits file  
&nbsp;&nbsp;pam\_limits:  
&nbsp;&nbsp;&nbsp;&nbsp;domain: "{{ sidadm }}"  
&nbsp;&nbsp;&nbsp;&nbsp;limit\_type: soft  
&nbsp;&nbsp;&nbsp;&nbsp;limit\_item: nofile  
&nbsp;&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![FRANK\_L](https://avatars.discourse-cdn.com/v4/letter/f/839c29/32.png) [@FRANK\_L](https://forum.ansible.com/u/FRANK_L)\
**Post date:** [February 7, 2021, 3:04am UTC](https://forum.ansible.com/t/need-help-of-ansible-lineinfile-module-with-a-variable-based-on-hostname/34580/3 "2021-02-07T03:04:17Z")

</div>

Thanks a lot for the tip, Racke. It really works.

I really appreciate your help.

Regards

Frank
