J. Doe
J. Doe

Reputation: 1529

Playbook with with_items is not going through all statements

I have this code

  1 - hosts: webservers
  2   remote_user: user
  3   become: yes
  4   become_method: sudo
  5   tasks:
  6
  7   - name: Adding hosts to file
  8     lineinfile:
  9                  dest=/etc/hosts
 10                  state=present
 11                  insertafter=EOF
 12                  line="someline1"
 13     with_items:
 14       - line="someline2"
 15       - line="someline3"
 16       - line="someline4"
 17       - line="someline5"

However, when the script runs, it only goes through two of the cases, like this and it doesnt tell me about the line in the original lineinfile function -

PLAY [webservers] *************************************************************

GATHERING FACTS ***************************************************************
ok: [[email protected]]

TASK: [Adding hosts to file] **************************************************
ok: [[email protected]] => (item=line="someline2")
ok: [[email protected]] => (item=line="someline3")
ok: [[email protected]] => (item=line="someline4")
ok: [[email protected]] => (item=line="someline5")

PLAY RECAP ********************************************************************
[email protected]        : ok=2    changed=0    unreachable=0    failed=0

Someline3, 4, and 5 are not added into the file. Any idea what the issue might be?

Upvotes: 0

Views: 87

Answers (1)

helloV
helloV

Reputation: 52375

You are checking for the same line every time. Change this to suit your needs:

  - name: Adding hosts to file
    lineinfile:
                 dest=/etc/hosts
                 state=present
                 insertafter=EOF
                 line={{item}}
    with_items:
      - "someline1"
      - "someline2"
      - "someline3"
      - "someline4"
      - "someline5"

Upvotes: 2

Related Questions