New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iptables: setting chain policy is not idempotent #25153

Closed
valentin-krasontovitsch opened this Issue May 30, 2017 · 3 comments

Comments

Projects
None yet
5 participants
@valentin-krasontovitsch
Contributor

valentin-krasontovitsch commented May 30, 2017

ISSUE TYPE
  • Bug Report
COMPONENT NAME

iptables

ANSIBLE VERSION
ansible 2.3.0.0
  config file = /Users/valkra/code/ansible-config/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Dec 18 2016, 07:03:39) \
    [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
CONFIGURATION
OS / ENVIRONMENT

Running ansible on MacOS Sierra 12.12.3 (probably irrelevant), and managing Ubuntu 16.04 (kernel: 4.4.0-62-generic)

SUMMARY

Using the iptables module to set policies on built in chains always returns changed, is hence not idempotent.

STEPS TO REPRODUCE

Run the following playbook twice on any host with iptables, substituting the hostname appropriately.

---
- hosts: myhost.local
  tasks:
  - name: set output policy to accept
    iptables:
      chain: OUTPUT
      policy: ACCEPT
    become: yes
EXPECTED RESULTS

Should report OK at least on the second run.

ACTUAL RESULTS
PLAY [myhost.local] ***********************

TASK [Gathering Facts] *********************
ok: [myhost.local]

TASK [set output policy to accept] ***********************
changed: [myhost.local]

PLAY RECAP *****************
myhost.local         : ok=2    changed=1    unreachable=0    failed=0
@ansibot

This comment has been minimized.

Contributor

ansibot commented May 30, 2017

@valentin-krasontovitsch

This comment has been minimized.

Contributor

valentin-krasontovitsch commented May 30, 2017

Trying to fix this, I see in the code that the existence of a rule is checked using

iptables -C

which let's one check if a rule exists in a given table and chain.

As far as I can tell, there is no such option for checking the policy of a given chain.

One way I see is using something like

iptables -S $CHAIN

which returns a list of all rules, including

-P INPUT DROP

which could be grepped and cut. All in all, something along the lines of

iptables -S $CHAIN | grep -e '-P' | cut -f 3 -d ' '

or perhaps implementing the last step in python.

I'm not sure how brittle this is, and how accepted such bash hacking is in an ansible module though. Would appreciate some feedback on that.

valentin-krasontovitsch added a commit to valentin-krasontovitsch/ansible that referenced this issue May 31, 2017

Check the current policy before setting it (#25153)
To enable idempotency we check the policy of a given chain before
changing it.

Before, there was no check at all, the policy was always set and the
returned status was always changed.

Should fix ansible#25153.

@bcoca bcoca removed the needs_triage label May 31, 2017

@sebastiendarocha

This comment has been minimized.

Contributor

sebastiendarocha commented Sep 22, 2017

Didn't reproduce on 2.5.0.0

fixed by 4c72e98

test: modules/system/test_iptables : test_policy_table_no_change

@mscherer could you close this ?

@mscherer mscherer closed this Sep 22, 2017

@ansibot ansibot added bug and removed bug_report labels Mar 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment