# lineinfile help (file doesn't exist)

**URL:** <https://forum.ansible.com/t/lineinfile-help-file-doesnt-exist/14668>\
**Category:** Ansible Project\
**Created:** [February 27, 2014, 7:32pm UTC](https://forum.ansible.com/t/lineinfile-help-file-doesnt-exist/14668 "2014-02-27T19:32:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_R](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@Matthew\_R](https://forum.ansible.com/u/Matthew_R)\
**Post date:** [February 27, 2014, 7:32pm UTC](https://forum.ansible.com/t/lineinfile-help-file-doesnt-exist/14668/1 "2014-02-27T19:32:01Z")

</div>

Hi everyone,

I’m still a bit new to Ansible. I really like it, but it can be a little flummoxing at times for a beginner ;).

For example, I am probably going about this the wrong way, but consider these lines from a playbook:

`

- name: Configure MTA to Local Only – CIS 3.16  
lineinfile: dest=/etc/postfix/main.cf  
regexp=‘^(?i)inet\_interfaces.\*’  
line=‘inet\_interfaces = localhost’  
state=present  
`

This produces the following output:

`TASK: [cis | Configure MTA to Local Only -- CIS 3.16] ************************* failed: [devct67.umaryland.edu] => {"failed": true, "item": "", "rc": 257} msg: Destination /etc/postfix/main.cf does not exist !`

OK. So, not **all** of my machines have postfix on them. But _if_ they do, I want to run this ‘lineinfile’ command.

I have considered almost every “when” option I can think of, but most of them fail because it’s a negative test. For example, I wrote a plugin that returns all packages on a system in a dictionary. So I had a line like:

`when: "packages['postfix'] == True"`

produces:

`TASK: [cis | Configure MTA to Local Only -- CIS 3.16] ************************* fatal: [devct67.umaryland.edu] => error while evaluating conditional: umb_packages['postfix'] == True`

That fails, however, when postfix isn’t installed, which is exactly what I’m trying to find out.

So- what are my options? How _should_ I be doing this?

---

<div class="post-metadata">

**Author:** ![Walid](https://avatars.discourse-cdn.com/v4/letter/w/ea5d25/32.png) [@Walid](https://forum.ansible.com/u/Walid)\
**Post date:** [February 28, 2014, 2:40pm UTC](https://forum.ansible.com/t/lineinfile-help-file-doesnt-exist/14668/2 "2014-02-28T14:40:16Z")

</div>

you can add one extra pre-task before hand to check if the file/service exist then run handlers ( or other tasks with a registered variable e.g. when)

---

<div class="post-metadata">

**Author:** ![Mark\_Phillips](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Mark\_Phillips](https://forum.ansible.com/u/Mark_Phillips)\
**Post date:** [February 28, 2014, 5:04pm UTC](https://forum.ansible.com/t/lineinfile-help-file-doesnt-exist/14668/3 "2014-02-28T17:04:24Z")

</div>

Yup, just to echo Walid, I do a similar thing. Check for file/directory, save in register, react in following task.

An example being initialising Postgres databases…

```auto
- name: Check for default database
 stat: path={{ postgres.datadir }}/PG_VERSION
 register: pgv
 
- name: Initialise database (if doesn't already exist)
 command: /sbin/service postgresql-9.3 initdb
 when: pgv.stat.exists != true

HTH

```

---

<div class="post-metadata">

**Author:** ![Aaron\_Hunter](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron\_Hunter](https://forum.ansible.com/u/Aaron_Hunter)\
**Post date:** [February 28, 2014, 10:59pm UTC](https://forum.ansible.com/t/lineinfile-help-file-doesnt-exist/14668/4 "2014-02-28T22:59:51Z")

</div>

I addition to conditional checks mentioned, you might also consider using roles to handle this case. You could create a role that installs and configures postfix and then assign it to only those servers which need it. I prefer roles to conditional checks when possible.

–Aaron

---

<div class="post-metadata">

**Author:** ![Walid](https://avatars.discourse-cdn.com/v4/letter/w/ea5d25/32.png) [@Walid](https://forum.ansible.com/u/Walid)\
**Post date:** [March 1, 2014, 3:33pm UTC](https://forum.ansible.com/t/lineinfile-help-file-doesnt-exist/14668/5 "2014-03-01T15:33:29Z")

</div>

It looks that Matthew playbook is a CIS benchmark, I am interested in such benchmarks, and checks. to second Aaron, I too use roles with a combination of group variables to handle different nodes configuration
