2

I'm getting an error with a very simple play,

- name: add to environment                                                          
  lineinfile:                                                                       
    path: /etc/environment                                                          
    line: "{{ item }}"                                                              
    loop:                                                                           
      - "foo=1"                                                                     
      - "bar=2"

I simply want to add those lines to that file if they don't exist. The error I get is,

fatal: [10.1.38.15]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to have been in '/home/ecarroll/cp/ansible/roles/sandbox/tasks/main.yml': line 6, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n dest: /\n- name: add to environment\n ^ here\n"}

2 Answers 2

6

You have mis-indented your loop directive. It's not an argument to lineinfile; it's a task setting:

- name: add to environment
  lineinfile:
    path: /etc/environment
    line: "{{ item }}"
  loop:
    - "foo=1"
    - "bar=2"
0

i have implemented ansible lineinfile module with loop and it's working as expected.

Able to add multiple entry in the sudoers file using lineinfile loop module.

Hope this will meet your requirement

- name: "Providing sudo access to linux team users"
  lineinfile:
    path: /etc/sudoers
    line: '{{item}}   ALL=(ALL)       NOPASSWD: ALL'
    state: present
    create: yes
    validate: 'visudo -cf %s'
  loop:
    - Admin.Linux
    - Admin.Unix

ansible play execution output

TASK [Providing sudo access to accenture linux team members]
changed: [ANSIBLECLINETNODE] => (item=Admin.Linux)
changed: [ANSIBLECLINETNODE] => (item=Admin.Unix)

Below is the sudoers file output

[root@ANSIBLECLINETNODE~]# cat /etc/sudoers| tail -2
Admin.Linux   ALL=(ALL)       NOPASSWD: ALL
Admin.Unix   ALL=(ALL)       NOPASSWD: ALL

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .