Skip to content
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
Closed

iptables: setting chain policy is not idempotent #25153

valentin-krasontovitsch opened this issue May 30, 2017 · 3 comments
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@valentin-krasontovitsch
Copy link
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
Copy link
Contributor

ansibot commented May 30, 2017

cc @LinusU
click here for bot help

@ansibot ansibot added affects_2.3 This issue/PR affects Ansible v2.3 bug_report module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. labels May 30, 2017
@valentin-krasontovitsch
Copy link
Contributor Author

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 pushed a commit to valentin-krasontovitsch/ansible that referenced this issue May 31, 2017
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 Needs a first human triage before being processed. label May 31, 2017
@ansibot ansibot added the support:core This issue/PR relates to code supported by the Ansible Engineering Team. label Jun 29, 2017
@sebastiendarocha
Copy link
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 ?

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants